7

私は中央項目水平RecyclerView(GridLayoutManagerを使用)で

RecyclerViewに行ごとに水平にすべての私の項目を中心にしようとしている私は多くのことを試してみました:RelativeLayoutは親として、そして、layout_centerInParentを使用する基本的なアンドロイドを使用します。 layout_gravity = "センター" は、重みを使用して、いくつかの空間要素を追加しようと.... item_row.xml
に、私は成功しませんでした:(

を私が持っているもの

a busy cat

私が欲しいの活動に

a busy cat

recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); 

item_row.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:id="@+id/cardView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
card_view:cardCornerRadius="2dp" 
card_view:cardElevation="4dp" 
card_view:cardUseCompatPadding="true"> 

<!-- Recycler View Item Row --> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 


    <ImageView 
     android:id="@+id/imgThumbnail" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:adjustViewBounds="true" 
     android:background="@android:color/black" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 


     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="item name" 
      android:id="@+id/txtName" 
      android:layout_margin="5dp" 
      android:singleLine="false" 
      android:lines="2" 
      android:gravity="center" /> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:id="@+id/txtCategory" 
       android:textStyle="italic" 
       android:textColor="#496fa9" 
       android:text="subtitle" 
       android:layout_weight="1" 
       android:gravity="center_vertical" 
       android:paddingLeft="10dp" 
       android:layout_gravity="center" /> 

      <ImageButton 
       android:id="@+id/imageButton" 
       android:layout_width="48dp" 
       android:layout_height="wrap_content" 
       android:background="@android:color/black" 
       android:adjustViewBounds="true" 
       android:scaleType="fitCenter" 
       android:padding="8dp"/> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

</android.support.v7.widget.CardView> 

私は多くのグーグルでこれを見つけましたanswerしかし、実際に私は項目の各行のLinearLayoutを作成する方法を知らない!

  • が最適ですか?

  • 他のアイデアですか?

+0

非常に似たようなものを目指して、この作品を制作しましたか? – hibob

+0

@hibob no、以下の回答のコメントをご覧ください –

+0

@AliSherafat解決策は見つかりましたか? –

答えて

2

SpanSizeLookupを使用して、グリッドビューの幅が異なるビュー/アイテムを作成できます。それで、それに応じて調整するために、空のビューでいくつかの巧みさを行うことができます。あなたのレイアウトが実際にどのように変わるかによって、より複雑になるかもしれません。

参照:あなたはあなたのようなものだろう、あなたの図面に表示され、この特定のケースのためのhttps://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.SpanSizeLookup.html

:あなたは、アダプタに確認する必要があります

GridLayoutManager lm = new GridLayoutManager(context, 6); 
lm.setSpanSizeLookup(new MySizeLookup()); 

public static class MySizeLookup extends SpanSizeLookup { 

    public int getSpanSize(int position) { 
     if(position >= 0 && position <= 2) { 
      return 2; 
     } else if (position == 3 || position == 6) { 
      return 1; 
     } else { 
      return 2; 
     } 
    } 
} 

とそれとを、位置3及び6は空の/目に見えない要素になります。その余分なスペースを占有するためにnew View(context);のようなもの

+0

あなたの返事に感謝しますが、ちょっと混乱しています –

+0

次に私はあなたがドキュメントを読んでいくつかのテストをすることを提案しました – Budius

+0

私はたくさんのテストを行い、あなたのコードを試しましたが成功しませんでした。 3個未満のアイテムを持つ行と 'LinearLayout'に' android:layout_gravity = "center"属性を持つ行のアイテムを入れてください**しかし、どうすればいいのか分かりません。 –

2

gridLayoutManager = new GridLayoutManager(context,6)を使用し、setSpanSizeLookupを上書きします。

例:

int totalSize=list.size(); 
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
        @Override 
        public int getSpanSize(int position) { 
         int span; 
         span = totalSize % 3; 
         if (totalSize < 3) { 
          return 6; 
         } else if (span == 0 || (position <= ((totalSize - 1) - span))) { 
          return 2; 
         } else if (span == 1) { 
          return 6; 
         } else { 
          return 3; 
         } 
        } 
       }); 

あなたが行の3つの項目を表示したいとグリッドの中央に残っている場合、これは動作します。

必要に応じて、グリッドレイアウトマネージャのスパン数を変更します。

+0

こんにちは@Rahul Devanavar、これを4つのアイテムで連続して行う方法 –

関連する問題