2016-09-03 6 views
1

RecyclerViewGridLayoutManagerを使用してグリッドパターンの項目を表示しています。それは良い仕事をしており、グリッド内の2列にアイテムを表示しています。GridLayoutManager(RecyclerView)を使用して最後のグリッドを全幅に設定します。

しかし、アイテムの数が奇数の場合、最後のアイテムは全幅(RecyclerViewの幅と同じ)でなければなりません。

GridLayoutManagerを使用してカスタムデザインを作成する必要がありますか?

助けてください。あなたがここGridLayoutManager

からlayoutManager.setSpanSizeLookupメソッドを使用することができ

答えて

3

は、より良いアプリケーションを構築してください

final GridLayoutManager layoutManager = new GridLayoutManager(context, 2); 
     layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
      @Override 
      public int getSpanSize(int position) { 
       if (mAdapter != null) { 
        switch (mAdapter.getItemViewType(position)) { 
         case 1: 
          return 1; 
         case 0: 
          return 2; //number of columns of the grid 
         default: 
          return -1; 
        } 
       } else { 
        return -1; 
       } 
      } 
     }); 

は今、あなたはあなたのadapter

@Override 
    public int getItemViewType(int position) { 
     return (isLastLogic) ? 0 : 1; // If the item is last, `itemViewType` will be 0 
    } 

viewTypeを決定するために持っていることを使用する方法です!!

+0

これは完璧に動作します、ありがとう –

関連する問題