Recycleview滚动问题再次添加元素arraylist并再次向下滚动

时间:2018-01-31 08:45:15

标签: android

我有问题当屏幕上的某些项目开启时添加到一个arraylist并向下滚动然后再向上这个值再添加在数组列表中再发出请作为快速解决方案。

public class NeedSizeAdapter extends RecyclerView.Adapter<NeedSizeAdapter.ViewHolder> {

        private List<SizeModel> sizeModelList;


        public NeedSizeAdapter(List<SizeModel> numbers) {
            this.sizeModelList = new ArrayList<>(numbers);
            selecteSizeList = new ArrayList<>();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_rv_notificationsetting, parent, false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            holder.bindData(sizeModelList.get(position));

            //in some cases, it will prevent unwanted situations
            holder.mSwitch.setOnCheckedChangeListener(null);

            //if true, your checkbox will be selected, else unselected


        holder.mSwitch.setChecked(sizeModelList.get(position).isSelected());
                if (sizeModelList.get(position).isSelected()) {
                    selecteSizeList.add(sizeModelList.get(position).getSizeId());
                    Log.d("selectedSize", sizeModelList.get(position).getSizeId());
                }
                holder.mSwitch.setTag(sizeModelList.get(position));

                holder.mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        Switch cb = (Switch) buttonView;
                        SizeModel contact = (SizeModel) cb.getTag();

                        contact.setSelected(cb.isChecked());
                        sizeModelList.get(position).setSelected(cb.isChecked());
    //                    sizeModelList.get(holder.getAdapterPosition()).setSelected(isChecked);
                        if (isChecked) {
                            selecteSizeList.add(sizeModelList.get(position).getSizeId());
                        } else {
                            selecteSizeList.remove(sizeModelList.get(position).getSizeId());
                        }


                    }
                });
        }

        @Override
        public int getItemCount() {
            return sizeModelList.size();
        }

2 个答案:

答案 0 :(得分:0)

您需要覆盖方法getItemId和getItemViewType

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
   return position;
}

这是在这里得到解答的。 请在此处查看原始答案: Android RecyclerView Duplicate Item When Scrolling

答案 1 :(得分:0)

我认为这是

if (sizeModelList.get(position).isSelected()) {
                selecteSizeList.add(sizeModelList.get(position).getSizeId());
                Log.d("selectedSize", sizeModelList.get(position).getSizeId());
            }  

多余。因为您添加了setOnCheckedChangeListener,因此您可以删除