46

当 Items 布局具有可点击/可编辑小部件(RadioButton、EditText 或 CheckBox)时,是否可以在 ListView 上使用 OnItemClickListener?

4

6 回答 6

69

您可能想看看这个问题。在 a 的一行中有一个可聚焦的项目ListView会导致OnItemClickListenerNOT 被调用。但是,这并不意味着您不能连续拥有可聚焦/可点击的项目,有一些像这样的解决方法。

此外,您可以查看通话记录屏幕。它有一个ListView可点击的项目(右侧的呼叫图标)。 在此处查看源代码

于 2010-01-20T03:31:04.620 回答
21

在 Samuh 提到的链接中引用评论 #31(它为我解决了这个问题):

事实上,你可以将它添加到布局 XML 中(如果增加了一个):android:descendantFocusability="blocksDescendants"。

在此处添加 JIC,该网页将来会关闭。

于 2012-09-03T03:27:23.883 回答
12

如果列表的任何行项包含可聚焦或可点击的视图,OnItemClickListener则将不起作用。

行项目必须具有类似的参数android:descendantFocusability="blocksDescendants"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical" >

    // your other widgets here

</LinearLayout>
于 2013-02-22T06:20:10.950 回答
3

尝试了许多复杂的解决方案,但这是最简单的解决方案:

只需使用android:focusable="false"如下:

<CheckBox
    android:id="@+id/fav_check_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false" />
于 2013-10-11T10:04:12.853 回答
0

两个最佳解决方案

  1. 添加android:descendantFocusability="beforeDescendants"到 xml 中的 listView
  2. 将给定的两个属性设置为false

喜欢

   android:focusable="false"
   android:focusableInTouchMode="false"

然后它将处理 listView 行项子(按钮,EditText 等)事件,而不是 listView.setOnItemClick 。

于 2015-01-12T09:02:35.973 回答
0

我以不同的方式解决了我的问题,在我的项目中,我有多个 LinearLayout,因此如果您在适配器类中为您的 linearayout 和 setOnclickListener 提供 id 它将起作用,只有触摸的原始效果会消失。但是此链接使 LinearLayout 像按钮一样 操作对于使 linearlaout 在单击时像按钮一样有用

物品

<?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:layout_marginTop="10dp">

    <TextView
        android:id="@+id/txt_item_followers_name"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center|start"
        android:paddingLeft="15dp"
        android:text="Ali"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/txt_item_followers_name"
        android:layout_marginLeft="10dp"
        android:src="@drawable/puan_icon" />

    <TextView
        android:id="@+id/txt_item_followers_mark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView"
        android:layout_toEndOf="@+id/imageView"
        android:background="@color/red_400"
        android:paddingLeft="10dp"
        android:text="25.5"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <LinearLayout
        android:id="@+id/linear_one"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/txt_item_followers_name"
        android:background="@color/red_400"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/btn_item_followers_2b_follow"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_marginLeft="10dp"
            android:src="@drawable/follow_buton" />
    </LinearLayout>


</RelativeLayout>

在getView方法里面

 @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        View view = convertView;
        if (convertView == null)
            view = inflater.inflate(R.layout.deneme, null);

        final Followers2 myObj = myList.get(position);
        LinearLayout linear_one = (LinearLayout) view.findViewById(R.id.linear_one); // HERE WE DOMUNİCATE IT
        linear_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(parentActivity, "One Two", Toast.LENGTH_SHORT).show();
            }
        });

        TextView name = (TextView) view.findViewById(R.id.txt_item_followers_name);
        TextView mark = (TextView) view.findViewById(R.id.txt_item_followers_mark);
        final ImageView btn_follow = (ImageView) view.findViewById(R.id.btn_item_followers_2b_follow);
        name.setText(myObj.getName());
        mark.setText(myObj.getScore());
       /* if (myObj.isFollow() == true) {
            btn_follow.setImageResource(R.drawable.following_buton);
        } else {
            btn_follow.setImageResource(R.drawable.follow_buton);
        }

        btn_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Followers2 myObj = myList.get(position);
                if (myObj.isFollow() == true) {
                    btn_follow.setImageResource(R.drawable.following_buton);
                } else {
                    btn_follow.setImageResource(R.drawable.follow_buton);
                }
            }
        });*/

        return view;
    }
于 2016-07-21T12:53:41.723 回答