Tony
Tony

Reputation: 1603

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

Upvotes: 0

Views: 1178

Answers (3)

coargiann
coargiann

Reputation: 61

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

Upvotes: 1

Vijay Kumar M
Vijay Kumar M

Reputation: 182

As you are using the two recycleview and there layout height is wrap content instead of use this code or define layout_height in dp:

android:layout_height="250dp"

Upvotes: 0

Tony
Tony

Reputation: 1603

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

Upvotes: 2

Related Questions