创建Class实例化PreferenceFragment
Create Class to instantiate PreferenceFragment
我有一个带有片段的应用程序。我无法将片段的 work.One 的 PreferenceFragment 设为设置页面。为此,我希望使用 PreferenceFragment,但是我使用 android.support.v4.app.FragmentManager 开发了我的应用程序,它与 PreferenceFragment 不兼容。下面的 link 有描述和 link 代码,可以与 v4 一起使用。
https://github.com/codepath/android_guides/wiki/Settings-with-PreferenceFragment
https://github.com/kolavar/android-support-v4-preferencefragment
我的 MainActivity
private class CustomAdapter extends FragmentPagerAdapter {
private String fragments [] = {"Event","Gallery","Match","Settings"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new event();
case 1:
return new gallery();
case 2:
return new match();
case 3:
return new PreferenceFragment();
default:
return null;
}
}
错误:'PreferenceFragment'是抽象的,无法实例化。
偏好片段
public abstract class PreferenceFragment extends Fragment implements
PreferenceManagerCompat.OnPreferenceTreeClickListener {
private static final String PREFERENCES_TAG = "android:preferences";
有人告诉我不能直接实例化 PreferenceFragment,我必须创建一个 class 来扩展它。有人可以解释一下在这种情况下我会怎么做吗?太感谢了! :-)
例如:
public class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_preference);
}
}
及其关联的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="FOO">
<CheckBoxPreference
android:key="checkBoxPref"
android:title="check it out"
android:summary="click this little box"/>
</PreferenceCategory>
</PreferenceScreen>
来源:How do you create Preference Activity and Preference Fragment on Android?
我有一个带有片段的应用程序。我无法将片段的 work.One 的 PreferenceFragment 设为设置页面。为此,我希望使用 PreferenceFragment,但是我使用 android.support.v4.app.FragmentManager 开发了我的应用程序,它与 PreferenceFragment 不兼容。下面的 link 有描述和 link 代码,可以与 v4 一起使用。
https://github.com/codepath/android_guides/wiki/Settings-with-PreferenceFragment
https://github.com/kolavar/android-support-v4-preferencefragment
我的 MainActivity
private class CustomAdapter extends FragmentPagerAdapter {
private String fragments [] = {"Event","Gallery","Match","Settings"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new event();
case 1:
return new gallery();
case 2:
return new match();
case 3:
return new PreferenceFragment();
default:
return null;
}
}
错误:'PreferenceFragment'是抽象的,无法实例化。
偏好片段
public abstract class PreferenceFragment extends Fragment implements
PreferenceManagerCompat.OnPreferenceTreeClickListener {
private static final String PREFERENCES_TAG = "android:preferences";
有人告诉我不能直接实例化 PreferenceFragment,我必须创建一个 class 来扩展它。有人可以解释一下在这种情况下我会怎么做吗?太感谢了! :-)
例如:
public class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_preference);
}
}
及其关联的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="FOO">
<CheckBoxPreference
android:key="checkBoxPref"
android:title="check it out"
android:summary="click this little box"/>
</PreferenceCategory>
</PreferenceScreen>
来源:How do you create Preference Activity and Preference Fragment on Android?