ListView與CheckBox,EditText,Button結合


  這段時間沒有寫多少博客,主要是天氣冷,加上沒有網更是不想動。這段時間的知識點快忘記了,趕緊把它復習下。

  今天說的比較簡單:那就是當ListView與CheckBox,EditText,Button結合時候,onListItemClick()事件無法響應,找了下,所是與list本身的獲得焦點的優先級低於象CheckBox,EditText,Button。所以設置一下他們的這個屬性:android:focusable="false"。另外希望不響應它們自己的onClick事件,那就這樣:android:clickable="false"。那這個時候怎么像CheckBox選中與不選中呢?有一個途徑是在某個條件下依托ListView的OnListItemClick()事件。舉個簡單的例子吧:

  例如我的ListView的item布局文件是:

  

       .....
<CheckBox
android:id="@+id/selected"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center"
android:layout_alignParentRight
="true"
android:layout_marginRight
="4dip"
android:layout_alignWithParentIfMissing
="true"
android:focusable
="false"
android:clickable
="false"
android:visibility
="gone"
/>
......

  接着在ListView的OnListItemClick()實現CheckBox的選中與非選中:

 @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(isPick){
CheckBox checkbox = (CheckBox) v.findViewById(R.id.selected);
checkbox.setChecked(!checkbox.isChecked());
return;
}
// other code
}
......

  注:上面獲得ListView某一項item里的一個控件還有其它類似的方法:

  

        RelativeLayout mRelativeLayout = (RelativeLayout) v;  
//index為view在父View里的索引index,從0開始計算
checkbox = (CheckBox) mRelativeLayout.getChildAt(index);

   或者你還可以選這這樣做:

    if (isSelected.containsKey(position)) {
isSelected.remove(position);
    //刷新當前的item,然后在getview函數里面檢測isSelected來判斷checkbox是什么狀態
mAdapter.notifyDataSetChanged();
} else {
isSelected.put(position, true);
mAdapter.notifyDataSetChanged();
}

  

  好了這樣,你就可以得到我們想要的東西了吧。

  這個時候有的人想包存checkbox的狀態(雖然我沒有用到),那么把這個問題順便也說下吧:

  一個可行的方法就是加一個hashmap變量,同時也方便了批量操作:

       private HashMap<Integer, Boolean> isSelected;//Batch operations

  isSelected實際上存儲的是listView選中的狀態。還是按照上面講的那個樣子,只不過加上了對isSelected操作:

 @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if (isPick) {
CheckBox checkbox = (CheckBox) v.findViewById(R.id.selected);
if (isSelected.containsKey(position)) {
isSelected.remove(position);
checkbox.setChecked(false);
} else {
isSelected.put(position, true);
checkbox.setChecked(true);
}
return;
}
// other code
}

  然后在Adapter里面,對於每次顯示View時候調用的方法getView()(或另一種是在bindView()),每次顯示一個item時候,通過isSelected檢測該item是否被選中。如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("adapter getview count", position+""
);
ViewHolder holder ;
if(convertView==null){
LayoutInflater inflate = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflate.inflate(R.layout.list_item , null);
holder = new ViewHolder();
holder.icon = (ImageView)convertView.findViewById(R.id.note_icon);
holder.name = (TextView)convertView.findViewById(R.id.note_name);
holder.date = (TextView)convertView.findViewById(R.id.note_date);
holder.checkBox = (CheckBox)convertView.findViewById(R.id.selected);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}

holder.icon.setImageResource(R.drawable.icon);
holder.name.setText(mData.get(position));
holder.date.setText("日期");
if(mIsSelected.containsKey(position)){
holder.checkBox.setChecked(true);
}else{
holder.checkBox.setChecked(false);
}
return convertView;
}

static class ViewHolder{
ImageView icon;
TextView name;
TextView date;
CheckBox checkBox;
}

  好了,這樣就在我滾動ListView的時候,不會出現item的checkbox狀態丟失混亂情況了。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM