【发布时间】:2014-11-17 18:33:18
【问题描述】:
最初我是适配器的 getView() 以编程方式创建视图,其中仅加载了一个图像,gridview 上的onItemClick 在这里工作。我需要在每个单元格中使用更复杂的视图,因此我将 getView() 更改为使用 XML 文件,现在所有 gridview 侦听器都不起作用。切换回程序化视图 getView() 按预期工作。
这是我的适配器单元格的 XML:
注意:我对编程视图使用了相同类型的扩展对象(RecyclingImageView、ImageViewTintState),因此不需要查看它们的代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/containerView"
android:focusable="false"
android:clickable="true"
android:focusableInTouchMode="false"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.whosay.talent.views.RecyclingImageView
android:id="@+id/gridImage"
android:layout_width="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="match_parent"
app:tint="@color/image_click_state"
android:scaleType="centerCrop" />
<com.whosay.talent.views.ImageViewTintState
android:id="@+id/videoIconView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_gravity="center"
android:src="@drawable/icon_play_img"
app:tint="@color/image_click_state"
android:visibility="invisible" />
如您所见,我尝试了其他问题建议的 onFocusable 技巧。我也尝试过使用更基本的视图,例如ImageView,但似乎任何在 gridview 单元格中具有多个视图的东西都会破坏项目单击方法。此外,将 onClickListener 添加到 getView 中的各个视图也不起作用,即使添加了 clickable:true
这是我的 Gridview 的 xml。
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="@android:color/white"
android:gravity="center"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
【问题讨论】:
标签: android gridview adapter onitemclicklistener