[英]Android RecyclerView and GridLayoutManager layout
I'm trying to implement a RecyclerView
with a GridLayoutManager
in my Android application. 我正在尝试在Android应用程序中使用GridLayoutManager
实现RecyclerView
。 The CardView
elements within the RecyclerView
are displaying vertically as if they were in a LinearLayout
. RecyclerView
中的CardView
元素垂直显示,就像它们在LinearLayout
。 How can I coerce them to fill the grid from left to right, one row at a time? 如何强制他们从左到右每次填充一格?
Here is my RecyclerView
layout code: 这是我的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" />
Here is my CardView
layout code: 这是我的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>
Here is the code to set up the RecyclerView
in the activity's onCreate
method ( CustomAdapter
extends RecyclerView.Adapter
): 这是在活动的onCreate
方法中设置RecyclerView
的代码( 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);
GridLayoutManager (Context context,
int spanCount,
int orientation,
boolean reverseLayout)
Set reverseLayout as true 将reverseLayout设置为true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.