简体   繁体   中英

Android: PreferenceFragment only showing first preference

I'm having issues with loading the PreferenceFragment in Android. At this point it's a really basic implementation, that I want to expand later on when it works.

The problem is that only the first preference is shown after navigating to the SettingsFragment .

I'm not getting any error, but logcat is showing me this:

W/PathParser: Points are too far apart 4.000000596046461

I googled on this, but without any useful solutions.

The code is the following:

MainActivity navigation through NavigationDrawer

navigate() is a generic function using the FragmentManager

case R.id.nav_settings:
    navigate(new SettingsFragment(), "settingsFragment", false);
    break;

SettingsFragment

public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
    public SettingsFragment() { }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { }
}

prefences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:key="switch_notifications"
        android:title="Notifications"
        android:summary="Receive notifications"
        android:defaultValue="true" >

    </SwitchPreference>

    <CheckBoxPreference
        android:key="switch_notifications2"
        android:title="Notifications"
        android:summary="Receive notifications"
        android:defaultValue="true" >

    </CheckBoxPreference>

</PreferenceScreen>

Screenshots

Left: Actual output | Right: Preview

实际产量 预习

Thanks in advance!

Niels

FOUND THE SOLUTION:

The NestedScrollView which contains my FrameLayout should have the setting android:fillViewport="true" . Also the FrameLayout should have its height set to wrap_content.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="nmct.jaspernielsmichielhein.watchfriends.view.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/frame_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" >

            <FrameLayout
                android:id="@+id/fragment_frame"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" >

            </FrameLayout>

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

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

Sorry, this might not be an answer (not able to comment yet), more like an insight. Just want to point out that it works as expected on my device (oneplus3) and also in the nexus 5 emulator seems to be showing correctly.

My guess: could it be that somehow the parent activity is limiting the output of the fragment? For example if I set a high enough paddingBottom on the parent, I get a similar result as in the picture on the left.

If not, it's probably some weird device specific bug. Maybe you could give more info about the device, screen size, android version etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM