Recyclerview inside ViewPager inside scrollview not working
Asked Answered
P

2

15

I have an xml layout that has following views Scrollview->RelativeLayout->Some Views + Tablayout + ViewPager->Recylerview(In Fragment of ViewPager). ViewPager has some fixed height (Keeping it "Wrap_Content" does not show it at all).Now the issue is that Recylerview never scrolls. I have tried several solutions already posted like Wraping viewpager inside "Nested Scrollview" Or Even Making Child of LinearLayout. 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 Fragment that Viewpager is showing has following layout xml:

<?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 have tried using nestedScrollingEnabled and also setFixedHeight properties on Recyclerview. But n Nothing worked.

Pestilence answered 1/8, 2017 at 14:8 Comment(2)
I think you should remove scroll view from Relative layout as recycler view itself has his own scroller.Sedentary
@RounakLahoti My Layout has a lot of views that need to be scrolled. I am not using srollview for that RecyclerView. Recylerview is also a child in the layout that display another list of data beside other views ( A combination of TextView,ImageView,EditView) .Pestilence
P
22

I just Figured out that problem was not with RecyclerView inside ViewPager or ScrollView, but issue was with ViewPager inside ScrollView. When I put ViewPager inside ScrollView its Wrap_Content Property was not working so A fixed Height restrict RecyclerView content to fully showup. So I resolved this issue by Customizing ViewPager 's onMeasure Method and it working smoothly. No need to wrap in NestedScroll view or other given solutions.

Below is Code for my CustomView Pager that wraps content when added as child to 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;
    }
}
Pestilence answered 2/8, 2017 at 7:52 Comment(3)
I have set the height of custom view pager to wrap_content for default. I think your code will recalculate the height as per child(recyclerview) height. right ?Ventail
@Ventail yes it will recalculate child height and reassign to view pagerPestilence
How to achieve this with viewpager 2?Drye
S
0

Adding NestedScrollView and also then setting recyclerView.setNestedScrollingEnabled(false);

Check that you already have this android.support.v7.widget.RecyclerView

Remember that the RecyclerView element has his 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 on 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 does not work, let me know! i will try to figure out why it is not working :) Have a nice day!

Sloganeer answered 1/8, 2017 at 18:32 Comment(2)
I have used same method as you told. But It does not work at all.Pestilence
I saw your post below of it ! i did something like that but I customized the ImageView! So! Did you find out the answer? @SairaNawazSloganeer

© 2022 - 2024 — McMap. All rights reserved.