Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple NestedScrollViews inside ViewPager not scrolling

I have a ViewPager which displays a different fragment on each page. All fragments have a NestedScrollView as their root view. So essentially this:

first_fragment_layout.xml:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="4dp"/>

</android.support.v4.widget.NestedScrollView>

second_fragment_layout.xml:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- Various irrelevant views -->

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

...etc.

The fragments are instantiated via my FragmentPagerAdapter class, as follows:

private class ViewPagerAdapter extends FragmentPagerAdapter
{
    private final List<String> mFragmentNameList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        return Fragment.instantiate(getContext(), mFragmentNameList.get(position));
    }

    @Override
    public int getCount()
    {
        return mFragmentNameList.size();
    }

    void add(String fragmentName)
    {
        mFragmentNameList.add(fragmentName);
    }
}

My problem is that NestedScrollView is only able to scroll on one of the fragments at a time (and generally not the fragment on the current page). There seems to be an issue where only one NestedScrollView can be active at any one time (the one that was inflated first). I've inferred this because the fragment that is able to scroll is always directly beside a fragment that can't scroll, and by default a maximum of two fragments are retained in memory in the ViewPager (i.e. ViewPager.setOffscreenPageLimit(int limit) defaults to 1)

like image 321
Thomas Bradford Avatar asked Oct 19 '25 15:10

Thomas Bradford


1 Answers

Had a 20 minute break and solved it immediately. The ViewPager needs nested scrolling enabled, either via xml android:nestedScrollingEnabled="true" or programatically via ViewCompat.setNestedScrollingEnabled(viewPager, true) (>= API 21 only)

like image 160
Thomas Bradford Avatar answered Oct 21 '25 05:10

Thomas Bradford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!