我有一个GridView,我在其中显示图像。单击图像,图像将扩展为全屏。一切都很好。现在我想刷这些图像。当我滑动图像时,没有任何反应。它仍然保持原样。任何人都可以帮助我,代码中有什么不对。下面是我的代码和布局文件。
public class MainActivity extends Activity {
private GridView gridview;
private int j = 0;
private final GestureDetector detector = new GestureDetector(new SwipeGestureDetector());
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
// Create Array thumbs resource id's:
private int thumb[] = { R.drawable.logowhite,
R.drawable.logo_bg,
R.drawable.logowhite,
R.drawable.logo_bg,
R.drawable.logowhite,
R.drawable.logo_bg, };
private ImageView expandedImageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the variables:
gridview = (GridView) findViewById(R.id.gridView);
// Set an Adapter to the ListView
gridview.setAdapter(new ImageAdapter(this));
// Set on item click listener to the ListView
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
j = pos;
setContentView(R.layout.slide_image);
expandedImageView = (ImageView) findViewById(R.id.imageViewslides);
expandedImageView.setBackgroundResource(thumb[pos]);
}
});
}
// Create an Adapter Class extending the BaseAdapter
class ImageAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageAdapter(MainActivity activity) {
// TODO Auto-generated constructor stub
layoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// Set the count value to the total number of items in the Array
return thumb.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.grid_item, null);
}
// Initialize the views in the layout
ImageView imgView = (ImageView) listItem.findViewById(R.id.thumb);
// Set the views in the layout
imgView.setBackgroundResource(thumb[pos]);
return listItem;
}
}
private class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if(thumb.length>j) {
j++;
if(j < thumb.length){
expandedImageView.setImageResource(thumb[j]);
return true;
}
else {
j = 0;
expandedImageView.setImageResource(thumb[j]);
return true;
}
}
}
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if(j>0){
j--;
expandedImageView.setImageResource(thumb[j]);
return true;
}
else {
j = thumb.length-1;
expandedImageView.setImageResource(thumb[j]);
return true;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
布局文件是:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mukesh.gridviewimageswipe.MainActivity">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="80dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
<ImageView
android:id="@+id/expanded_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"
android:visibility="invisible" />
</FrameLayout>
</RelativeLayout>
grid_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<ImageView
android:id="@+id/thumb"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_centerHorizontal="true"
android:contentDescription="@string/app_name" />
</RelativeLayout>
slide_image.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/imageViewslide"
android:layout_width="fill_parent"
android:layout_height="420dp"
android:orientation="horizontal"
android:layout_below="@+id/textq">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageViewslides">
</ImageView>
</LinearLayout>
</LinearLayout>