我有一個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