【问题标题】:ListView listener not intercepting events from itemListView 侦听器不拦截来自项目的事件
【发布时间】:2013-07-19 06:18:16
【问题描述】:

我有一个ListView

<ListView
        android:id="@+id/sensorList"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        android:layout_below="@+id/chooseHint"
        android:choiceMode="multipleChoice" >
</ListView>

其中有CheckedTextViews:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:layout_margin="5dp"
    style="@android:style/TextAppearance.Medium"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple" />

我添加了一个OnItemClickListener 以在单击列表项时执行某些操作。而且当一个项目被点击时,复选框也会被切换。我怎样才能实现监听器正在捕获点击事件,但复选框没有?

【问题讨论】:

    标签: android listview android-listview onitemclicklistener checkedtextview


    【解决方案1】:

    要防止CheckedTextView 切换,请执行以下操作:
    1.子类CheckedTextView.
    2. 覆盖setChecked(boolean) 方法。
    3. 将您的CheckedTextView 替换为已覆盖的@。

    步骤 1 和 2 的代码:

    package com.example.multichoicelist;
    public class UnresponsiveCheckedTextView extends CheckedTextView {
    
        public UnresponsiveCheckedTextView(Context context) {
                this(context, null);
        }
    
        public UnresponsiveCheckedTextView(Context context, AttributeSet attrs) {
             this(context, attrs, 0);
        }
    
        public UnresponsiveCheckedTextView(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void setChecked(boolean checked) {
                  //Do nothing.
        }
    
    }  
    

    setChecked方法刷新了CheckedTextView的选中状态,带来了切换效果。覆盖setChecked 将防止CheckedTextView 的复选框在用户单击列表项时被切换。

    第 3 步的 XML:

    <com.example.multichoicelist.UnresponsiveCheckedTextView  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/textView"
        style="@android:style/TextAppearance.Medium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
     />  
    

    下面的活动包含一个多选 ListView 和一个 OnItemClickListener,显示已单击的列表项。请注意,在此类单击事件期间,复选框 未切换(感谢覆盖的 CheckedTextView 实现):

    package com.example.multichoicelist;
    public class MainActivity extends ListActivity implements OnItemClickListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ListView mListView = getListView();
            mListView.setOnItemClickListener(this);
            mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            mListView.setAdapter(ArrayAdapter.createFromResource(this,
                    R.array.phonetic_alphabet, R.layout.list_item));
        }
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View childView,
                int adapterPosition, long arg3) {
            Toast.makeText(this,
                    ((CheckedTextView) childView).getText() + " selected",
                    Toast.LENGTH_SHORT).show();
        }
    
    }  
    

    下图显示了当单击名为“Charlie”的列表项时,多选 ListView 的行为:

    【讨论】:

    • 谢谢,非常好的回答!
    猜你喜欢
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 2018-02-16
    相关资源
    最近更新 更多