gpt4 book ai didi

java - 如何将 onBindViewHolder 内部的项目存储到数组

转载 作者:行者123 更新时间:2023-12-01 17:50:08 27 4
gpt4 key购买 nike

我在recycview中有很多按钮我只是想将它们添加到数组中,然后让它们添加一些工作。

  • 首先我定义了数组名称“buttons”ToggleButton 按钮[] = new ToggleButton[5];

  • 然后我将值放入其中按钮[位置]=holder.playBtn;

  • 现在我想按其中一个更改所有按钮的所有背景所以我使用了这个函数:关闭所有(),但我没有成功
// My class
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {

public RecyclerViewClickListener mListener;
private MyListData[] listdata;
private Context context;
private ToggleButton [] listBtnPlyStop = null;

ToggleButton buttons[] = new ToggleButton[5];

public MyListAdapter(Context context ,MyListData[] listdata , RecyclerViewClickListener mListener) {
this.mListener =mListener;
this.listdata = listdata;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.row_list_azcar, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final MyListData myListData = listdata[position];
holder.textView.setText(listdata[position].getDescription());
buttons[position]=holder.playBtn;
holder.playBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
mListener.onClick(compoundButton,position);
setBgBtn(compoundButton,checked,position);
}
});
}

private void setBgBtn(CompoundButton compoundButton , boolean checked,int id) {
if(checked){
compoundButton.setBackground(ContextCompat.getDrawable(context, R.drawable.btnpause));
}else{
compoundButton.setBackground(ContextCompat.getDrawable(context, R.drawable.btnplay));
}
closeAll();
}

private void closeAll(){
for(int j=0; j<buttons.length-1;j++) {
buttons[j].setBackgroundResource(R.drawable.btnpause);
}
}

@Override
public int getItemCount() {
return listdata.length;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView;
public LinearLayout linearLayout;
public ToggleButton toggleButton,playBtn;
public RadioButton radioButton1,radioButton2,radioButton3;
private RecyclerViewClickListener mListener;

public ViewHolder(View itemView){
super(itemView);
//this.itemView = itemView;
this.toggleButton =(ToggleButton) itemView.findViewById(R.id.bt_check2);
this.playBtn =(ToggleButton) itemView.findViewById(R.id.bt_play);
this.radioButton1 = (RadioButton)itemView.findViewById(R.id.radio_btn1);
this.radioButton2= (RadioButton)itemView.findViewById(R.id.radio_btn2);
this.radioButton3= (RadioButton)itemView.findViewById(R.id.radio_btn3);
this.textView = (TextView) itemView.findViewById(R.id._txt_kind_of_azkar);
linearLayout = (LinearLayout)itemView.findViewById(R.id.l_containe_row);
}
}
}

最佳答案

我建议创建一个 ToggleButtons 的动态数组列表,如下所示:

        private ArrayList<ToggleButton> buttons;

public MyListAdapter(Context context ,MyListData[] listdata , RecyclerViewClickListener mListener) {
this.mListener =mListener;
this.listdata = listdata;
this.context = context;
this.buttons = new ArrayList<>();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.row_list_azcar, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final MyListData myListData = listdata[position];
holder.textView.setText(listdata[position].getDescription());
buttons.add(holder.playBtn);

...
private void closeAll(){
for(int j=0; j<buttons.size(); j++) {
buttons.get(j).setBackgroundResource(R.drawable.btnpause);
}
}

关于java - 如何将 onBindViewHolder 内部的项目存储到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60811189/

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