2016-11-29 62 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后,它应该工作。