我正在使用網格視圖來顯示使用Glide的圖像。我如何在網格中選中圖像?此外,我需要限制用戶只選擇一個圖像,如果用戶再次單擊一個圖像,則應取消選擇第一個圖像。如何在Grid View中點擊/選擇網格
這裏是我的xml文件
<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"
>
<GridView
android:id="@+id/galleryGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="280dp"
android:gravity="center"
android:horizontalSpacing="2dp"
android:numColumns="3"
android:padding="2dp"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp"
android:listSelector="@drawable/selected_grid">
</GridView>
</RelativeLayout>
和Java代碼
GridView gallery = (GridView) findViewById(R.id.galleryGridView);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// Help me here
}
});
}
適配器
private class ImageAdapter extends BaseAdapter {
/** The context. */
private Activity context;
/**
* Instantiates a new image adapter.
*
* @param localContext
* the local context
*/
public ImageAdapter(Activity localContext) {
context = localContext;
images = getAllShownImagesPath(context);
}
public int getCount() {
return images.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
picturesView = new ImageView(context);
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView
.setLayoutParams(new GridView.LayoutParams(270, 270));
} else {
picturesView = (ImageView) convertView;
}
Glide.with(context).load(images.get(position))
.placeholder(R.mipmap.ic_launcher).centerCrop()
.into(picturesView);
return picturesView;
}
設置你的GridView的listSelector添加ImageAdapter – Pavya
的代碼,你可以處理它裏面適配器 – mdDroid
@ Pravin ImageAdapter添加。 – Ramesh