gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:42:19 25 4
gpt4 key购买 nike

我的适配器中有一个布局:

<?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_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">

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

<LinearLayout
android:id="@+id/lnVisibleLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<com.joanzapata.iconify.widget.IconTextView
android:id="@+id/itxtArrow"
android:layout_width="0dp"
android:layout_height="match_parent"
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_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1.3"
android:gravity="center"
android:text="@string/contact_teacher" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />

<TextView
android:id="@+id/txtTeacherName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/cardVisibleLayout"
android:animateLayoutChanges="true"
android:background="#E0E0E0"
android:visibility="gone">

<RelativeLayout
android:id="@+id/rlLessonLink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp">

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

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

visible/gone RelativeLayout 使用此 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);
}
});
....
}

最佳答案

试试这个

像这样在您的 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);
}
});

关于android - 如何在适配器类中维护 recyclerview 项目的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51744486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com