失去了在Android RecyclerView中向下滑动或向上滑动时选择的项目。

huangapple go评论82阅读模式
英文:

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

问题

我创建了一个回收视图来显示和选择Instagram帖子,在回收视图适配器中,我处理了点击事件,以向用户显示哪些项目被选择,以及处理已选择帖子的总数...但是当适配器中的Instagram帖子超过12个时,当我在页面上滚动时,一些已选择的项目显示为未选择,而一些未选择的帖子看起来像您已选择它们!有人可以帮助我吗?
以下是我的适配器:

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

希望这可以帮助您解决问题!

英文:

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 !!! 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&lt;InstaPostsRecyclerViewHolders&gt; {
private List&lt;InstaPosts&gt; instaList;
private List&lt;InstaPosts&gt; selectedPosts;
LinearLayout linearLayout;
private Context context;
TextView numberTextView,eachPostPriceTxt,totalPriceTxt;
BuyBasketFlow buyBasketFlow;
SellingItems sellingItems;
public InstaPostsRecyclerViewAdapter(Context context, List&lt;InstaPosts&gt; itemList,List&lt;InstaPosts&gt; 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 &lt; 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(&quot;Log3&quot;, &quot; item number: &quot; + position + &quot; is clicked&quot;);
instaList.get(position).setSelected(!instaList.get(position).isSelected());
if(buyBasketFlow.getType().compareTo(&quot;like&quot;)==0) {
if (instaList.get(position).isCanBeSelect()) {
selectingPhase(holder,position);
} else {
Toast.makeText(context, R.string.just_select_photo, Toast.LENGTH_SHORT).show();
Log.i(&quot;Log1&quot;, instaList.get(position).getTypename());
}
}else if(buyBasketFlow.getType().compareTo(&quot;view&quot;)==0){
if (instaList.get(position).isViewCanBeSelected()) {
selectingPhase(holder,position);
} else {
Toast.makeText(context, R.string.just_select_video, Toast.LENGTH_SHORT).show();
Log.i(&quot;Log1&quot;, instaList.get(position).getTypename());
}
}else if(buyBasketFlow.getType().compareTo(&quot;comment&quot;)==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(&quot;Log1&quot;, instaList.get(position).getTypename());
}
}
}
});
Picasso.get().load(instaList.get(position).getThumbnail_url()).into(holder.instaImage);
}
public List&lt;InstaPosts&gt; 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(&quot;Log1&quot;, selected.toString());
}
holder.radioBtn.setImageDrawable(context.getDrawable(R.drawable.radio_button_checked));
holder.cardView.setStrokeWidth(10);
if (Build.VERSION.SDK_INT &gt;= 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(&quot;Log1&quot;, selected.toString());
}
holder.cardView.setStrokeWidth(0);
holder.radioBtn.setImageDrawable(context.getDrawable(R.drawable.radio_button_unchecked));
}
if (selectedPosts.size() &lt; 1) {
linearLayout.setVisibility(View.GONE);
Log.i(&quot;Log1&quot;, &quot;layout must become invisible&quot;);
} else {
Log.i(&quot;Log1&quot;, &quot;layout must become visible&quot;);
linearLayout.setVisibility(View.VISIBLE);
sellingItems = buyBasketFlow.getSellingItem();
numberTextView.setText(&quot;&quot; + selectedPosts.size());
Log.i(&quot;Log1&quot;, &quot;&quot; + Integer.parseInt(sellingItems.getPrice()));
String text = &quot;قیمت هر پست&quot; + sellingItems.getPrice() + &quot; تومان &quot;;
String sum = Integer.toString((selectedPosts.size() * Integer.parseInt(sellingItems.getPrice())));
text = text + &quot;و قیمت مجموع &quot; + sum + &quot; تومان &quot;;
eachPostPriceTxt.setText(text);
}
}
}

答案1

得分: 1

你需要在每次调用 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));

英文:

You need to set selected state on onBindViewHolder every time.

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

huangapple
  • 本文由 发表于 2020年8月13日 22:22:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定