setOnItemClickListener()在Fragment中的gridView上不起作用

我在A Fragment中有一个gridview我为此设置了onItemClick,但是它不起作用

我尝试了android:descendantFocusability="blocksDescendants",但仍未点击

这是代码

public class ImageGalfrag extends Fragment {
    private Cursor cursor;
     private int columnIndex;
    ImageView thumbV;
    ImageButton videoICON;
    ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
//   @Override
//    public void onCreate(Bundle savedInstanceState) {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.gallery_gridview, container, false);
}
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //setContentView(R.layout.gallery_gridview);
        try {
            String[] projection = {MediaStore.Images.Thumbnails._ID};

            cursor = getActivity().managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                    projection, // Which columns to return
                    null,       // Return all rows
                    null,
                    MediaStore.Images.Thumbnails.IMAGE_ID);
            View v = getView();
            // Get the column index of the Thumbnails Image ID
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
            GridView sdcardImages = (GridView) v.findViewById(R.id.PhoneImageGrid);
            sdcardImages.setAdapter(new ImageAdapter(getActivity()));
            sdcardImages.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView parent, View v, int position, long id) {
                  String[] projection = {MediaStore.Images.Media.DATA};
                   cursor = getActivity().managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                           projection, // Which columns to return
                           null,       // Return all rows
                           null,
                           null);
                   columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                   cursor.moveToPosition(position);
                   String imagePath = cursor.getString(columnIndex);
           Toast.makeText(getActivity(), "" + imagePath, Toast.LENGTH_SHORT).show();
                   ImageAdd(imagePath);
                   Intent ImageIntent = new Intent(getActivity(), ZoomImageView.class);
                   ImageIntent.putExtra("ImageName", imagePath);
                   getActivity().startActivity(ImageIntent);

               }
           });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
    public void ImageAdd(String imagePath){

        try {
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
            bitmapArray.add(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private class ImageAdapter extends BaseAdapter {
        private Context context;
        public ImageAdapter(Context localContext) {
            context = localContext;
        }
        public int getCount() {
            return cursor.getCount();
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            View grid = null;
            try {
                LayoutInflater inflater = getActivity().getLayoutInflater();
                if (convertView == null) {
                    try {
                        cursor.moveToPosition(position);
                        int imageID = cursor.getInt(columnIndex);
                        //grid = new View(mContext);
                        grid = inflater.inflate(R.layout.gallery_view, null);
                        //TextView textView = (TextView) grid.findViewById(R.id.grid_text);
                        thumbV = (ImageView) grid.findViewById(R.id.thumbImage);
                        thumbV.setImageURI(Uri.withAppendedPath(
                                MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
                        videoICON = (ImageButton) grid.findViewById(R.id.videoICON);
                        //textView.setText("");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    grid = (View) convertView;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return grid;
        }
    }
}


布局是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    >

    <GridView
        android:id="@+id/PhoneImageGrid"
        android:numColumns="2"
        android:gravity="center"
        android:columnWidth="250dp"
        android:stretchMode="columnWidth"
        android:layout_width="fill_parent"

        android:layout_height="wrap_content"></GridView>

</RelativeLayout>


网格项目为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/fragment_gallery1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:padding="5dp">

    <LinearLayout

        android:id="@+id/fragment_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#455569"
        android:orientation="vertical"
        android:padding="5dp">

        <ImageView
            android:id="@+id/thumbImage"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center_horizontal"
            android:background="#CCCCCC"
            android:scaleType="fitXY"
            android:layout_margin="5dp"
            android:src="@mipmap/ic_launcher" />

        <ImageButton
            android:id="@+id/videoICON"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_below="@+id/thumbImage"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="0dp"
            android:background="#00000000"
            android:scaleType="fitXY" />

    </LinearLayout>
</LinearLayout>

最佳答案

不要使用ItemClickListener

在您的getView()方法中,在您的适配器中。做这个:

 @Override
 public View getView(final int position ...

    grid.setOnClickListener(new View.OnClickListener() {
        @Override

    public void onClick(View v) {

    //Do something

    }});

09-10 14:10