0
我想在我的Android應用程序中實現RecyclerView
和GridLayoutManager
。 RecyclerView
中的CardView
元素垂直顯示,就好像它們在LinearLayout
中一樣。我怎麼能強迫他們從左到右填充網格,一次一行?Android RecyclerView和GridLayoutManager佈局
這裏是我的RecyclerView
佈局代碼:
<android.support.v7.widget.RecyclerView
android:id="@+id/RECYCLER_VIEW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/item_offset" />
這裏是我的CardView
佈局代碼:
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
card_view:cardUseCompatPadding="true"
android:layout_width="120dp"
android:layout_height="120dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentBottom="true"
android:textAlignment="center"
android:textSize="18dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
這裏是成立RecyclerView
在活動的onCreate
方法的代碼(CustomAdapter
延伸RecyclerView.Adapter
):
// set up adapter
adapter = new CustomAdapter(getApplicationContext(), ApplicationState.getGridElements());
view.setAdapter(adapter);
// grid layout manager with 2 columns
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
view.setLayoutManager(layoutManager);
// custom decoration for equal spacing
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getApplicationContext(), R.dimen.item_offset);
view.addItemDecoration(itemDecoration);
view.setHasFixedSize(true);
你可以添加屏幕截圖? –