0

我想在我的Android應用程序中實現RecyclerViewGridLayoutManagerRecyclerView中的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); 
+0

你可以添加屏幕截圖? –

回答

0
GridLayoutManager (Context context, 
       int spanCount, 
       int orientation, 
       boolean reverseLayout) 

設置reverseLayout爲真正

相關問題