向下或向上滑动时在 android 回收站视图中丢失了所选项目

lost chosen items in android recycler view when swiping down or top

我制作了一个回收站视图来显示和选择 Instagram posts,在回收站视图适配器中,我处理了 onclick 以显示用户是否选择了项目,并处理了所选 posts 的总和。 .. 但是当我在页面中滚动时适配器中的 insta post 变得超过 12 时出现问题,一些选择的项目显示为未选择,而一些未选择的 posts 看起来像你选择的他们 !!!谁能帮我 ? 这是我的适配器:

package com.example.pinstagram.adaptor;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.pinstagram.R;
import com.example.pinstagram.classes.BuyBasketFlow;
import com.example.pinstagram.classes.InstaPosts;
import com.example.pinstagram.classes.SellingItems;
import com.example.pinstagram.viewHolders.InstaPostsRecyclerViewHolders;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


public class InstaPostsRecyclerViewAdapter extends RecyclerView.Adapter<InstaPostsRecyclerViewHolders> {
    private List<InstaPosts> instaList;
    private List<InstaPosts> selectedPosts;
    LinearLayout linearLayout;
    private Context context;
    TextView numberTextView,eachPostPriceTxt,totalPriceTxt;
    BuyBasketFlow buyBasketFlow;
    SellingItems sellingItems;

    public InstaPostsRecyclerViewAdapter(Context context, List<InstaPosts> itemList,List<InstaPosts> selectedPosts,LinearLayout linearLayout,TextView[] textViews) {
        this.instaList = itemList;
        this.context = context;
        this.selectedPosts = selectedPosts;
        this.linearLayout = linearLayout;
        numberTextView = textViews[0];
        eachPostPriceTxt = textViews [1];
        buyBasketFlow = new BuyBasketFlow();
        sellingItems = new SellingItems();
        selectedPosts.clear();
        for(int i = 0; i < instaList.size();i++){
            instaList.get(i).setSelected(false);
        }
        linearLayout.setVisibility(View.GONE);
    }

    @NonNull
    @Override
    public InstaPostsRecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.isnta_posts_listview, null);
        InstaPostsRecyclerViewHolders crcv = new InstaPostsRecyclerViewHolders(layoutView);
        return crcv;
    }
    @Override
    public void onBindViewHolder(@NonNull final InstaPostsRecyclerViewHolders holder, final int position) {
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("Log3", " item number: " + position + " is clicked");
                instaList.get(position).setSelected(!instaList.get(position).isSelected());
                if(buyBasketFlow.getType().compareTo("like")==0) {
                    if (instaList.get(position).isCanBeSelect()) {
                        selectingPhase(holder,position);

                    } else {
                        Toast.makeText(context, R.string.just_select_photo, Toast.LENGTH_SHORT).show();
                        Log.i("Log1", instaList.get(position).getTypename());
                    }
                }else if(buyBasketFlow.getType().compareTo("view")==0){
                    if (instaList.get(position).isViewCanBeSelected()) {
                        selectingPhase(holder,position);

                    } else {
                        Toast.makeText(context, R.string.just_select_video, Toast.LENGTH_SHORT).show();
                        Log.i("Log1", instaList.get(position).getTypename());
                    }

                }else if(buyBasketFlow.getType().compareTo("comment")==0){
                    if (instaList.get(position).isViewCanBeSelected()||instaList.get(position).isCanBeSelect()) {
                        selectingPhase(holder,position);

                    } else {
                        Toast.makeText(context, R.string.just_select_video, Toast.LENGTH_SHORT).show();
                        Log.i("Log1", instaList.get(position).getTypename());
                    }
                }
            }
        });

        Picasso.get().load(instaList.get(position).getThumbnail_url()).into(holder.instaImage);
    }
    public List<InstaPosts> getSelectedPosts(){
        return selectedPosts;
    }

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

    private void selectingPhase(InstaPostsRecyclerViewHolders holder,int position){
        if (instaList.get(position).isSelected()) {
            selectedPosts.add(instaList.get(position));
            buyBasketFlow.setPostsArrays(selectedPosts);
            for (InstaPosts selected : selectedPosts) {
                Log.e("Log1", selected.toString());
            }
            holder.radioBtn.setImageDrawable(context.getDrawable(R.drawable.radio_button_checked));
            holder.cardView.setStrokeWidth(10);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                holder.cardView.setStrokeColor(context.getColor(R.color.colorPrimary));
            }
        } else {
            selectedPosts.remove(instaList.get(position));
            buyBasketFlow.setPostsArrays(selectedPosts);
            for (InstaPosts selected : selectedPosts) {
                Log.e("Log1", selected.toString());
            }
            holder.cardView.setStrokeWidth(0);
            holder.radioBtn.setImageDrawable(context.getDrawable(R.drawable.radio_button_unchecked));
        }
        if (selectedPosts.size() < 1) {
            linearLayout.setVisibility(View.GONE);
            Log.i("Log1", "layout must become invisible");

        } else {
            Log.i("Log1", "layout must become visible");
            linearLayout.setVisibility(View.VISIBLE);
            sellingItems = buyBasketFlow.getSellingItem();
            numberTextView.setText("" + selectedPosts.size());
            Log.i("Log1", "" + Integer.parseInt(sellingItems.getPrice()));
            String text = "قیمت هر پست" + sellingItems.getPrice() + " تومان ";
            String sum = Integer.toString((selectedPosts.size() * Integer.parseInt(sellingItems.getPrice())));
            text = text + "و قیمت مجموع " + sum + " تومان ";
            eachPostPriceTxt.setText(text);
        }
    }
}

每次都需要在onBindViewHolder上设置选中状态。

在 onBindViewHolder 中:

InstaPosts instaPosts = instaList.get(position);
holder.cardView.setStrokeWidth(instaPosts.isSelected() ? 10 : 0);
holder.radioBtn.setImageDrawable(context.getDrawable(instaPosts.isSelected() ? R.drawable.radio_button_checked : R.drawable.radio_button_unchecked));