如何维护适配器类中recyclerview项目的可见性

Posted

技术标签:

【中文标题】如何维护适配器类中recyclerview项目的可见性【英文标题】:how to maintain Visibllity of recyclerview item in adapter class 【发布时间】:2019-01-15 14:08:06 【问题描述】:

我的Adapter 中有一个layout,如下所示:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_
        android:layout_
        android:background="#FFFFFF">

        <android.support.v7.widget.CardView
            android:id="@+id/cardVisibleLayout"
            android:layout_
            android:layout_
            android:layout_below="@+id/txtCategoryNameTop"
            card_view:cardElevation="2dp">

            <LinearLayout
                android:id="@+id/lnVisibleLayout"
                android:layout_
                android:layout_
                android:orientation="horizontal">

                <com.joanzapata.iconify.widget.IconTextView
                    android:id="@+id/itxtArrow"
                    android:layout_
                    android:layout_
                    android:layout_weight="1"
                    android:background="@null"
                    android:gravity="center"
                    android:text="@string/fa_chevron_down"
                    android:textColor="@color/orangePeel" />
                <TextView
                    android:id="@+id/txtConnectToTeacher"
                    android:layout_
                    android:layout_
                    android:layout_gravity="center"
                    android:layout_weight="1.3"
                    android:gravity="center"
                    android:text="@string/contact_teacher" />
                <RelativeLayout
                    android:layout_
                    android:layout_
                    android:layout_gravity="right"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="3.7"
                    android:gravity="right"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/txtCourseName"
                        android:layout_
                        android:layout_
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="10dp" />

                    <TextView
                        android:id="@+id/txtTeacherName"
                        android:layout_
                        android:layout_
                        android:layout_alignParentRight="true"
                        android:layout_below="@+id/txtCourseName"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="10dp" />
                </RelativeLayout>
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <RelativeLayout
            android:id="@+id/rlInvisibleLayout"
            android:layout_
            android:layout_
            android:layout_below="@+id/cardVisibleLayout"
            android:animateLayoutChanges="true"
            android:background="#E0E0E0"
            android:visibility="gone">

            <RelativeLayout
                android:id="@+id/rlLessonLink"
                android:layout_
                android:layout_
                android:layout_alignParentRight="true"
                android:layout_marginRight="15dp">

                <ImageView
                    android:id="@+id/imgLessonLink"
                    android:layout_
                    android:layout_
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    app:srcCompat="@drawable/ic_circle" />

                <TextView
                    android:id="@+id/txtLessonLink"
                    android:layout_
                    android:layout_
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/imgLessonLink"
                    android:text="@string/lesson_link"
                    android:textColor="@color/black" />
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>

我是visible/goneRelativeLayout,这个ID是rlInvisibleLayout,但是当我滚动RecyclerView时我把它弄丢了。

如下:

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int position) 
.....
        viewHolder.cardVisibleLayout.setOnClickListener(view -> 
            if (viewHolder.rlInvisibleLayout.getVisibility() == View.VISIBLE) 
                viewHolder.rlInvisibleLayout.setVisibility(View.GONE);
                viewHolder.iconTextView.setText(R.string.fa_chevron_down);
             else 
                viewHolder.rlInvisibleLayout.setVisibility(View.VISIBLE);
                viewHolder.iconTextView.setText(R.string.fa_chevron_up);
            
        );
....

【问题讨论】:

这是因为RecyclerView被称为RecyclerView?问了很多次 记住位置状态并在onBindViewHolder中恢复(不在OnClickListener中) 【参考方案1】:

试试这个

像这样在POJO / DataModelClass 中添加boolean 标志

public class Pojo
               
    boolean isOpen;    

    public boolean isOpen() 
        return isOpen;
    

    public void setOpen(boolean open) 
        isOpen = open;
    

当用户点击cardVisibleLayout 时,你必须维护boolean 标志

示例代码

@Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) 

        // check here the flag and maintain visibility of item based on flag
        if (arrayList.get(position).isOpen())
            viewHolder.rlInvisibleLayout.setVisibility(View.VISIBLE);
        else 
            viewHolder.rlInvisibleLayout.setVisibility(View.GONE);

        

        viewHolder.cardVisibleLayout.setOnClickListener(view -> 

            if (viewHolder.rlInvisibleLayout.getVisibility() == View.VISIBLE) 
                viewHolder.rlInvisibleLayout.setVisibility(View.GONE);
                viewHolder.iconTextView.setText(R.string.fa_chevron_down);
                arrayList.get(position).setOpen(false);// set flag false when you hide the item
             else 
                arrayList.get(position).setOpen(true);// set flag true when you show the item
                viewHolder.rlInvisibleLayout.setVisibility(View.VISIBLE);
                viewHolder.iconTextView.setText(R.string.fa_chevron_up);
            
        );

【讨论】:

以上是关于如何维护适配器类中recyclerview项目的可见性的主要内容,如果未能解决你的问题,请参考以下文章

从片段类中的对话框添加项目到recyclerview。

Recyclerview 项目在适配器中应用条件时未在 cardview 中显示数据

在 recyclerview 适配器类中显示自定义弹出窗口 - Android Java

如何从recyclerview适配器获取选定项目的数量?

为啥在recyclerview android中滚动后突出显示的项目丢失

如果为空,如何隐藏 Firestore RecyclerView