Tony Tony - 6 days ago 4
Android Question

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

Answer

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