ListView与CheckBox,EditText,Button结合

来源:互联网 发布:数据迁移方案101data 编辑:程序博客网 时间:2024/06/04 18:07

当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状态丢失混乱情况了。