禁用 PreferenceScreen 的滚动

Mat*_*hew 6 android android-fragments

如何禁用 PreferenceScreen 的滚动以使其占据整个空间?

这是设置.. 我有一个 Fragment,它有一个 ScrollView 作为它的主(根)视图,里面是一个包含两个 Fragment(FrameLayout)的 LinearLayout。第一个是 PreferenceFragment,另一个只是包含一个 View 和一个 ListView 的 Fragment。我希望 PreferenceScreen 占据整个空间,以便整个事物作为一个滚动。

这是代码

<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>
Run Code Online (Sandbox Code Playgroud)

非常感谢!

小智 1

我在这里回答过类似的问题。希望能帮助到你!

在您的PreferenceFragment班级中执行以下操作:

@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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)