android - setOnItemClickListener() 不适用于 gridView

标签 android android-layout gridview android-gridview

我知道有人问过这个问题,但不知何故这些解决方案对我不起作用。

我有一个 gridView,它由 relativeLayout 扩展。适配器设置完美,当我将 clickListener 添加到 relativeLayout 的子项之一时,它们也可以正常工作。但不是 gridview 上的 itemClickListener

这是我尝试过的:

GridView :

<GridView
            android:id="@+id/gridView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clickable="true"
            android:clipToPadding="true"
            android:columnWidth="170dp"
            android:fitsSystemWindows="true"
            android:focusable="true"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth" >
</GridView>

gridview中添加的Relativelayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlImage"
    android:layout_width="220dp"
    android:layout_height="180dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:paddingBottom="7dp"
    android:paddingRight="7dp" >

    <ImageButton
        android:id="@+id/image"
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:scaleType="centerCrop"
        android:src="@drawable/noimage" />

    <!-- <ImageButton -->
    <!-- android:id="@+id/imageHover" -->
    <!-- android:layout_width="220dp" -->
    <!-- android:layout_height="220dp" -->
    <!-- android:adjustViewBounds="true" -->
    <!-- android:background="@null" -->
    <!-- android:scaleType="fitXY" -->
    <!-- android:src="@drawable/tile_selector_style" /> -->

    <TextView
        android:id="@+id/ShowTitle"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:hint="Show Title"
        android:paddingBottom="40dp"
        android:paddingRight="20dp"
        android:singleLine="true" />

    <TextView
        android:id="@+id/ShowTime"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:hint="Show Time"
        android:paddingBottom="20dp"
        android:paddingRight="20dp"
        android:singleLine="true" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="0dp"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</RelativeLayout>

gridview监听器的设置:

gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Log.v("griditem", "clicked");
                Toast.makeText(context, "Here", Toast.LENGTH_LONG).show();

            }
        });

不知道这里出了什么问题..

最佳答案

尝试将 android:descendantFocusability="blocksDescendants" 设置为您的 RelativeLayout

并从每个 child 身上移除 android:focusable="false"android:focusableInTouchMode="false"

关于android - setOnItemClickListener() 不适用于 gridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363229/

相关文章:

android - GridView 第一项不工作

c# - 来自 json 的 XAML GridView 设计时数据

android - 您如何做到这一点,以便在流程结束时,正在进行的通知振动并播放声音?

java - 在没有 Android Studio 运行选项的情况下运行 UiAutomator 测试

java - 如何在 Android studio 中更新分数和星星?

android - RelativeLayout/customview 不填充选项卡的宽度

LINQ OrderBy 字符串参数

java - 将字符串数组传递给另一个 Activity 以与 ion 一起使用

android - 在 android N 以下本地化 Android 阿拉伯语

android - TableLayout 行拉伸(stretch)/收缩