【发布时间】:2010-09-16 14:00:02
【问题描述】:
我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:numColumns="auto_fit"
android:verticalSpacing="5dip"
android:horizontalSpacing="2.5dip"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"
android:listSelector="@drawable/transparent"
android:padding="5dip"
android:scrollbarStyle="outsideOverlay"
android:clickable="true" />
</RelativeLayout>
现在我使用一个自定义的适配器来扩展我自己的视图并将它们设置为 gridview 中的项目,并为 gridview 中的点击设置一个监听器。
GridView grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(new ProductAdapter(getApplicationContext(), products));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
...
}
});
问题是,如果我按下 gridview 中的一个单元格,侦听器不会触发。如果我使用触控板在网格中导航并按下触控板,监听器就会触发。是否有其他一些视图正在捕获点击?我的自定义视图由一个带有 ImageView 的 LinearLayout 和一个 TextView 组成,我读到为 gridview 膨胀布局会在某些地方引起问题。
如何使网格中的项目可点击?
编辑在我的适配器中扩展布局或仅实例化单个视图并将其返回给单元视图之间没有区别。
【问题讨论】: