免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2107 | 回复: 1
打印 上一主题 下一主题

ListView与CheckBox,EditText,Button结合 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-06 21:04 |只看该作者 |倒序浏览
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布局文件是:
  1.        .....       <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"    />        ......
  2. 复制代码
复制代码
接着在ListView的OnListItemClick()实现CheckBox的选中与非选中:
  1. @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     }    ......
  2. 复制代码
复制代码
注:上面获得ListView某一项item里的一个控件还有其它类似的方法:

  1.         RelativeLayout mRelativeLayout = (RelativeLayout) v;          //index为view在父View里的索引index,从0开始计算        checkbox = (CheckBox) mRelativeLayout.getChildAt(index);
  2. 复制代码
复制代码
这样,你就可以得到我们想要的东西了吧。

  这个时候有的人想包存checkbox的状态(虽然我没有用到),那么把这个问题顺便也说下吧:

  一个可行的方法就是加一个hashmap变量,同时也方便了批量操作:
  1.    private HashMap<Integer, Boolean> isSelected;//Batch operations
复制代码
isSelected实际上存储的是listView选中的状态。还是按照上面讲的那个样子,只不过加上了对isSelected操作:
  1. @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     }
  2. 复制代码
复制代码
然后在Adapter里面,对于每次显示View时候调用的方法getView()(或另一种是在bindView()),每次显示一个item时候,通过isSelected检测该item是否被选中。如下:
  1. @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;    }
  2. 复制代码
复制代码
好了,这样就在我滚动ListView的时候,不会出现item的checkbox状态丢失混乱情况了。

论坛徽章:
0
2 [报告]
发表于 2012-01-06 21:25 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP