GridView 内的 ImageButton 不可点击

ImageButton inside of GridView not clickable

这是我在网格视图中动态创建 ImageButtons 的代码。当我 运行 activity 时, ImageButtons 不可点击。我尝试添加 android:descendantFocusability="blocksDescendants" 到 xml 文件,但这并没有解决问题。

编辑:我将适配器更新为用于 ImageView,而不是 ImageButton,但问题仍然存在。

这是我的自定义适配器:

    package com.example.myname.myproject;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;

/**
 * Created by myname on 3/31/17.
 */

public class ImageArrayAdapter extends BaseAdapter {
    Context context;
    int[] currentResources;
    String[] currentIcons;


    public ImageArrayAdapter(Context context, String[] currentIcons, int[] currentResources){
        ImageArrayAdapter.this.context = context;
        ImageArrayAdapter.this.currentIcons = currentIcons;
        ImageArrayAdapter.this.currentResources = currentResources;
    }

    @Override
    public int getCount() {
        return currentResources.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
            imageView.setBackgroundColor(Color.TRANSPARENT);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setClickable(true);
        imageView.setImageResource(currentResources[position]);
        return imageView;
    }

}

这是我的 xml 文件 GridView

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myname.myproject.AdminControlTabbed$PlaceholderFragment">

<GridView
    android:id="@+id/templateGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:clickable="true"
    android:descendantFocusability="blocksDescendants"
    />

感谢您的帮助。

为什么不用 ImageView 而不是 ImageButton。然后,您只需在初始化它的 activity class 中为您的 GridView 设置一个 OnItemClickListener。将适配器附加到它之后,您也可以像这样附加一个 clickListener:

myGridView.setAdapter(this, currentIconsArray, currentResourcesArray);
myGridViewsetOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              //code for handling onClick event
        }
    });

Activity.dispatchTouchEvent(MotionEvent) 将其发送给其他听众