简体   繁体   中英

How to select only one checkbox within 2 recyclerviews

I got 2 recyclerviews on a fragment. Both recyclerviews contain items and checkboxes. Only a single checkbox should be selectable within the two recyclerviews. So if a checkbox is selected, all other checkboxes should be switched off IN BOTH RECYCLERVIEWS etc..

Here is my current code. this code means that only one checkbox can currently be selected in each recyclerview.

My two recylcerviewadapters look like this ( NOTE: both a quiet identical so I'm only posting one of them ):

@Override
public void onBindViewHolder(@NonNull NameViewHolder holder, @SuppressLint("RecyclerView") int position) {
    //getting broadcast from 1st recyclerviewadapter
    LocalBroadcastManager.getInstance(context).registerReceiver(mNameReceiver, new IntentFilter("checkbox_first_adapter"));

    String name = namesArrayList.get(position);

    holder.nameView.setText(name);
    
    holder.checkBox.setVisibility(View.VISIBLE);

    holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            rowIndex = position;
            notifyDataSetChanged();
            //If checkbox is checked, send broadcast to 2nd recyclerviewadapter
            sendCheckBoxBroadCast();
            isClicked = true;
        }
    });


    if (isCheckBoxChecked) {
        holder.checkBox.setChecked(false);
    }


    if (!isClicked) {
        if (selectedName != null) {
            if (name.equals(selectedName.getName())) {
                holder.checkBox.setChecked(true);
                sendCheckBoxBroadCast();
            }
        }
    } else {
        if (rowIndex == position) {
            holder.checkBox.setChecked(true);
        } else {
            holder.checkBox.setChecked(false);
        }
    }

}


public BroadcastReceiver mNameReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        isCheckBoxChecked = intent.getBooleanExtra("isCheckBoxChecked", false);
    }
};

private void sendCheckBoxBroadCast() {
    Intent intent = new Intent("checkbox_second_adapter");
    intent.putExtra("isCheckBoxChecked", true);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

How can I signal the 2nd recyclerview adapter that a checkbox has been selected so that it knows to unselect all checkboxes?

You can go three ways with this:

The first, dirty way is to just pass one adapter to the other and viceversa, and when one has a checkbox event, it callbacks the other one.

The second way would be to implement some sort of interface. You said that both adapters are similar, so it shouldn't be a problem. This interface would have a single method, the callback to update the checkboxes. From the caller activity class, you should be able to manage them both (you could also be able to put them in an array, if you ever need to add more) and get callbacks from either one of the adapters.

The third and perhaps the cleanest way would be to merge the recylcerviews: since a recyclerview can display different kind of views (you just have to fiddle a bit with the onBindViewHolder method) it could be possible (depends on the situation, you haven't provided enough information for this to be possible or entirely ruled out) to use only one adapter class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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