2016-02-19 92 views
0

我有一個名爲DogRecyclerView的RecyclerView。我有另一個名爲FoodRecyclerView的RecyclerView。 FoodRecyclerView恰好是DogRecyclerView的衆多佈局之一(即ViewHolder之一)。 DogRecyclerView是一個列表。 FoodRecyclerView實際上如下。問題在於下面的視圖並沒有包含內容,而是它正在佔用更多的高度。我該如何解決這個問題?嵌套RecyclerView太高

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:background="#f00" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     tools:text="This is a Label that I am testing right now"/> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/food_grid" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

所以,你可以看到我說謊了一點。嵌套視圖實際上是一個包裝RecyclerView和TextView的LinearLayout。 DEEE尾巴......

我留下了一些東西是如此的重要:我LayoutManager是如下:

public class NestedGridLayoutManager extends GridLayoutManager { 

    public NestedGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    public NestedGridLayoutManager(Context context, int spanCount) { 
     super(context, spanCount); 
    } 

    public NestedGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) { 
     super(context, spanCount, orientation, reverseLayout); 
    } 

    private int[] mMeasuredDimension = new int[2]; 

    @Override 
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { 
     final int widthMode = View.MeasureSpec.getMode(widthSpec); 
     final int heightMode = View.MeasureSpec.getMode(heightSpec); 
     final int widthSize = View.MeasureSpec.getSize(widthSpec); 
     final int heightSize = View.MeasureSpec.getSize(heightSpec); 
     int width = 0; 
     int height = 0; 
     for (int i = 0; i < getItemCount(); i++) { 
      measureScrapChild(recycler, i, 
        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), 
        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), 
        mMeasuredDimension); 

      if (getOrientation() == HORIZONTAL) { 
       width = width + mMeasuredDimension[0]; 
       if (i == 0) { 
        height = mMeasuredDimension[1]; 
       } 
      } else { 
       height = height + mMeasuredDimension[1]; 
       if (i == 0) { 
        width = mMeasuredDimension[0]; 
       } 
      } 
     } 
     switch (widthMode) { 
      case View.MeasureSpec.EXACTLY: 
       width = widthSize; 
      case View.MeasureSpec.AT_MOST: 
      case View.MeasureSpec.UNSPECIFIED: 
     } 

     switch (heightMode) { 
      case View.MeasureSpec.EXACTLY: 
       height = heightSize; 
      case View.MeasureSpec.AT_MOST: 
      case View.MeasureSpec.UNSPECIFIED: 
     } 

     setMeasuredDimension(width, height); 
    } 

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, 
            int heightSpec, int[] measuredDimension) { 
     View view = recycler.getViewForPosition(position); 
     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[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin; 
      measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin; 
      recycler.recycleView(view); 
     } 
    } 
} 
+0

就好像LinearLayout希望將實際內容高度增加兩倍。不是,但幾乎。嵌套的RecyclerView也是一個網格(即GridLayoutManager) –

回答

0

支持WRAP_CONTENT和一般的自動測量RecyclerView基於其內容的大小是一個Support Library 23.2.0 release的標題功能。只要您使用的是包含的LayoutManager,您應該可以免費獲得此行爲。