Recyclerview inside ViewPager inside scrollview not working
I have an xml layout that has the following views Scrollview-> RelativeLayout-> Some views + Tablayout + ViewPager-> Recylerview (in a ViewPager fragment) . The ViewPager has some fixed height (keeping "Wrap_Content" doesn't display it at all). Now the problem is the Recylerview never scrolls. I have tried several solutions already posted, such as Wraping the viewpager inside "Nested Scrolls " or even creating a LinearLayout child . But nothing worked.
Below is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn_startdraw"
android:fillViewport="true"
android:gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="@dimen/_5sdp">
<TextView
android:id="@+id/invites_accepted"
style="@style/bodyStyleBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/committee_slots"
android:text="@string/invites_accepted" />
<!-- A lot of other Textfields for data display-->
<android.support.design.widget.TabLayout
android:id="@+id/tabs_pendingcommittee"
style="@style/TabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/draw_mode_val"
app:tabMode="fixed"></android.support.design.widget.TabLayout>b
<com.apponative.committeeapp.ui.custom.CustomViewPager
android:id="@+id/pending_member_view"
android:layout_width="match_parent"
android:layout_height="@dimen/_250sdp"
android:layout_below="@+id/tabs_pendingcommittee"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<!-- Some other views on bottom-->
</RelativeLayout>
And the snippet that the Viewpager shows has the following xml layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
style="@style/bodyStyleBold1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No People..." />
<android.support.v7.widget.RecyclerView
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:background="@color/white"></android.support.v7.widget.RecyclerView>
</FrameLayout>
I tried using nestedScrollingEnabled as well as setFixedHeight properties in Recyclerview. But nothing worked.
source to share
I just realized that the problem was not with the RecyclerView inside the ViewPager or ScrollView , but the problem was with the ViewPager inside the ScrollView . When I put the ViewPager inside the ScrollView its Wrap_Content property did n't work, so the fixed height limited the content of the RecyclerView to be fully displayed. So I solved this problem with Setting the ViewPager onMeasure method and it works smoothly. No need to wrap the NestedScroll view or other given solutions.
Below is the code for my CustomView player that wraps the content when added as a child to the ScrollView:
public class CustomViewPager extends ViewPager {
boolean canSwipe = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View child = getChildAt(getCurrentItem());
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void canSwipe(boolean canSwipe) {
this.canSwipe = canSwipe;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.canSwipe) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.canSwipe) {
return super.onInterceptTouchEvent(event);
}
return false;
}
}
source to share
Add NestedScrollView
and then installrecyclerView.setNestedScrollingEnabled(false);
Make sure you already have this android.support.v7.widget.RecyclerView
Remember the RecyclerView item has its own scroller
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Replacing ScrollView with android.support.v4.widget.NestedScrollView
works in all versions.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
style="@style/ScrollBarStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn_startdraw"
android:fillViewport="true"
android:gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="@dimen/_5sdp">
<TextView
android:id="@+id/invites_accepted"
style="@style/bodyStyleBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/committee_slots"
android:text="@string/invites_accepted" />
<!-- A lot of other Textfields for data display-->
<android.support.design.widget.TabLayout
android:id="@+id/tabs_pendingcommittee"
style="@style/TabStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/draw_mode_val"
app:tabMode="fixed"></android.support.design.widget.TabLayout>b
<com.apponative.committeeapp.ui.custom.CustomViewPager
android:id="@+id/pending_member_view"
android:layout_width="match_parent"
android:layout_height="@dimen/_250sdp"
android:layout_below="@+id/tabs_pendingcommittee"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<!-- Some other views on bottom-->
</RelativeLayout>
I hope it works! :) If it doesn't work, let me know! I'll try to find out why it doesn't work :) Have a nice day!
source to share