【问题标题】:ListView with clickable/editable widget带有可点击/可编辑小部件的 ListView
【发布时间】:2011-01-07 02:24:14
【问题描述】:

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

【问题讨论】:

标签: android android-widget


【解决方案1】:

您可能想看看this issue。在 ListView 的一行中有一个可聚焦的项目会导致 OnItemClickListener 不被调用。但是,这并不意味着您不能连续拥有可聚焦/可点击的项目,有一些解决方法,例如 this one

此外,您还可以查看通话记录屏幕。它有一个带有可点击项目的ListView(右侧的呼叫图标)。 See Source code here

【讨论】:

  • 快速帮助:“解决此问题的一种简单方法是在添加列表视图视图时调用“setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);”。您将能够选择行,单击行并单击子复选框和按钮。” - 这是上面答案中的两个链接引用的页面中评论 #27 的引用。非常感谢!!
  • 只有第一个链接有效,其他链接坏了,请修复
  • 另外,请确保不要在列表行中执行android:clickable="true" 之类的操作。那是我的问题。
【解决方案2】:

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

实际上,您可以将其添加到布局 XML(如果增加一个):android:descendantFocusability="blocksDescendants"。

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

【讨论】:

    【解决方案3】:

    如果列表的任何行项包含可聚焦或可点击的视图,则 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>
    

    【讨论】:

      【解决方案4】:

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

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

      <CheckBox
          android:id="@+id/fav_check_box"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:focusable="false" />
      

      【讨论】:

      【解决方案5】:

      两个最佳解决方案

      1. android:descendantFocusability="beforeDescendants" 添加到列表视图 在 xml
      2. 将给定的两个属性设置为false

      喜欢

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

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

      【讨论】:

        【解决方案6】:

        我以不同的方式解决了我的问题,在我的项目中我有多个 LinearLayout 因此,如果您在适配器类中为您的线性布局和 setOnclickListener 提供 id ,它将起作用,只有触摸的原始效果会消失。 但是这个链接Making a LinearLayout act like an Button对于让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;
            }
        

        【讨论】:

          猜你喜欢
          • 2015-11-24
          • 1970-01-01
          • 1970-01-01
          • 2011-10-02
          • 1970-01-01
          • 1970-01-01
          • 2017-02-10
          • 2015-06-02
          • 1970-01-01
          相关资源
          最近更新 更多