Home > Blockchain >  I want to show this LinearLayout in only 1 recyclerview at a time
I want to show this LinearLayout in only 1 recyclerview at a time

Time:04-07

I have attached an image of my recyclerview, in my onClick method I show or hide a linearlayout with some image buttons however I want to hide linearlayout when I click on another recyclerview item, basically the linearlayout with the buttons should only show in one item. Can anyone guide me?

below see how it is now, which I do not want to happen.

enter image description here

        //recyclerView single options
        single_item_options = itemView.findViewById(R.id.single_item_options);
        edit_btn = itemView.findViewById(R.id.edit_btn);
        duplicate_btn = itemView.findViewById(R.id.duplicate_btn);
        delete_btn = itemView.findViewById(R.id.delete_btn);

        single_item_options.setVisibility(View.GONE);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (gone_selection) {
                    single_item_options.setVisibility(View.VISIBLE);
                    gone_selection = false;
                } else {
                    single_item_options.setVisibility(View.GONE);
                    gone_selection = true;
                }
            }
        });

CodePudding user response:

Try this approach. Step 1: Get the view position Step 2: Compare the clicked view position if its is the same as the position you get earlier
Step 3: perform the logic based on the positions

CodePudding user response:

You're interested in two ViewHolders (one of both which may not be visible at any given time): the current ViewHolder which shows the the linear layout; and the new ViewHolder which you want to show the linear layout.

Call RecyclerView.findViewHolderForItemId (long id) or RecyclerView.findViewHolderForAdapterPostion() for each of the respective items. Once you have them, call appropiate custom methods on your ViewHolder class to show or hide the linear layout for the bound ViewHolder. If either of the findViewHolderForItemId calls return null, your item isn't visible, so there's no change to be made. Just make sure that you show or hide the LinearLayout appropriately when the focused item does become visible, in your ViewHolder.bind() method.

CodePudding user response:

You can do it with the following steps.

declare one variable like 'visiblePosition' in adapter class and assign the value of position in OnClick

then in onBindViewHolder check the below condition

if(visiblePosition == position){
   //Show You Layout
}else{
   //Hide You Layout
}
  • Related