Android 如何在ViewPager中滚动RecyclerView?

Android 如何在ViewPager中滚动RecyclerView?,android,android-viewpager,android-recyclerview,Android,Android Viewpager,Android Recyclerview,我在一个ViewPager中有两个选项卡,每个选项卡都包含一个基本上是垂直列表视图的RecyclerView。我的问题是我不能垂直滚动RecyclerViews <com.myapplication.MyViewPager android:layout_below="@+id/music_tabs" android:id="@+id/music_switcher" android:layout_width="match_parent" android:la

我在一个ViewPager中有两个选项卡,每个选项卡都包含一个基本上是垂直列表视图的RecyclerView。我的问题是我不能垂直滚动RecyclerViews

 <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);
}
公共类MyViewPager扩展了ViewPager{
公共MyViewPager(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
整数高度=0;
对于(int i=0;i高度)高度=h;
}
heightMeasureSpec=MeasureSpec.MakeMasureSpec(高度,精确测量);
超级测量(宽度测量、高度测量);
}

似乎是我的onMeasure方法导致了问题。删除它后,问题得到了解决。

似乎是我的onMeasure方法导致了问题。删除它后,问题得到了解决。

当您使用两个recycleview时,布局高度是包装内容,而不是使用此代码或在dp中定义布局高度:

android:layout_height="250dp"

由于您使用的是两个recycleview,布局高度为包裹内容,而不是使用此代码或在dp中定义布局高度:

android:layout_height="250dp"

您不能预先测量recyclerview的大小。在测量时删除后,它会正常工作。

您不能预先测量recyclerview的大小。在测量时删除后,它会正常工作。

发布您的代码。查看此发布您的代码。查看此发布