2016-03-20 8 views
1

常に高さは同じですが幅の異なるアイテムでRecyclerViewを作成したいと思います。 Horizo​​ntal Recycler Viewではかなり簡単ですが、私はVertical Recycler Viewでそれをしたいと思います。幅の異なるRecyclerView

結果は次のようになります。

___________________________________________ 
|           | 
|[................] [......] [........] | 
|[......................] [..............] | 
|[.....] [.................]    | 
|[......................] [......]   | 
|           | 
------------------------------------------- 

どのように私はそれを達成することができますか?あるいは、リサイクラーの視点よりも優れたものがありますか?

答えて

1

短い答えはLayoutManagerです。これは、あなたがRecyclerViewで必要なレイアウトを実行するための多くのオプションを提供します。あなたが記述しているレイアウトは、GridLayoutManagerが提供しているものの変形と思われるので、おそらくそこから始めるでしょう。このquestionの類似した問題の解決策を見ることができます。ドロンヤコブレフ-Golaniによって提案されたアプローチを使用して

1

、私はこのクラスでそれをやった:

public class LessonsLayoutManager extends StaggeredGridLayoutManager { 

    private Point mMeasuredDimension = new Point(); 

    public LessonsLayoutManager() { 
     super(2, HORIZONTAL); 
    } 

    @Override 
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, 
          int widthSpec, int heightSpec) { 

     final int widthSize = View.MeasureSpec.getSize(widthSpec) - (getPaddingRight() + getPaddingLeft()); 

     int width = 0; 
     int height = 0; 
     int row = 0; 

     for (int i = 0; i < getItemCount(); i++) { 

      if (!measureScrapChild(recycler, i, 
       View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), 
       View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), 
       mMeasuredDimension)) continue; 

      if (width + mMeasuredDimension.x > widthSize || mMeasuredDimension.x > widthSize) { 
       row++; 
       width = mMeasuredDimension.x; 
      } else { 
       width += mMeasuredDimension.x; 
      } 

      height += mMeasuredDimension.y; 
     } 

     setSpanCount(row); 
     setMeasuredDimension(View.MeasureSpec.getSize(widthSpec), height); 
    } 

    @Override 
    public boolean canScrollHorizontally() { 
     return false; 
    } 

    @Override 
    public boolean canScrollVertically() { 
     return false; 
    } 

    private boolean measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, Point measuredDimension) { 

     View view = null; 
     try { 
      view = recycler.getViewForPosition(position); 
     } catch (Exception ex) { 
      // try - catch is needed since support library version 24 
     } 

     if (view != null) { 

      RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); 

      int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, 
       getPaddingLeft() + getPaddingRight(), p.width); 
      int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, 
       getPaddingTop() + getPaddingBottom(), p.height); 

      view.measure(childWidthSpec, childHeightSpec); 

      measuredDimension.set(
       view.getMeasuredWidth() + p.leftMargin + p.rightMargin, 
       view.getMeasuredHeight() + p.bottomMargin + p.topMargin 
      ); 

      recycler.recycleView(view); 

      return true; 
     } 

     return false; 
    } 

} 

結果は次のようになります。

enter image description here

関連する問題