Android RecyclerView - scrolling changes items

这一生的挚爱 提交于 2020-01-05 05:34:06

问题


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());
        }
    }

回答1:


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);
        }
       }
     });



回答2:


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

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



回答3:


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.



来源:https://stackoverflow.com/questions/40018690/android-recyclerview-scrolling-changes-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!