【问题标题】:Android listview onclicklistener not working with checkboxAndroid listview onclicklistener 不使用复选框
【发布时间】:2012-03-15 10:11:36
【问题描述】:

在我的列表视图中有一个复选框、文本视图和一个图像视图。当我单击一个项目时,onclicklistnere 不起作用。我知道这是因为复选框有自己的侦听器,它覆盖了列表视图 onclicklistener。美好的。当我设置

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

在复选框中,我可以点击列表项,但复选框也变成橙色(就像项目一样)。

所以这是一个半解决方案,有人知道更好的解决方案吗?

这是课程:

    public class ListViewTutorial2Activity extends Activity {

    SimpleAdapter mSchedule;
    ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        list = (ListView) findViewById(R.id.SCHEDULE);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("train", "101");
        map.put("from", "6:30 AM");
        map.put("to", "7:40 AM");
        mylist.add(map);
        map = new HashMap<String, String>();
        map.put("train", "103(x)");
        map.put("from", "6:35 AM");
        map.put("to", "7:45 AM");
        mylist.add(map);
         mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
                    new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
        list.setAdapter(mSchedule);

        list.setOnItemClickListener(new OnItemClickListener() 
        {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
             Object o = list.getItemAtPosition(position);
            }
        });
    }
}

和row.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:descendantFocusability="beforeDescendants">
    </CheckBox>
     <TextView android:id="@+id/TRAIN_CELL"
         android:layout_width="50dip"
         android:layout_height="wrap_content"/>

     <TextView android:id="@+id/FROM_CELL"
         android:layout_width="70dip"
         android:layout_height="wrap_content" android:layout_weight="1"/>

     <TextView android:id="@+id/TO_CELL"
         android:layout_width="60dip"
         android:layout_height="wrap_content"  android:layout_weight="1"/>

            <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/icon" >
    </ImageView>
</LinearLayout>

【问题讨论】:

    标签: android listview checkbox


    【解决方案1】:

    您无需在 CheckBox 上设置任何特殊属性即可执行此操作。但是,在 ListView 中,请尝试以下操作:

    android:clickable="true"
    android:descendantFocusability="beforeDescendants"
    

    如果这不起作用,您可能在其他地方做错了什么。

    【讨论】:

    • 您不应该以这种方式处理 ListViews 和 ExpandableListViews(试图直接访问这些项目)。相反,您应该制作自己的自定义 ListAdapter(例如,扩展 SimpleAdapter)并覆盖它的 .getView() 方法。在您的 .getView() 方法的自定义版本中,您可以在子视图本身中进行本地搜索以获取它的 CheckBox(即 view.findViewById(R.id.itemcheckbox))并将其设置为 OnClickListener。 ListView 对不同的项重复使用 View,因此您必须以这种方式对待 ListView 及其子视图。
    • 我接受您对此解释的回答。谢谢。我终于设法解决了我的问题,但这并不容易。
    【解决方案2】:

    您是否尝试将其添加到您的 xml 布局中的 CheckBox

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

    这对我有用,可能是比你必须经历的更好的解决方案。

    【讨论】:

      【解决方案3】:
      android:focusable="false"
      

      它将解决这种情况下的问题

      【讨论】:

        【解决方案4】:

        我在 RecyclerView 遇到了这个问题,也许这个答案对某人有用。 所有这些答案都没有帮助我。也许这个解决方案不是最优的,但它是:

        <FrameLayout
                    android:id="@+id/like"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/views_holder"
                    android:layout_toEndOf="@+id/btn_comment"
                    android:clickable="true">
        
                    <CheckBox
                        android:id="@+id/cb_like"
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/pressable_size"
                        android:gravity="center_vertical"
                        android:button="@drawable/ic_heart_checkbox"
                        android:paddingStart="8dp"
                        android:paddingEnd="8dp"
                        android:clickable="false"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        tools:text="324"
                        />
        
                </FrameLayout>
        

        我添加了 ViewGroup 包装器(在本例中为 FrameLayout) - 并将点击侦听器设置为 FrameLayout: (以我的 ButterKnife 为例):

        @OnClick(R.id.like)
        void onLikeClicked() {
                cbLike.setChecked(!cbLike.isChecked());
                if (onNewsClickListener != null) {
                    onNewsClickListener.onLikeClicked(getAdapterPosition());
                }
            }
        

        R.id.like - FrameLayout,cbLike(CheckBox)。 它工作正常。

        【讨论】:

          【解决方案5】:

          即使我将它添加到复选框中,我也遇到了同样的问题

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

          然后我意识到我的问题是因为我有一个onStateChangedListener 用于复选框

          【讨论】:

            猜你喜欢
            • 2016-04-20
            • 2016-03-13
            • 1970-01-01
            • 1970-01-01
            • 2011-07-31
            • 2016-02-03
            • 2015-06-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多