首页 > 代码库 > 解决 listview的item超过屏幕显示出现滚动的全选问题

解决 listview的item超过屏幕显示出现滚动的全选问题

1、listview的getcount与getchildcount,getcount>=getchildcount.getcount为所有的listdata的size值,getchildcount获得的是显示的值;

                    当itemsize<=显示屏的显示数量,getcount()=getchildcount(),

                   当itemsize >显示屏的显示数量,getcount> getchildcount;

2、问题原因:

ListView+CheckBox数据超过一屏时数据乱序的原因主要是重新加载了contentview。当第一次加载convertView为null,而再有新的View就会发现convertView不为空,所以新显示的View其实使用了之前某个View的对象。这就造成了状态可能混乱。比如你的listview可以显示7个item,第一行的checkbox点选时,第8行的也同时会被点选。
解决方案:当选择Checkbox的时候,记下其状态,然后在getView方法中进行设置 。
关键代码:
 1 List<Integer> checkPosition = new ArrayList<Integer>();//记住状态 2  3 //重写getview() 4  5  6 @Override 7     public View getView(final int position, View convertView, ViewGroup parent) { 8         // TODO Auto-generated method stub 9         View rowView = convertView;10         ViewHolder holder = null;11         if (rowView == null) {12             holder = new ViewHolder();13             rowView = mInflater.inflate(R.layout.item_autoresult_list, null);14             holder.cb_auto_check = (CheckBox) rowView15                     .findViewById(R.id.cb_auto_check);16             17             rowView.setTag(holder);18         } else {19             holder = (ViewHolder) rowView.getTag();20         }21         holder.cb_auto_check.setText(mList.get(position).getName());22         23         24         holder.cb_auto_check.setTag(new Integer(position));25         26 27         if (checkPosition != null) {28             holder.cb_auto_check.setChecked((checkPosition29                     .contains(new Integer(position)) ? true : false));30         } else {31             holder.cb_auto_check.setChecked(false);32         }33 34         final CheckBox finalholder = holder.cb_auto_check;35         holder.cb_auto_check36                 .setOnCheckedChangeListener(new OnCheckedChangeListener() {37 38                     @Override39                     public void onCheckedChanged(CompoundButton buttonView,40                             boolean isChecked) {41                         // TODO Auto-generated method stub42                         System.out.println("finalholder.getTag()"43                                 + finalholder.getTag());44                         if (isChecked) {45                             if (!checkPosition.contains(finalholder.getTag())) {46                                 System.out.println("true+contains"47                                         + finalholder.getTag());48                                 checkPosition.add((Integer) finalholder49                                         .getTag());50                             }51                         } else {52                             if (checkPosition.contains(finalholder.getTag())) {53                                 System.out.println("fasle+contains"54                                         + finalholder.getTag());55                                 checkPosition.remove(finalholder.getTag());56                             }57                         }58                     }59                 });60         return rowView;61     }
OnCheckedChangeListener change = new OnCheckedChangeListener() {        @Override        public void onCheckedChanged(CompoundButton buttonView,                boolean isChecked) {            // TODO Auto-generated method stub            if(isChecked){                for(int i=0;i<billall.size();i++){                    LinearLayout layout = (LinearLayout)listView.getChildAt(i);                    ImageView checkView = (ImageView)layout.findViewById(R.id.check);                    checkView.setImageResource(R.drawable.orders_sign);                }            }            else{                for(int i=0;i<billall.size();i++){                    LinearLayout layout = (LinearLayout)listView.getChildAt(i);                    ImageView checkView = (ImageView)layout.findViewById(R.id.check);                    checkView.setImageResource(R.drawable.orders_choose);                }            }        }    };