简体   繁体   English

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

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

I made a recycler view to show and choose Instagram posts, in recycler view adapter I handled onclick to show user if items are selected or not and handling sum of chosen posts... but there is problem when insta post in adapter become more than 12 when i scroll in page some chosen items are shown as not chosen, and some posts that are not choosed they look like you choosed them?:!我制作了一个回收站视图来显示和选择 Instagram 帖子,在回收站视图适配器中我处理了 onclick 以向用户显示是否选择了项目并处理所选帖子的总和......但是当适配器中的 insta 帖子超过 12 时出现问题当我在页面中滚动时,一些选择的项目显示为未选择,而一些未选择的帖子看起来像您选择了它们?:! can any one help me ?谁能帮我 ? here is my adapter:这是我的适配器:

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);
        }
    }
}

You need to set selected state on onBindViewHolder every time.每次都需要在 onBindViewHolder 上设置选中的 state。

In 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM