ListView 中的 OnItemClickListener 和 OnClickListener
我在自定义 ListAdapter 中的每一行都有一个自定义视图,我正在尝试执行 onClick 操作并获取单击来自的列表中的行位置。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingLeft="10dip" android:id="@+id/itemRoot" android:clickable="false">
<TextView android:layout_width="wrap_content" android:text="TextView"
android:layout_height="wrap_content" android:id="@+id/itemTxt"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"></TextView>
<TextView android:layout_toRightOf="@+id/itemTxt" android:text="TextView"
android:layout_height="wrap_content" android:layout_alignBottom="@+id/itemTxt"
android:id="@+id/amountTxt" android:paddingLeft="6dip"
android:layout_centerVertical="true" android:layout_width="match_parent"></TextView>
<ImageView android:id="@+id/delBtn" android:layout_height="wrap_content"
android:src="@drawable/delete" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:paddingRight="10dip"
android:layout_centerVertical="true"></ImageView>
</RelativeLayout>
我想弄清楚何时单击 TextView 或 ImageView,我还需要知道它来自列表中的哪一行。我尝试过使用 OnItemClickListener,它可以很好地获取点击来自的行。但是,一旦我为视图注册了 OnClick 侦听器, onItemClicked() 方法就会被跳过并立即执行 onClick() ,但是无法获取视图所在的行位置(或者至少不是我)知道如果)。
如果我将视图的 clickable 设置为 false,则 onItemClicked 会被调用,并且我尝试在给定视图上手动调用 PerformClick() 。但这仅适用于根元素(RelativeLayout),如果单击来自布局内的 TextView,则单击不会传播。
我真的无法弄清楚如何获取列表中的位置并执行 onClick 操作。
欢迎任何想法。
亚历克斯
I have a custom view for each row in a custom ListAdapter and I am trying to perform onClick action and get the row position in the list where the click came from.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingLeft="10dip" android:id="@+id/itemRoot" android:clickable="false">
<TextView android:layout_width="wrap_content" android:text="TextView"
android:layout_height="wrap_content" android:id="@+id/itemTxt"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"></TextView>
<TextView android:layout_toRightOf="@+id/itemTxt" android:text="TextView"
android:layout_height="wrap_content" android:layout_alignBottom="@+id/itemTxt"
android:id="@+id/amountTxt" android:paddingLeft="6dip"
android:layout_centerVertical="true" android:layout_width="match_parent"></TextView>
<ImageView android:id="@+id/delBtn" android:layout_height="wrap_content"
android:src="@drawable/delete" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:paddingRight="10dip"
android:layout_centerVertical="true"></ImageView>
</RelativeLayout>
I want to figure out when TextView or ImageView is clicked, I also need to know what row in the list it came form. I have tried using OnItemClickListener and it works fine to get the row where the click comes from. However, once I register an OnClick listener for the views, the onItemClicked() method gets skipped and onClick() is executed straight away, but there is no way of getting the row position that the view is in(or at least not that I know if).
If I set clickable to false for the views, then onItemClicked get called and I tried manually calling performClick() on the given view. But this only works for the root element (RelativeLayout), and if click comes from TextView inside the layout the click doesn't propagate.
I can't really figure out how to get both position in the list and perform onClick action.
Any thoughts are welcome.
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从
ListAdapter
覆盖的getView 内部为每个
ImageView
和TextView
分配适当的OnClickListener
方法。在该方法中,您知道该项目的位置,因此您可以将其传递给自定义侦听器类,并根据需要在那里使用它:
You could assign the proper
OnClickListener
to eachImageView
andTextView
from inside yourListAdapter
's overriddengetView
method.Inside that method you know the item's position, so you can pass it to the custom listener classes, and use it there as you want:
将 OnClickListener 与创建活动放在同一类中的另一个可能的选项是添加到活动
implements OnItemClickListener
。然后设置自定义列表进行侦听
最后在 onItemClick 返回中,您可以使用资源 ID 找到内部视图
这是我采用的解决方案。我将 LinearLayout 用于自定义布局容器,但我认为这同样适用于relativelayout。
The other possible option to have the OnClickListener in the same class as the creating activity is to add to the activity
implements OnItemClickListener
.Then set the custom list to listen
Finally in the onItemClick return, you can find the inner views by using resource ID
This is the solution I went with. I used LinearLayout for my custom layout container, but I assume the same applies to RelativeLayout.
您需要扩展 TextView 等,覆盖 click 方法,执行适当的操作,然后技巧是返回 false,以便将其传播到列表视图。
You need to extend your TextView etc, override the click method, do the appropriate actions and then the trick is to return false so it's then propagated to the listview.