Android 具有可单击/可编辑小部件的ListView

Android 具有可单击/可编辑小部件的ListView,android,android-widget,Android,Android Widget,当项目布局具有可单击/可编辑的小部件单选按钮、编辑文本或复选框时,是否可以在ListView上使用OnItemClickListener 您可能想看看。在ListView的行中有一个可聚焦项会导致不调用Listener。但是,这并不意味着您不能在一行中有可聚焦/可单击的项目,有一些解决方法,如 此外,您还可以查看通话记录屏幕。它有一个列表视图,右侧有一个调用图标,上面有可单击的项目。 引用Samuh提到的链接中的第31条意见,为我解决了这个问题: 事实上,如果增加一个:android:gende

当项目布局具有可单击/可编辑的小部件单选按钮、编辑文本或复选框时,是否可以在ListView上使用OnItemClickListener

您可能想看看。在ListView的行中有一个可聚焦项会导致不调用Listener。但是,这并不意味着您不能在一行中有可聚焦/可单击的项目,有一些解决方法,如

此外,您还可以查看通话记录屏幕。它有一个列表视图,右侧有一个调用图标,上面有可单击的项目。

引用Samuh提到的链接中的第31条意见,为我解决了这个问题:

事实上,如果增加一个:android:genderantfocusability=blocksDescendants,则可以将其添加到布局XML中


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

如果列表中的任何行项目包含可聚焦或可单击的视图,则OnItemClickListener将无法工作

行项目必须具有类似android:DegenantFocusability=BlocksDescents的参数


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

只需使用android:focusable=false,如下所示:

两个最佳解决方案

将android:genderantfocusability=before子体添加到listView 以xml或 将给定的两个属性设置为false 像


然后它将处理listView行项目childButton、EditText等事件,而不是listView.setOnItemClick。

我修复了我的问题,不同的是,在我的项目中,我有多个LinearLayout 所以,若在适配器类中将id赋予LinearyOut和setOnclickListener,它将工作,只有触摸的原始效果将消失。 但此链接可用于使linearlaout像单击按钮一样工作

项目


扩展ListActivity而不是Activity。参考快速帮助:解决此问题的一个简单方法是调用SetDegenantFocusabilityFocus\u BLOCK\u子体;在添加listView视图时在其上显示。您可以选择行,单击行,然后单击子复选框和按钮。-这是对上面答案中两个链接所指页面中评论27的引用。非常感谢!!只有第一个链接有效,其他链接已断开请修复。此外,请确保不要在列表行中执行类似android:clickable=true的操作。这就是我的问题。这里更详细地解释了这种技术:
<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>
<CheckBox
    android:id="@+id/fav_check_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false" />
   android:focusable="false"
   android:focusableInTouchMode="false"
<?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>
 @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;
    }