2

攔截事件我有一個ListViewListView的聽衆不是從項目

<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> 

其中有CheckedTextView,它出現:單擊列表項時

<?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做一些事情。但是,當一個項目被點擊時,該複選框被切換。我如何實現監聽器捕獲點擊事件,但複選框不是?

回答

3

爲了防止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的複選框在用戶單擊列表項時切換。

XML用於步驟3:

<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" 
/> 

下面是含有選擇題ListViewOnItemClickListener表示已單擊列表項目的活性。請注意,這樣的單擊事件的複選框不反轉(感謝覆蓋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(); 
    } 

} 

下圖顯示的選擇題ListView的行爲時,將名爲「查理」的列表項點擊:

enter image description here

+0

謝謝,非常好的答案! – WonderCsabo