英文:
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<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);
}
}
}
答案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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论