我知道有人问过这个问题,但不知何故这些解决方案对我不起作用。
我有一个 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/