Я пытаюсь реализовать RecyclerView
с GridLayoutManager
в своем приложении для Android. Элементы CardView
в пределах RecyclerView
отображаются вертикально, как если бы они находились в 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);
Вы можете добавить скриншот ?? –