问题
I have a layout
like bellow in my Adapter
:
<?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>
I am visible/gone
RelativeLayout
with this id rlInvisibleLayout
but when I scroll the RecyclerView
I lost it.
Like bellow:
@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);
}
});
....
}
回答1:
Try this
add a boolean
flag in your POJO
/ DataModelClass
like this
public class Pojo
{
boolean isOpen;
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean open) {
isOpen = open;
}
}
Than you have to main that boolean
flag whenever user click cardVisibleLayout
SAMPLE CODE
@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);
}
});
来源:https://stackoverflow.com/questions/51744486/how-to-maintain-visibllity-of-recyclerview-item-in-adapter-class