1

I have a recyclerview with linear layout manager and it contains different items. one of the items have a button that hides or display the nested layout of this items.

So I added something like 10 rows of the specific type, and when I click on specific item, it's nested layout is displayed, but I see that another item display it's own nested layout. Second try was to click on some button and scrolling, the nested layout was shown on a different items.

As you can see from the code, I tried to call the method - notifyDataSetChanged(), but it doesn't help. I know that there is expanded recycler view, but In my case I want to solve it without change the whole code..

The layout:

<RelativeLayout 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
>
    <TextView 
        android:id = "@+id/tv"
        android:layout_height="wrap_content"
        android:layout_width = "wrap_content"
        android:text = "click on the button"
    />
    <Button 
        android:id = "@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:layout_toRightOf="@+id/tv"
    />

    <RelativeLayout
        android:id = "@+id/nested"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv"
        android:visibility="gone"
    >

        <TextView 
           android:id = "@+id/tv1"
           android:layout_height="wrap_content"
           android:layout_width = "wrap_content"
           android:text = "click on the button"
       />   
    </RelativeLayout>
</RelativeLayout>

The code in the adapter (onBind)

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position)
    {
        int type = getItemViewType(position);
        ViewType viewType = ViewType.values()[type];
        try
        {
            switch (viewType)
            {
                case VIEW1:
                  // code for view 1
                break;
                case VIEW2:

                    final ViewHolder viewHolder = (ViewHolder) holder;

                    viewHolder.tv.setText("TV1");
                    viewHolder.btn.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {

                            viewHolder.nestedLayout.setVisibility(!isVisible ? View.VISIBLE : View.GONE );
                            viewHolder.isVisible = !viewHolder.isVisible;

                            // notifyDataSetChanged();
                        }
                    });


                break;

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            Log.d("ChatListAdapter","chat list adapter ioexception" + e.getMessage());
        }
    }
Elior
  • 3,178
  • 6
  • 37
  • 67

3 Answers3

2

As pointed out by Samuel Robert, you need to track the expanded items visibility in your model class :

Say you are using a model class item, have a field inside it to track the visibility of nested layout.

Try this out :

Item item = itemList.get(position);
final ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.tv.setText("TV1");
viewHolder.nestedLayout.setVisibility(!item.isVisible ? View.VISIBLE : View.GONE );
viewHolder.btn.setOnClickListener(new View.OnClickListener()
    {
       @Override
       public void onClick(View v)
       {
        int position = getAdapterPosition();
        if( position != RecyclerView.NO_POSITION ) {
          Item item = itemList.get(position)
          item.isVisible = !item.isVisible;
          notifyItemChanged(position);
        }
       }
     });
Alok Omkar
  • 628
  • 1
  • 9
  • 18
1

Have id field in the datamodel and override getItemId() like below,

@Override
    public long getItemId(int position) {
        return yourList.get(position).getId();
    }
0

You have to set the visibility state for each view (each call to onBindViewHolder). I guess what happens for you is that you set the visibility for a view that is later recycled and have the visibility set.

MaTePe
  • 936
  • 1
  • 6
  • 11
  • for each nested layout, the default value of the visibility is "gone". So if the user will click on the 2nd item button, the visibility value of this specific nested layout will be "visible", and all the others will stay "gone" – Elior Oct 13 '16 at 10:51
  • Maybe I didn't get the problem then. If you have multiple view showing you either a problem with the viewholder having values set or that you have the same viewId, I guess the latter isn't possible. I would suggest you try to print the values of each view as debug. – MaTePe Oct 13 '16 at 11:08