ListView 中的 OnItemClickListener 和 OnClickListener

发布于 2024-11-09 05:15:46 字数 1715 浏览 0 评论 0原文

我在自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酒忠诚 2024-11-16 05:15:46

您可以从 ListAdapter 覆盖的 getView 内部为每个 ImageViewTextView 分配适当的 OnClickListener方法。

在该方法中,您知道该项目的位置,因此您可以将其传递给自定义侦听器类,并根据需要在那里使用它:

@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;
}

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;
}
伏妖词 2024-11-16 05:15:46

将 OnClickListener 与创建活动放在同一类中的另一个可能的选项是添加到活动 implements OnItemClickListener

public class DisplayListCustom extends Activity implements OnItemClickListener

然后设置自定义列表进行侦听

ListView list = (ListView)findViewById(R.id.custom_list);
list.setClickable(true);
list.setOnItemClickListener(this);
list.setAdapter(new CustomListAdapter(getApplicationContext(), listItems));

最后在 onItemClick 返回中,您可以使用资源 ID 找到内部视图

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    LinearLayout listItem = (LinearLayout) v;
    TextView clickedItemView = (TextView) listItem.findViewById(R.id.name);
    String clickedItemString = clickedItemView.getText().toString();
    Log.i("DisplayListCustom", "Click detected " + clickedItemString + ", position " + Integer.toString(position));

这是我采用的解决方案。我将 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.

public class DisplayListCustom extends Activity implements OnItemClickListener

Then set the custom list to listen

ListView list = (ListView)findViewById(R.id.custom_list);
list.setClickable(true);
list.setOnItemClickListener(this);
list.setAdapter(new CustomListAdapter(getApplicationContext(), listItems));

Finally in the onItemClick return, you can find the inner views by using resource ID

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    LinearLayout listItem = (LinearLayout) v;
    TextView clickedItemView = (TextView) listItem.findViewById(R.id.name);
    String clickedItemString = clickedItemView.getText().toString();
    Log.i("DisplayListCustom", "Click detected " + clickedItemString + ", position " + Integer.toString(position));

This is the solution I went with. I used LinearLayout for my custom layout container, but I assume the same applies to RelativeLayout.

混吃等死 2024-11-16 05:15:46

您需要扩展 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文