【问题标题】:Gridview OnItemClickListener not working with cardViewGridview OnItemClickListener 不适用于 cardView
【发布时间】:2016-07-29 09:08:22
【问题描述】:

我想为我的 gridView 实现一个OnItemClickListener

我在片段中这样定义监听器:

    GridView gridview = (GridView)view.findViewById(R.id.gridview);
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("imagedata","Position clicked: "+ position);
        }
    });

    List<ItemObject> allItems = ((MainActivity) getActivity()).getTinkeractivities(); //getJSON data
    CustomAdapter customAdapter = new CustomAdapter(getActivity(), allItems, "home");
    gridview.setAdapter(customAdapter);

gridView 中每个项目的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <ImageView
        android:id="@+id/screen_shot"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:contentDescription="@string/hello_world"
        android:layout_alignParentTop="true"
        android:scaleType="centerCrop"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/activity_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/screen_shot"
        android:layout_alignLeft="@+id/screen_shot"
        android:text="@string/hello_world"
        android:inputType="text"
        android:textColor="#000"
        android:layout_marginTop="8dp"
        android:textSize="12sp"/>

    <TextView
        android:id="@+id/theme_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/activity_name"
        android:layout_alignLeft="@+id/activity_name"
        android:text="@string/hello_world"
        android:textSize="12sp"
        android:layout_marginTop="4dp"/>

</RelativeLayout>
</android.support.v7.widget.CardView>

但是,点击监听器似乎没有工作。我尝试在卡片视图的所有子视图中将android:clickable 设置为false,并尝试在卡片视图本身上将其设置为false,但没有成功。

我能够在我的CustomAdapter 中为每个行视图成功设置点击侦听器,但我不想采用这种方法,因为我想使用标准在我的gridview 上使用custom action bar 进行一些批量选择执行。否则我将不得不处理longPress 事件并为我的行项设置checked 状态,然后在适配器中实现自定义操作栏。

为了有一个干净的实现,我想在我的片段中定义click 监听器。我在 Stackoverflow 上阅读了几篇博客文章和相关答案,都建议使用适配器方法,但没有指定如何使其以正确的方式在活动或片段中工作。

我想知道在我的情况下是否无法设置OnItemClickListener。如果没有,那我该如何调试并解决问题?

【问题讨论】:

  • 也许将CardView 包裹在RelativeLayout 内。应该可以的。
  • 对卡片视图使用线性布局
  • @VoraAnkit,这不是您刚刚发布的问题的重复。那里的 OP 希望在他的 gridView 实现中为子项设置不同的点击侦听器。我想为我的网格视图实现一个OnItemClickListener。问题是它没有被调用可能是因为卡片视图。我在布局中的其他元素,即 ImageViewTextView 不应该干扰我猜的行的可点击性。
  • @AkshayBhat,你的意思是把 cardView 放在一个线性布局中?
  • @vrundpurohit,尝试了你的方法。将CardView 放入RelativeLayout 但无济于事:(。我该如何调试这个问题?有什么想法吗?

标签: android android-fragments gridview android-cardview onitemclicklistener


【解决方案1】:

你可以尝试添加

android:descendantFocusability="blocksDescendants"

到您的网格项目


编辑:

如果你有可以阻止点击的按钮,你可以添加:

android:focusable="false"
android:focusableInTouchMode="false"

【讨论】:

  • 我应该在哪里添加android:descendantFocusability="blocksDescendants"。在CardView 小部件项中?另外,我没有任何按钮。我的布局中只有一个图像视图和 2 个文本视图..
  • 啊,将它添加到我的RelativeLayout 中,我将CardView 放入其中。在我的CardView 上设置android:focussable = false 并将android:descendantFocusability="blocksDescendants" 在我的RelativeLayout 上。奇迹般有效! :)
  • 是的,在父布局中添加 descendantFocusability :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-02
相关资源
最近更新 更多