使用自定义适配器绕过 ListView 的 onListItemClick()
我意识到关于这个问题有很多问题,但是将 row.xml 中的每个视图设置为 focusable="false" 和 focusableInTouchMode="false" 的解决方案不起作用,从 ListActivity 获取 ListView 并设置也不起作用setItemsCanFocus(假)。
奇怪的是,当注册上下文菜单时,长按有效。普通的水龙头呢?没有。我尝试设置像 OnItemClickListener 这样的监听器,但没有成功。
我在某处读到,我可以通过重写适配器中的 getView() 来解决这个问题?但我不太确定这是如何运作的。注意,我不想知道用户点击了哪个视图;我只关心单击列表行以在 onListItemClick() 中启动相应的代码。
也许我的 row.xml 中有一些东西是错误的?或者它是否受到我设置 ListView 适配器的方式的影响(放置在 onResume() 而不是 onCreate() 中来更新信息)?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView style="?android:attr/listSeparatorTextViewStyle"
android:id="@+id/listSectionHeader"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:textColor="@android:color/white"
android:visibility="gone"
android:focusable="false"
android:focusableInTouchMode="false"/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="@drawable/list_button"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="true"
android:longClickable="true"
>
<TextView android:id="@+id/itemID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:textStyle="bold"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"/>
<CheckBox
android:id="@+id/returnedCheckbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<LinearLayout
android:layout_toRightOf="@id/returnedCheckbox"
android:layout_margin="5dp"
android:layout_centerVertical="true"
android:orientation="vertical"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView
android:id="@+id/stuffName"
android:layout_width="fill_parent"
android:lines="1"
android:layout_height="wrap_content"
android:ellipsize="end"
android:scrollHorizontally="true"
android:text="Hey there"
android:textSize="25sp"
android:textColor="#FFF"
android:shadowColor="#000"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<RelativeLayout
android:id="@+id/detailsLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView
android:id="@+id/dueListItem"
android:layout_width="wrap_content"
android:lines="1"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/dueListing"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/dueDate"
android:layout_toRightOf="@id/dueListItem"
android:layout_marginLeft="2dp"
android:layout_width="90dp"
android:lines="1"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="HEHEHE"
android:focusable="false"
android:focusableInTouchMode="false"/>
<ImageView
android:id="@+id/starMark"
android:layout_alignRight="@id/detailsLayout"
android:layout_toRightOf="@id/dueDate"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_height="15dp"
android:layout_width="15dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="@drawable/list_starred"
android:visibility="invisible"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
</LinearLayout>
<ImageView
android:id="@+id/contactPic"
android:layout_alignParentRight="true"
android:layout_height="48dp"
android:layout_width="48dp"
android:background="#FFF"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:padding="3dp"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<ImageView
android:id="@+id/lentArrow"
android:visibility="invisible"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/indicator_lent"
android:layout_marginTop="42dp"
android:layout_marginRight="1dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
</LinearLayout>
I realize there are a ton of questions regarding this issue, but the solution of setting every view in the row.xml to focusable="false" and focusableInTouchMode="false" do not work, nor does getting the ListView from the ListActivity and setting setItemsCanFocus(false).
Weirdly, when registered for a context menu, the long tap works. The regular tap though? Nope. I tried setting listeners like OnItemClickListener to no avail.
I read somewhere that I might be able to remedy this by overriding getView() in my Adapter? I'm not too sure how that works though. Note, I don't want to know what view the user has clicked; I just care about the list row being clicked to initiate the corresponding code in onListItemClick().
Maybe there's something in my row.xml that's all wrong? Or is it affected by the way I set my ListView's adapter (placed in onResume() instead of onCreate() to update information)?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView style="?android:attr/listSeparatorTextViewStyle"
android:id="@+id/listSectionHeader"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:textColor="@android:color/white"
android:visibility="gone"
android:focusable="false"
android:focusableInTouchMode="false"/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="@drawable/list_button"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="true"
android:longClickable="true"
>
<TextView android:id="@+id/itemID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:textStyle="bold"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"/>
<CheckBox
android:id="@+id/returnedCheckbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<LinearLayout
android:layout_toRightOf="@id/returnedCheckbox"
android:layout_margin="5dp"
android:layout_centerVertical="true"
android:orientation="vertical"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView
android:id="@+id/stuffName"
android:layout_width="fill_parent"
android:lines="1"
android:layout_height="wrap_content"
android:ellipsize="end"
android:scrollHorizontally="true"
android:text="Hey there"
android:textSize="25sp"
android:textColor="#FFF"
android:shadowColor="#000"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<RelativeLayout
android:id="@+id/detailsLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView
android:id="@+id/dueListItem"
android:layout_width="wrap_content"
android:lines="1"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/dueListing"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/dueDate"
android:layout_toRightOf="@id/dueListItem"
android:layout_marginLeft="2dp"
android:layout_width="90dp"
android:lines="1"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="HEHEHE"
android:focusable="false"
android:focusableInTouchMode="false"/>
<ImageView
android:id="@+id/starMark"
android:layout_alignRight="@id/detailsLayout"
android:layout_toRightOf="@id/dueDate"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_height="15dp"
android:layout_width="15dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="@drawable/list_starred"
android:visibility="invisible"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
</LinearLayout>
<ImageView
android:id="@+id/contactPic"
android:layout_alignParentRight="true"
android:layout_height="48dp"
android:layout_width="48dp"
android:background="#FFF"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:padding="3dp"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<ImageView
android:id="@+id/lentArrow"
android:visibility="invisible"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/indicator_lent"
android:layout_marginTop="42dp"
android:layout_marginRight="1dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Onlistitemclick 永远不会被调用,因为列表项中有一个可单击的视图。删除复选框并查看是否能够获得点击次数。
还有一种替代方法可以直接在项目布局中使用复选框。如果适合您的需要,请使用
android:choiceMode="multipleChoice"
作为列表视图。The Onlistitemclick will never be called because there is a clickable view in your list item. Remove the checkbox and see if you are able to get the clicks.
There is an alternative to using using a checkbox directly in your item layout. Use
android:choiceMode="multipleChoice"
for listview if it suits your needs.查看此链接,了解如何创建自定义适配器:
http://android-er.blogspot.com/2010/06/using-convertview-in-getview-to-make.html
在 Override getView() 方法中,您可以设置这row.setOnClickListener() 来执行单击该列表项时需要执行的操作。
理想情况下,您可以使用 ConvertView 来填充 ViewHolder 类,这样您就不会在创建列表项时重新创建列表项,但这是一个不同的问题。
编辑
下面是扩展 SimpleCursorAdapter 的精简自定义实现:
Take a look at this link in how to create a custom adapter:
http://android-er.blogspot.com/2010/06/using-convertview-in-getview-to-make.html
In the Override getView() method, you could just set the row.setOnClickListener() to do what you needed to do when that list item is clicked.
Ideally, you would use convertView to fill a ViewHolder class so that you aren't recreating list items when they have already been created, but that's a different question.
Edit
Here's a slimmed down custom implementation of extending a SimpleCursorAdapter:
我不需要对我的适配器做任何事情;由于某种原因,
onListItemClick()
奇迹般地自行工作了。我现在更摸不着头脑了。顺便说一句,我扩展了
CursorAdapter
,而不是SimpleCursorAdapter
,我知道这是有点不同的。据我所知,CursorAdapter扩展只需要重写newView()和bindView()而不是基于此的通常的getView() a href="http://www.workreloaded.com/2011/02/android-extending-cursoradapter-for-custom-listview/" rel="nofollow noreferrer">http://www.workreloaded.com/2011/02/android-extending-cursoradapter-for-custom-listview/。I didn't have to do anything to my adapter;
onListItemClick()
just miraculously worked on its own for some reason. I'm scratching my head even more right now.Incidentally, I extended
CursorAdapter
, notSimpleCursorAdapter
, which I understand is sort of different. From what I read,CursorAdapter
extension requires only overriding thenewView()
andbindView()
instead of the usual getView() based on this http://www.workreloaded.com/2011/02/android-extending-cursoradapter-for-custom-listview/.