简体   繁体   中英

How to scroll RecyclerView inside a ViewPager?

I have two tabs inside a ViewPager and each tab contains a RecyclerView which are basically vertical ListViews. My problem is I can't scroll RecyclerViews vertically.

 <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>

Here is 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);
}

It seems my onMeasure method was causing the problem. After deleting it, problem solved.

You can't measure size of recyclerview beforehand. As expected after deleting onMeasure it should work.

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

android:layout_height="250dp"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM