2016-03-20 70 views
1

我想創建具有始終相同高度但寬度不同的項目的RecyclerView。使用Horizo​​ntal Recycler View很容易,但我想用Vertical Recycler View來做到這一點。不同寬度的RecyclerView

結果應該是這樣的:

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

我怎樣才能做到這一點?或者,也許有更好的回收者觀點?

回答

1

簡短的回答是LayoutManager。它爲您提供了很多選擇,可以在RecyclerView中進行任何佈局。您所描述的佈局似乎是GridLayoutManager所提供的變體,因此可能從那裏開始。您可以在此question中看到類似問題的解決方案。

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