对于我的android项目,我在一个布局中使用了回收视图,该布局具有10个以上的视图持有者,每个视图持有者都有一个按钮和一个文本。
每当我单击第一项或视图持有者中的按钮时,第八项也会被自动单击。当我单击第二个按钮时,第9个按钮被单击。
因此,我在Google上搜索了此问题,然后发现此问题很少见。 而且我也有这种类型的解决方案,但这部分解决了我的问题。
所以我想知道这是错误还是我的错误?
CaptionedImagesAdapterMenu.java
package com.boredboy.atithikhana;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.facebook.share.Share;
import java.util.Objects;
class CaptionedImagesAdapterMenu extends
RecyclerView.Adapter<CaptionedImagesAdapterMenu.ViewHolder>{
private String[] captions;
private String[] foodName;
private String[] desc;
private String[] price;
private Context context;
private Listener listener;
private Remover remover;
private SparseBooleanArray hidebtns = new SparseBooleanArray();
private SparseBooleanArray setnums = new SparseBooleanArray();
public interface Remover{
void onRemove(int position);
}
public interface Listener{
void onClick(int position);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private CardView cardView;
private Button button;
private ElegantNumberButton numberButton;
public ViewHolder(CardView v) {
super(v);
cardView = v;
this.button = v.findViewById(R.id.addbt);
this.numberButton = v.findViewById(R.id.numberincdec);
}
}
public CaptionedImagesAdapterMenu(Context context,String[] foodName, String[] desc,String[] captions, String[] price){
this.context = context;
this.foodName = foodName;
this.captions = captions;
this.desc = desc;
this.price = price;
}
public void setListener(Listener listener){
this.listener = listener;
}
public void setRemover(Remover remover){
this.remover =remover;
}
@Override
public int getItemCount(){
return captions.length;
}
@Override
public CaptionedImagesAdapterMenu.ViewHolder onCreateViewHolder(
ViewGroup parent, int viewType){
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_captioned_image_menu, parent, false);;
return new ViewHolder(cv);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position){
SharedPreferences preferences = context.getSharedPreferences("NEWV",0);
SharedPreferences.Editor editor = preferences.edit();
SharedPreferences prr = context.getSharedPreferences("ELEV",0);
SharedPreferences.Editor editor1 = prr.edit();
holder.button.setVisibility(hidebtns.get(position,false)?View.GONE:View.VISIBLE);
holder.numberButton.setVisibility(hidebtns.get(position,false)?View.VISIBLE:View.GONE);
String elval = prr.getString("elev","1");
holder.numberButton.setNumber(Objects.requireNonNull(setnums.get(position) ? elval : "1"));
CardView cardView = holder.cardView;
TextView textView = cardView.findViewById(R.id.info_text);
textView.setText(captions[position]);
TextView textView1 = cardView.findViewById(R.id.info_menu);
textView1.setText(desc[position]);
TextView textView2 = cardView.findViewById(R.id.info_price);
textView2.setText("₹ " + price[position]);
TextView textView3 = cardView.findViewById(R.id.food_Info);
textView3.setText(foodName[position]);
holder.button.setOnClickListener(view -> {
holder.button.setVisibility(View.GONE);
holder.numberButton.setVisibility(View.VISIBLE);
holder.numberButton.setNumber("1");
hidebtns.put(position,true);
setnums.put(position,false);
editor.putString("newv", String.valueOf(holder.numberButton.getNumber()));
editor.apply();
if(listener!=null)
{
listener.onClick(position);
}
});
holder.numberButton.setOnValueChangeListener((view, oldValue, newValue) -> {
if (newValue < 1) {
holder.button.setVisibility(View.VISIBLE);
holder.numberButton.setVisibility(View.GONE);
editor1.clear();
editor1.putString("elev", String.valueOf(newValue));
editor1.apply();
hidebtns.put(position,false );
setnums.put(position,true);
if (remover != null)
{
remover.onRemove(position);
}
editor.clear();
editor.apply();
}
if (newValue > 0) {
editor.putString("newv", String.valueOf(newValue));
editor.apply();
editor1.clear();
editor1.putString("elev", String.valueOf(newValue));
editor1.apply();
hidebtns.put(position,true);
setnums.put(position,true);
if(listener!=null)
{
listener.onClick(position);
}
}
});
}
}
答案 0 :(得分:0)
您知道recyclerview回收视图可提供更好的性能。 观看代码时,视口持有人的视线会发生一些变化。
请尝试尝试此解决方案。
在onBindViewHolder下,添加条件,该条件根据您的偏好String.valueOf(holder.numberButton.getNumber())
检查您的偏好数据,并切换holder.button holder.numberButton
视图的可见性。