简体   繁体   English

如何将项目从 onBindViewHolder 内部存储到数组

[英]How to store items from inside onBindViewHolder to array

I have a number of buttons inside recycview I just wanted to add them in array and then i get them to add to them some jobs.我在 recycview 中有许多按钮,我只想将它们添加到数组中,然后让它们添加一些作业。

  • At first i defined array name "buttons" ToggleButton buttons[] = new ToggleButton[5];起初我定义了数组名称“buttons” ToggleButton buttons[] = new ToggleButton[5];

  • Then i put values in it buttons[position]=holder.playBtn;然后我将值放入其中 button[position]=holder.playBtn;

  • Now I want to press one of them change all the backgrounds of all buttons And so i used this function : closeAll() ,but I didn't succeeding现在我想按其中一个改变所有按钮的所有背景所以我使用了这个函数:closeAll(),但我没有成功
// 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);
        }
    }
}

I would suggest creating a dynamic array list of ToggleButtons, like so:我建议创建一个 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);
            }
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM