我将 StaggeredGridLayoutManager 与 recylerview 一起使用,并在适配器中传递图像列表。当我向下然后向上滚动时,图像项目的顺序会被打乱。我用谷歌搜索并查看了类似的 SO 答案,建议将 setGapStrategy() 设置为 GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS 和 GAP_HANDLING_NONE,我同时使用了两者,但随机播放行为没有任何变化。
下面是我的布局管理器代码:
NewActivitiesAdapter adapter = new NewActivitiesAdapter(this, listImages, optionList);
SpacesItemDecoration decoration = new SpacesItemDecoration(16);
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.addItemDecoration(decoration);
我的 item_image_tile xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/rootView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="@color/pink_bg"
android:clipChildren="false"
android:clipToPadding="false"
android:focusable="true"
android:paddingStart="2dp"
android:paddingEnd="2dp">
<androidx.cardview.widget.CardView
android:id="@+id/cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:background="@android:color/transparent"
android:clipChildren="false"
android:clipToPadding="false"
android:elevation="1dp"
card_view:cardBackgroundColor="@android:color/transparent"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="1dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">
<ImageView
android:id="@+id/iv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cardview"
android:layout_centerHorizontal="true"
android:fontFamily="@font/open_sans"
android:paddingBottom="2dp"
android:text="Image sample title"
android:textColor="@color/black_color"
android:textSize="12sp" />
</RelativeLayout>
先感谢您!