Android preferenceFragment不显示任何内容,菜单保持选中状态

时间:2015-07-06 18:51:25

标签: android

我正在尝试研究Android的偏好系统。

当我从我的导航器中调用设置片段时,它不显示任何内容并且菜单保持选中状态,如果我选择其他语音也是如此。

这是我的APP启动时的屏幕:

enter image description here

一切都很完美。点击Impostazioni(设置,英文),Home(或" Elenca"项目)reamin check,以及" Impostazioni"

enter image description here

另一个问题是设置框架是空白的(日志中没有错误,但它全部为空白)。

这是存储在res / xml

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


    <PreferenceCategory
        android:title="@string/pref_sms_storage_title"
        android:key="pref_key_storage_settings">
        <CheckBoxPreference
            android:key="pref_sync"
            android:title="@string/pref_sync"
            android:summary="@string/pref_sync_summ"
            android:defaultValue="true" />
    </PreferenceCategory>


</PreferenceScreen>

这是我的设置片段:

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.v4.app.Fragment;

import com.xxxxxx.R;

public class SettingsFragment extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        new InnerSettingsFragment();
    }

    public static class InnerSettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
        }
    }

}

最后这是我的BaseApp,它保留了navdrawer逻辑:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


                //Checking if the item is in checked state or not, if not make it in checked state
                if (menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();


                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {




                    case R.id.home:

                        DashboardFragment dashboardFragment = new DashboardFragment();
                        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, dashboardFragment,"DASHBOARD_FRAGMENT");
                        fragmentTransaction.commit();
                        return true;

                    case R.id.list_event:
                        ListEventFragment fragmentListEvent = new ListEventFragment();
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, fragmentListEvent);
                        fragmentTransaction.commit();
                        return true;

                    case R.id.settings:
                        SettingsFragment fragmentSettings = new SettingsFragment();
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, fragmentSettings);
                        fragmentTransaction.commit();
                        return true;






                    default:

                        return true;

                }
            }
        });

1 个答案:

答案 0 :(得分:0)

你不应该让你的PreferenceFragment 内部课程,将外部课程添加到其位置,并且仍然希望事情像往常一样工作。它不会因为外部 onCreate()本身不会委托给内部 onCreate()。因此,只需将InnerSettingsFragment作为常规顶级课程,您就会开始看到您的偏好设置是从XML资源加载的。

public class SettingsFragment extends PreferenceFragment {

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

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
        }
}