The Android list contains the problem of disordered state when scrolling with checkboxes

Problem description and analysis

When the list contains checkBox or switch, and the number of items exceeds one page, the selected state of the checkbox is often confused when scrolling, and the reason for this phenomenon is mainly when there are many data items in the listView. Items on the next page will reuse the view that scrolls off the screen when the current screen can contain items

Solution

Create an array to hold the position of the button in the selected state

  • Code example:
public class AddMonitorAdapter extends SimpleCursorAdapter {
  ......
   List< Integer>  mSelectedItemsPositions = new ArrayList<>();//用来保存处于选中状态的按钮的位置

    public AllsitesListAdapter(Context context, int layout, Cursor c,
                               String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        // TODO Auto-generated constructor stub
        mServerMonitor = (ServerMonitorPlus) context.getApplicationContext();
        //用数据库中的数据初始化数组
        setCheckedPosition(c);
    }

    /*
   根据数据库中的站点是否监控的状态将处于监控状态的站点的位置添加到数组中
    */
    public void setCheckedPosition(Cursor c) {
        boolean isMonitoring;
        for (int i = 0; i < c.getCount(); i++) {
            c.moveToPosition(i);
            //获取对应位置上的站点的状态
            isMonitoring = c.getInt(DataBaseAdapter.SiteColumns.IS_MONITORING_INDEX) > 0;
            if (isMonitoring)//如果站点处于监控中
            {
                mSelectedItemsPositions.add(i);//添加到数组中
                //Log.e("AddMonitorAdapter", "setCheckedPosition: add "+ i +" "+c.getPosition());
            }
        }
     }
     @Override
    public void bindView(final View view, Context context, final Cursor cursor) {
        super.bindView(view, context, cursor);
        final CheckBox checkBox = (CheckBox) view.findViewById(R.id.add_site_checkbox);

        //当listView中的数据项较多于当前屏幕所能包含的项时,下一页的数据项将会重用滚出屏幕的视图
        //将cursor的位置作为对应位置上checkBox的标记
        checkBox.setTag(cursor.getPosition());
        //如果数组中含有此位置,说明checkBox处于选中状态,否则,未选中
        if (mSelectedItemsPositions.contains(cursor.getPosition())) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();//获此按钮的附加位置
                int isOpen = 0;//表示按钮的选中状态
                if (checkBox.isChecked())//如果被选中
                {
                    //检查数组中是否含有此位置,如果不含有则将位置添加进去
                    if (!mSelectedItemsPositions.contains(position)) {
                        mSelectedItemsPositions.add(position);
                        isOpen = 1;
                    }
                } else//否则,从数组中移除此位置元素
                {
                    mSelectedItemsPositions.remove((Object) position);
                    isOpen = 0;
                }
                //更新数据库
                mServerMonitor.mDateBaseAdapter
                        .updateSite(
                                siteId,
                                new String[]{DataBaseAdapter.SiteColumns.IS_MONITORING},
                                new int[]{isOpen});
            }
        });

    }

If there is something wrong, please point it out and solve it together.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324724618&siteId=291194637