我制作了一个画廊,在这个中,当我在全 View 中打开图像(例如图像1),然后向右滑动时,仅图像1旁边的图像(即图像2)出现,而图像2后面的图像不会显示,并且类似的事情发生在我按图像 1 的相反方向单击。
FullimageActivity.java
package com.example.android.helloapp;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
public class Full_ImageActivity extends AppCompatActivity {
float startXValue = 1;
ImageView fullImageView;
int position;
private Integer[] mImageIds = { R.drawable.gallery_one,R.drawable.gallery_two,R.drawable.gallery_three,R.drawable.gallery_four,R.drawable.gallery_five,
R.drawable.gallery_six,R.drawable.gallery_seven,R.drawable.gallery_eight,R.drawable.gallery_nine,R.drawable.gallery_ten,
R.drawable.gallery_eleven,R.drawable.gallery_twelve,R.drawable.gallery_thirteen,R.drawable.gallery_fourteen,R.drawable.gallery_fifteen,
R.drawable.gallery_sixteen,R.drawable.gallery_seventeen,R.drawable.gallery_eighteen,R.drawable.gallery_nineteen,
R.drawable.gallery_twenty,R.drawable.gallery_twenty_one,R.drawable.gallery_twenty_two,R.drawable.gallery_twenty_three,R.drawable.gallery_twenty_four,
R.drawable.gallery_twenty_five};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_full__image);
} else {
setContentView(R.layout.activity_full__image);
}
Intent i=getIntent();
position=i.getExtras().getInt("id");
ImageAdapter imageAdapter=new ImageAdapter(this);
fullImageView=(ImageView)findViewById(R.id.fullImageView);
fullImageView.setImageResource(imageAdapter.images[position]);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation;
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
// or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}else {
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
// Add code if needed
setRequestedOrientation(orientation);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float endXValue=0;
float x1=event.getAxisValue(MotionEvent.AXIS_X);
int action= MotionEventCompat.getActionMasked(event);
switch (action){
case (MotionEvent.ACTION_DOWN):
startXValue=event.getAxisValue(MotionEvent.AXIS_X);
return true;
case (MotionEvent.ACTION_UP):
endXValue = event.getAxisValue(MotionEvent.AXIS_X);
if (endXValue > startXValue) {
if (endXValue - startXValue > 100) {
System.out.println("Left-Right");
if(position-1!=-1)
fullImageView.setImageResource(mImageIds[position-1]);
}
}else {
if (startXValue -endXValue> 100) {
System.out.println("Right-Left");
if(position+1!=mImageIds.length)
fullImageView.setImageResource(mImageIds[position+1]);
}
}
return true;
default:
return super.onTouchEvent(event);
}
}
}
fullimage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_full__image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.helloapp.Full_ImageActivity">
<include layout="@layout/tool_bar"></include>
<ImageView
android:id="@+id/fullImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:layout_gravity="center"
/>
</LinearLayout>
ImageAdapter.java
package com.example.android.helloapp;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context context;
public Integer[] images={
R.drawable.gallery_one,R.drawable.gallery_two,R.drawable.gallery_three,R.drawable.gallery_four,R.drawable.gallery_five,
R.drawable.gallery_six,R.drawable.gallery_seven,R.drawable.gallery_eight,R.drawable.gallery_nine,R.drawable.gallery_ten,
R.drawable.gallery_eleven,R.drawable.gallery_twelve,R.drawable.gallery_thirteen,R.drawable.gallery_fourteen,R.drawable.gallery_fifteen,
R.drawable.gallery_sixteen,R.drawable.gallery_seventeen,R.drawable.gallery_eighteen,R.drawable.gallery_nineteen,
R.drawable.gallery_twenty,R.drawable.gallery_twenty_one,R.drawable.gallery_twenty_two,R.drawable.gallery_twenty_three,R.drawable.gallery_twenty_four,
R.drawable.gallery_twenty_five
};
public ImageAdapter(Context c){
context=c;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return images[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=new ImageView(context);
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setLayoutParams(new GridView.LayoutParams(240,240));
return imageView;
}
}
最佳答案
打开任何图像后,我们假设位置为3
所以向右走并显示 position+1
图像,但position的值仍然是3
,所以下次再次滑动position+1
意味着将显示第四张图像
解决方案:将新的位置值重新分配给position
变量或使用一元运算符,即position--
或position++
关于java - 图片没有被滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42714248/