Search code examples
androidandroid-collapsingtoolbarlayoutandroid-nestedscrollviewpreferencefragment

Scrolling PreferenceFragment with CollapsingToolbarLayout


I am trying to build PreferenceFragment in CollapsingToolbarLayout. The problem is that there are too many elements in PreferenceFragment and they are not displayed. It seems like NestedScrollView is not working with these fragments. Solution here doesn't work for me, because I can't use compat library for other reasons. How can I resolve this issue?

Code:

<android.support.v4.widget.NestedScrollView 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fillViewport="true"
 app:layout_behavior="@string/appbar_scrolling_view_behavior"
 tools:showIn="@layout/activity_user_profile">

  <FrameLayout
    android:id="@+id/user_profile_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</android.support.v4.widget.NestedScrollView>

Solution

  • okay after a long time I figured out that PreferenceFragment uses ListView to display its elements which in turn doesn't work inside NestedScrollView. I solved it by overriding onViewCreated method in PreferenceFragment:

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final ListView lv = (ListView) view.findViewById(android.R.id.list);
        if (lv != null)
            ViewCompat.setNestedScrollingEnabled(lv, true);
    }
    

    Hope this helps someone