1

So what I'm trying to achieve is the following:

I have a list of items in the recycler view (upto) 24 line items in the recycler.

In one list view item there will be a left and right item that a user can click on.

When the user clicks on one of the listviews (left or right item in the listview) the rest of the items should be disabled. Which means out of the 24 items listed the rest of the 23 items should be disabled as well as the list item not selected (if clicked the left item in the list view then the right one is also disabled and vice versa).

public class SheetAdapter extends RecyclerView.Adapter<SheetAdapter.ViewHolder> {

    public SheetAdapter(Context mCtx, List<Items> listList) {

        ...Init all the items here
     
    }

    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mCtx).inflate(R.layout.list_layout, parent,false);

        return new ViewHolder(view);

    }

    public void onBindViewHolder(ViewHolder holder, int position) {

        final List listItem = list.get(position);

        ...(setup the view etc)


        holder.leftImageView.setOnClickListener(v -> {

            // Gray Out all the Left and Right list items not selected

        }

        holder.leftImageView.setOnClickListener(v -> {

            // Gray Out all the Left and Right list items not selected

        }

    }

}

My question is how do i get a reference to the remaining items in the list (left and right) items so that I can disable them!?

UPDATE:

----------------------------------------------------
|            |                       |             |
| Left item 1|                       | Right item 1|
|            |                       |             |
|---------------------------------------------------
|            |                       |             |
| Left item 2|                       | Right item 2|
|            |                       |             |
|---------------------------------------------------
|            |                       |             |
| Left item 3|                       | Right item 3|
|            |                       |             |
|---------------------------------------------------
|                                                  |
|                                                  |

So if a user selects the Right item 2, the behavior I'm looking for would gray out/disable BOTH right and left item 1 as well as BOTH right and left item 3 and last but not least JUST the left item 2

1 Answer 1

0

I am a bit confused as to what you are describing the UI should look like but in your adapter you have your list you passed in List<Items> listList you hold into this list in the class and you then change the data in this list whenever you need to enable/disable stuff.

Once you have all the data in the list that you need changed you then call one of the notifyDataSetChanged methods in the adapter that best suits your needs and that tells the RecyclerView Adapter that it needs to update.

So you change the list dataset and then in your onBindViewHolder you look at the data for that index to enable/disable or just ignore the clicks for the other stuff.

Or another option is to save the last index clicked in your class then call notifyDataSetChanged and if the index in onBind is not matching to the one saved just disabled everything.

EDIT

Going with the assumption that this is just one list with two options for each list item you can setup your adapter like this.

public class SheetAdapter extends RecyclerView.Adapter<SheetAdapter.ViewHolder> {
    private Int selectedIndex = -1;
    private Int selectedSide = -1; //left (1) or right (2) image

    public void onBindViewHolder(ViewHolder holder, int position) {

        final List listItem = list.get(position);

        if(selectedIndex == position){
            if(selectedSide == 1){
                holder.leftImageView.setEnabled(true);
                holder.rightImageView.setEnabled(false);
            }else if(selectedSide == 2){
                holder.leftImageView.setEnabled(false);
                holder.rightImageView.setEnabled(true);
            }else{
                holder.leftImageView.setEnabled(true);
                holder.rightImageView.setEnabled(true);
            }
        }

        holder.leftImageView.setOnClickListener(v -> {
            selectedIndex = position;
            selectedSide = 1;
            notifyDataSetChanged();
        }

        holder.rightImageView.setOnClickListener(v -> {
            selectedIndex = position;
            selectedSide = 2;
            notifyDataSetChanged();
        }
    }
}

This is just an example of what you can do and there is probably other logic you have to handle here too

Sign up to request clarification or add additional context in comments.

5 Comments

but how do i "reference" all the individual items (left and right) in the list view. I need to gray them out and then call the notifydatasetchanged i woudl imagine?
you dont "reference" individual items you use your dataset individual data to adjust any individual item. if you mean how do you reference your dataset from an adapter then you just expose the list from your adapter class instead of making it private
sorry for not using the proper "terms" somewhat new to Android. Could you potentially provide some sample code or a link to an article for the same?
I edited my answer with an small example
The ViewHolder is presentation layer, not data layer, which means these notifyDatasetChanged() call will do nothing, as this List<Items> list (the data) was never modified. One doesn't need any IDs, but has to use adapter-position there... then one can modify one such Item and then let the adapter updaste one single adapter-position. notifyDatasetChanged() would even fit this use-case, because all items would need to be updated anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.