OnItemClickListener和OnClickListener中的ListViewOnItemClickListener、OnClickListener、ListView

2023-09-08 09:17:39 作者:帅到没朋友

我有一个自定义ListAdapter每一行的自定义视图,我想执行的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>

我想弄清楚的TextView和ImageView的被点击的时候,我还需要知道什么排在列表中它来形式。我已经使用OnItemClickListener尝试,它工作正常,以到达点击来自该行。但是,一旦我注册一个onClick侦听器的意见,onItemClicked()方法跳过的onClick()是直接执行,但是也没有办法获得该行的位置,该观点是(或者至少不是我知道)。

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).

如果我点击设置为false的意见,然后onItemClicked被调用,我试图手动调用performClick()在给定的视图。但是,这仅适用于根元素(RelativeLayout的),如果点击来自的TextView布局内的点击不传播。

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.

我真的不能找出如何让这两个列表中的位置和执行的onClick动作。

I can't really figure out how to get both position in the list and perform onClick action.

任何的想法是值得欢迎的。

Any thoughts are welcome.

亚历

推荐答案

您可以指定合适的 OnClickListener 每个的ImageView 的TextView 从里你的 ListAdapter 的重写 getView 方法。

You could assign the proper OnClickListener to each ImageView and TextView from inside your ListAdapter's overridden getView 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:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    // TODO: instantiate the layout 
    // here I call a super method
    final View view = super.getView(position, convertView, parent);
    final TextView textView = (TextView)view.findViewById(R.id.itemTxt);
    textView.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Log.i("Click", "TextView clicked on row " + position);
        }
    });
    final ImageView imageView = (ImageView)view.findViewById(R.id.delBtn);
    imageView.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Log.i("Click", "ImageView clicked on row " + position);
        }
    });
    return view;
}