2016-11-29 99 views
0

我在ViewPager中有兩個選項卡,每個選項卡都包含一個RecyclerView,它們基本上是垂直的ListView。我的問題是我無法垂直滾動RecyclerViews。如何在ViewPager中滾動RecyclerView?

<com.myapplication.MyViewPager 
    android:layout_below="@+id/music_tabs" 
    android:id="@+id/music_switcher" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 



     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler_featured_music" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 


     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler_device_music" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

</com.myapplication.MyViewPager> 

這裏是MyViewPager

public class MyViewPager extends ViewPager { 

public MyViewPager(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

    int height = 0; 
    for(int i = 0; i < getChildCount(); i++) { 
     View child = getChildAt(i); 
     child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
     int h = child.getMeasuredHeight(); 
     if(h > height) height = h; 
    } 

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
+1

發佈您的代碼。 –

+0

看看這個http://stackoverflow.com/questions/35082043/android-scrolling-issues-with-re-cyclerview-inside-a-viewpager –

回答

2

看來我onMeasure方法是造成問題。刪除後,問題解決了。

0

當你正在使用兩個recycleview和佈局有高度的包裹內容,而不是使用此代碼或DP定義layout_height:

android:layout_height="250dp" 
1

你無法衡量recyclerview的大小提前。如預期刪除onMeasure後,它應該工作。