内部PreferenceScreen无法使用PreferenceFragmentCompat打开 [英] Inner PreferenceScreen does not open with PreferenceFragmentCompat
问题描述
我的PreferenceFragmentCompat内部PreferenceScreen没有显示,或者似乎忽略了点击事件.
My inner PreferenceScreen of PreferenceFragmentCompat is not showing, or seems to ignore tapping events.
我创建了extends PreferenceFragmentCompat
public class MyPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
}
}
然后我在styles.xml
更改了主题,例如
then I changed my theme at styles.xml
like
<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
最后创建我的preferences.xml
文件,如
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:title="Check Me"/>
<PreferenceScreen android:title="My Screen"> <!-- This is not opening -->
<EditTextPreference android:title="Edit text" />
</PreferenceScreen>
</PreferenceScreen>
在build.gradle
中,我同时添加了两者:
At the build.gradle
I have added both:
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:preference-v7:23.0.1'
活动代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:name="com.mando.preferenceapp.MyPreferenceFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
测试以上代码,我无法打开/进入首选项屏幕.我想念什么吗?为什么这不起作用?
Testing the above code I cannot open / get into the preference screen. Am I missing something? Why this isn't working?
推荐答案
花了很多时间进行尝试之后,搜索并感谢支持库创建者的帮助.我设法使其正常工作.
After spending many many hours with tries, searching and thankfully with some assistance from the creators of the support library. I've managed to make it work.
第1步. Activity
public class MyActivity extends AppCompatActivity implements
PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
// Create the fragment only when the activity is created for the first time.
// ie. not after orientation changes
Fragment fragment = getSupportFragmentManager().findFragmentByTag(MyPreferenceFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new MyPreferenceFragment();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, fragment, MyPreferenceFragment.FRAGMENT_TAG);
ft.commit();
}
}
@Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat,
PreferenceScreen preferenceScreen) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
MyPreferenceFragment fragment = new MyPreferenceFragment();
Bundle args = new Bundle();
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey());
fragment.setArguments(args);
ft.replace(R.id.fragment_container, fragment, preferenceScreen.getKey());
ft.addToBackStack(preferenceScreen.getKey());
ft.commit();
return true;
}
}
提示.
- 请勿按
xml
添加片段,否则方向更改会导致崩溃. - 处理
onCreate
中添加的活动/片段的娱乐操作,以避免在首选项屏幕中丢失片段. - 片段的主机活动应实现
PreferenceFragmentCompat.OnPreferenceStartScreenCallback
并重新创建同一实例的片段.
- Do not add the fragment by
xml
you will have crashes on orientation changes. - Handle the recreations of activity / fragment add in
onCreate
so as to avoid losing your fragment when inside a preference screen. - The host activity of the fragment should implement the
PreferenceFragmentCompat.OnPreferenceStartScreenCallback
and recreate fragments of the same instance.
第2步. PreferenceFragment
public class MyPreferenceFragment extends PreferenceFragmentCompat {
public static final String FRAGMENT_TAG = "my_preference_fragment";
public MyPreferenceFragment() {
}
@Override
public void onCreatePreferences(Bundle bundle, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
提示.
- 使用方法
setPreferencesFromResource
并利用每个屏幕的rootKey
.这样,您的代码将可以正确重用. - 请记住,如果片段中有类似
findPreference
的代码,则应该进行null
检查,就像您在内部屏幕中一样,这将一无所获.
- Use the method
setPreferencesFromResource
and take advantage of therootKey
of each screen. This way your code will be reused properly. - Keep in mind that if you have code like
findPreference
in your fragment it should havenull
checks as when you were in inner screens this will give you nothing.
现在缺少的是操作栏中的后退箭头的实现(home操作),但是它永远无法单独工作;-)
The thing that is missing now is the implementation of the back arrow in the actionbar (home action) but this never works by itself ;-)
我还创建了一个演示应用程序,其中包装了所有这些代码,您可以在 github 上找到它.
I' also created a demo app wrapping all this code you can find it on github.
这篇关于内部PreferenceScreen无法使用PreferenceFragmentCompat打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!