Gridview OnItemClickListener 不适用于 cardView
Gridview OnItemClickListener not working with cardView
我想为我的 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
中为每个行视图成功设置点击侦听器,但我不想采用这种方法,因为我想使用 custom action bar 进行一些批量选择在我的 gridview 上使用标准实现。否则我将不得不处理 longPress
事件并为我的行项设置 checked
状态,然后努力从适配器中实现自定义操作栏。
为了有一个干净的实现,我想在我的片段中定义 click
侦听器。我在 Whosebug 上阅读了几篇博客文章和相关答案,所有这些都建议使用适配器方法并 none 指定如何使其以正确的方式在 activity 或片段中工作。
我想知道在我的情况下是否无法设置 OnItemClickListener
。如果不是,那我该如何调试并解决问题?
您可以尝试添加
android:descendantFocusability="blocksDescendants"
到您的网格项目
编辑:
否则,如果您有可以阻止点击的按钮,您可以添加:
android:focusable="false"
android:focusableInTouchMode="false"
我想为我的 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
中为每个行视图成功设置点击侦听器,但我不想采用这种方法,因为我想使用 custom action bar 进行一些批量选择在我的 gridview 上使用标准实现。否则我将不得不处理 longPress
事件并为我的行项设置 checked
状态,然后努力从适配器中实现自定义操作栏。
为了有一个干净的实现,我想在我的片段中定义 click
侦听器。我在 Whosebug 上阅读了几篇博客文章和相关答案,所有这些都建议使用适配器方法并 none 指定如何使其以正确的方式在 activity 或片段中工作。
我想知道在我的情况下是否无法设置 OnItemClickListener
。如果不是,那我该如何调试并解决问题?
您可以尝试添加
android:descendantFocusability="blocksDescendants"
到您的网格项目
编辑:
否则,如果您有可以阻止点击的按钮,您可以添加:
android:focusable="false"
android:focusableInTouchMode="false"