我有一个RecyclerView,其元素需要显示在4列。我通过GridLayoutManager做到这一点,如下图所示:RecyclerView的GridLayoutManager - 行在较短的屏幕上重叠
mActivityBinding.bwRv.setAdapter(mAdapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(MysActivityTwo.this, 4);
mActivityBinding.bwRv.setLayoutManager(gridLayoutManager);
用我个人的项目布局,如下图所示:
<android.support.v7.widget.CardView 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="@dimen/bw_items_width"
android:layout_height="100dp"
android:layout_alignEnd="@+id/bw_et_questions"
android:layout_alignStart="@+id/bw_et_questions"
android:layout_below="@+id/bw_et_questions"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
app:optCardBackgroundColor="@color/dark_font"
card_view:cardElevation="2dp">
</android.support.v7.widget.CardView>
所以它有固定的高度和宽度。
它在正常大屏幕中正确显示,但在较小的屏幕中,列彼此重叠。有什么方法可以指定:通常在较大的屏幕上应该选择4列,但是在较小的屏幕中,它应该以某种方式包裹并且不重叠?
是的,我知道我可以如xxxhdpi或xxhdpi等使用的文件夹定义,但我要为maxColumns可见一些自动的方式。这是如果列重叠比收到列数。 – Tarun
您可以在定义layoutManager之前检查屏幕宽度,并相应地定义列数。 – fightingCoder