【问题标题】:setOnItemClickListener() not working on gridViewsetOnItemClickListener() 在 gridView 上不起作用
【发布时间】:2014-06-15 07:21:16
【问题描述】:

我知道这已经被问过了,但不知何故,这些解决方案对我不起作用。

我有一个gridView,它被relativeLayout 膨胀。适配器设置完美,当我将clickListener 添加到relativeLayout 的其中一个孩子时,它们也可以正常工作。但不是gridview 上的itemClickListener

这是我尝试过的:

网格视图:

<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();

            }
        });

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

【问题讨论】:

  • 我认为你应该使用linearLayout.gv.setOnItemClickListener
  • 尝试从您的网格项目中删除此属性 android:focusable="false" android:focusableInTouchMode="false"
  • @Kedarnath 我的 gv 是:public static GridView gv;gv = (GridView) getActivity().findViewById(R.id.gridView);
  • ok 尝试将此android:descendantFocusability="blocksDescendants" 设置为您的RelativeLayout 并从每个孩子中删除android:focusable="false" android:focusableInTouchMode="false"
  • @Haresh 来自relativelayout中的所有项目?

标签: android android-layout gridview android-gridview


【解决方案1】:

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

并从每个孩子身上删除android:focusable="false" android:focusableInTouchMode="false"

【讨论】:

    【解决方案2】:

    大家好,我就是这样解决问题的;

    虽然在网格中使用可点击对象,但不幸的是android无法处理网格的点击事件。

    所以使用使用界面视图。

    示例:使用 IMAGEVIEW 代替 IMAGEBUTTON 和 BUTTON

    如果您需要在该图像中显示一些文本,请使用 textview 并将其对齐到 imageview 的顶部。

    【讨论】:

      【解决方案3】:

      我通过在我的 gridviewadapter 本身(在 getview 方法中)添加 onitemclicklistener 解决了这个问题。

      我不知道这种做法是对还是错,但它对我有用。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多