Android展开式列表视图滚动它更改选中的复选框状态

时间:2017-05-06 10:50:36

标签: android checkbox expandablelistview

我在组父级别使用可扩展列表视图我有复选框

  @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            View view = convertView;
            ViewHolder holder = new ViewHolder();
            if(view == null){
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             //   convertView = infalInflater.inflate(R.layout.home_activity_lv_sync_adapter, null);
                view = infalInflater.inflate(R.layout.home_activity_lv_sync_adapter, null);

                holder.ckupdate = (CheckBox) view.findViewById(R.id.ck_update);

                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.ckupdate.setTag(groupPosition);
            holder.ckupdate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    int position = (Integer) buttonView.getTag();
                   // objects.get(position).setChecked(buttonView.isChecked());
                }
            });
           // holder.ckupdate.setChecked(isChecked(position));

            return view;
        }

但是,当我滚动exp列表时,它会更改状态(我不会从任何列表或数据库中获取数据,只需用户检查并取消选中)

提前致谢

2 个答案:

答案 0 :(得分:2)

对于这个答案可能为时已晚,但我认为这是一个更简单的解决方案。只需尝试每次创建新视图(在此之前删除检查view == null)。

而不是:

View view = convertView;
ViewHolder holder = new ViewHolder();
if(view == null){
    LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = infalInflater.inflate(R.layout.home_activity_lv_sync_adapter, null);
    holder.ckupdate = (CheckBox) view.findViewById(R.id.ck_update);
    view.setTag(holder);
} else {
    holder = (RecyclerView.ViewHolder) view.getTag();
}

试试这个:

View view;
ViewHolder holder = new ViewHolder();
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view =infalInflater.inflate(R.layout.home_activity_lv_sync_adapter,null);
holder.ckupdate =(CheckBox)view.findViewById(R.id.ck_update);
view.setTag(holder);

此外,不需要为此父视图使用holder。您可以改用convertView。

答案 1 :(得分:0)

您好我通过以下

解决了我的问题
 holder.ckupdate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    int position = (Integer) buttonView.getTag();

                    if (isChecked) {
                        selectedStrings.add(position + " ");
                        ii.add(position);
                    } else {
                        selectedStrings.remove(position + "");
//                        ii.remove(position);
                        ii.removeAll(Arrays.asList(position));
                    }
                    System.out.println("selectedStrings!!!! " + selectedStrings);
                    for(int i=0;i<ii.size();i++)
                    {
                        System.out.println("HERE !! "+ii.get(i));
                    }
                    // objects.get(position).setChecked(buttonView.isChecked());
                }
            });