gpt4 book ai didi

java - 如何解决recyclerview中按钮自动点击的问题?这是一个错误吗?

转载 作者:行者123 更新时间:2023-12-02 05:16:05 27 4
gpt4 key购买 nike

对于我的 Android 项目,我在一个布局中使用了回收 View ,该布局有 10 多个 View 持有者,每个 View 持有者都有一个按钮和一个文本。

每当我单击第一个项目或 View 持有者中的按钮时,也会自动单击第 8 个项目。当我单击第二个按钮时,将单击第 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);
}
}
});


}
}



最佳答案

如您所知,recyclerview 回收 View 以获得更好的性能。在观看代码时,您的 View 持有者 View 中的可见性会发生一些变化。

尝试一下这个解决方案。在 onBindViewHolder 下添加条件,使用相应的 String.valueOf(holder.numberButton.getNumber()) 检查您的首选项中的数据,并切换 holder.buttonholder.numberButton 的可见性查看。

关于java - 如何解决recyclerview中按钮自动点击的问题?这是一个错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56291680/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com