【发布时间】:2014-06-28 23:37:38
【问题描述】:
当一个 .xml 文件代表网格的一般视图而另一个 .xml 文件代表该网格内的自定义项目时,如何正确实现项目单击侦听器?
概览fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"
/>
以及图片和文字视图的项目视图gridview_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.myapp.gridview.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_gravity="bottom"
android:textColor="@android:color/white"
android:background="#55000000"
/>
</FrameLayout>
需要在哪里写android:onClick="" - 在fragment_main 或gridview_item?是否还有其他需要启用的 gridview 属性?
感谢您的建议。
【问题讨论】:
-
使用
gridview_item.setOnItemClickListener
标签: android xml gridview layout onclicklistener