Android RecyclerView项目在RecyclerView中变得不可见,滚动时有多种项目类型
这是第一次,我的Recyclerview按照我的要求加载,但是当我滚动它时,我的所有项目都变得不可见,请查看附件中我面临的问题的视频,提前谢谢 这是我的Android RecyclerView项目在RecyclerView中变得不可见,滚动时有多种项目类型,android,android-recyclerview,Android,Android Recyclerview,这是第一次,我的Recyclerview按照我的要求加载,但是当我滚动它时,我的所有项目都变得不可见,请查看附件中我面临的问题的视频,提前谢谢 这是我的fragment\u offer.xml看起来像 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout tools:context=".views.fragments.OfferFragment" android:layout_width="match_pa
fragment\u offer.xml
看起来像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
tools:context=".views.fragments.OfferFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.recyclerview.widget.RecyclerView
tools:context=".views.fragments.OfferFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rvOffers"
android:clipToPadding="false">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
这是我的trailer\u offer\u item.xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="140dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:id="@+id/clParent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imgOffer"
style="@style/ImageViewStyle"
android:layout_width="match_parent"
android:layout_height="135dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ImageView>
<ImageView
android:id="@+id/imgGradient"
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="@drawable/image_view_gradient"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</ImageView>
<TextView
android:id="@+id/tvOfferSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_lsmall"
android:layout_marginEnd="@dimen/margin_lsmall"
android:layout_marginBottom="@dimen/margin_xxlsmall"
android:fontFamily="@font/aktiv_regular"
android:text="Dolor sit amet 20%"
android:textColor="@color/white"
android:textSize="@dimen/textsize_lmedium"
app:layout_constraintBottom_toBottomOf="parent"
android:maxLines="1"
android:ellipsize="end">
</TextView>
<TextView
android:id="@+id/tvOfferTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_lsmall"
android:layout_marginEnd="@dimen/margin_lsmall"
android:layout_marginBottom="@dimen/margin_xxlsmall"
android:fontFamily="@font/aktiv_bold"
android:text="Lorem ipsum"
android:textColor="@color/white"
android:textSize="@dimen/textsize_medium"
app:layout_constraintBottom_toTopOf="@id/tvOfferSubtitle">
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
更改
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_sort_search,null);
到
另一个视图也是如此。它应该会起作用 试试这个
@Override
public int getViewTypeCount() {
// return the total number of view types. this value should never change at runtime
return 2;
}
@Override
public int getItemViewType(int position) {
// return a value between 0 and (getViewTypeCount - 1)
return position % 2;
}
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_sort_search,null);
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_sort_search,parent, false);
@Override
public int getViewTypeCount() {
// return the total number of view types. this value should never change at runtime
return 2;
}
@Override
public int getItemViewType(int position) {
// return a value between 0 and (getViewTypeCount - 1)
return position % 2;
}