Reputation: 261
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)
Upvotes: 4
Views: 2733
Reputation: 261
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)
Upvotes: 5