android - setOnItemClickListener() 无法在 Fragment 中的 gridView 上工作

标签 android android-fragments gridview

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

    }});

关于android - setOnItemClickListener() 无法在 Fragment 中的 gridView 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44971195/

相关文章:

带有 AsyncLayoutInflater 的 Android Fragment onCreateView

Android ImageButton 在 Activity 中运行良好。它在 fragment 中不起作用

android - android studio中的proguard问题

android - 什么时候调用Application类的getApplicationContext()?

java - 单击时显示对话框首选项

android - 失败保存状态 - 目标不在 fragment 管理器中 (setTargetFragment)

c# - 更改 gridview 单元格中特殊单词的前景色

java - fragment 上的自定义 GridView 上的 NullPointerException

c# - Gridview 禁用对 1 列 asp.net 的编辑

安卓 - Scandit - java.lang.UnsatisfiedLinkError