8

How do you disable a PreferenceScreen's scrolling so that it would take up the whole space?

Here's the setup.. I have a Fragment which have a ScrollView as its main (root) view, inside is a LinearLayout which contains two Fragments (FrameLayout). The first one is a PreferenceFragment, the other is just a Fragment containing a View and a ListView. I want the PreferenceScreen to take up the whole space so that the whole thing scrolls as one.

Here's the code

<ScrollView
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:weightSum="2"
        android:orientation="vertical">
        <FrameLayout
            android:id="@+id/preferences_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        </FrameLayout>

        <FrameLayout
            android:id="@+id/services_edit_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        </FrameLayout>
    </LinearLayout>
</ScrollView>

Thanks a lot!

Matthew
  • 302
  • 1
  • 2
  • 12

3 Answers3

2

it is too late but for some one who looking for the right answer. in your PreferenceFragmentCompat class just override the onAvtivityCreated method like below.

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getListView().setOverScrollMode(View.OVER_SCROLL_NEVER);
    }
M.J.Ahmadi
  • 3,931
  • 4
  • 17
  • 24
1

I've answered a similar question here. Hope it helps!

In your PreferenceFragment class do as follows:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (getView() != null) {

        ListView listView = (ListView) getView().findViewById(android.R.id.list);
        Adapter adapter = listView.getAdapter();

        if (adapter != null) {
            int height = 0;
            //int height = listView.getPaddingTop() + listView.getPaddingBottom();

            for (int i = 0; i < adapter.getCount(); i++) {
                View item = adapter.getView(i, null, listView);

                item.measure(0, 0);
                height += item.getMeasuredHeight();
            }

            FrameLayout frame = (FrameLayout) getActivity().findViewById(R.id.content_frame);

            ViewGroup.LayoutParams param = frame.getLayoutParams();
            param.height = height + (listView.getDividerHeight() * (adapter.getCount());
            frame.setLayoutParams(param);
        }
    }

}
Community
  • 1
  • 1
goat
  • 307
  • 3
  • 8
1

I was struggling with this problem too. After investigating, I found out that ScrollView does not support this behavior. But just changing it to NestedScrollView solves the problem.

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:layout_marginHorizontal="@dimen/activity_horizontal_margin"
    android:layout_marginVertical="@dimen/activity_vertical_margin"
    >

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

...

        <FrameLayout
            android:id="@+id/preferences_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
...

    </LinearLayout>

</androidx.core.widget.NestedScrollView>
Alec Petrosky
  • 37
  • 1
  • 7