【发布时间】: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_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/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中)
标签: android android-layout android-recyclerview android-adapter android-relativelayout