zoukankan      html  css  js  c++  java
  • 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状态丢失混乱情况了。


  • 相关阅读:
    c++继承与多态
    逻辑运算符
    页面分栏布局
    js中的三大特殊数据:undefined,null,NaN
    逻辑分支中if小括号中的隐式转换
    最小高度的兼容方法
    js数据类型的转换
    ognl.OgnlException: target is null for setProperty(null, "goodsSize", [Ljava.lang.String;@c3bb5。
    解决拦截器的对于参数传递无效问题
    引用外部js乱码问题
  • 原文地址:https://www.cnblogs.com/slider/p/2314647.html
Copyright © 2011-2022 走看看