我正在尝试将 a 添加 PreferenceFragment
到FragmentPagerAdapter
.
我的课程扩展FragmentActivity
了,我尝试过FragmentTransaction
,如下所示,并尝试添加到容器中,但似乎无法正常工作。没有错误被抛出,实际上什么也没有发生。
主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
活动主.xml:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</android.support.v4.view.ViewPager>
我的选项菜单选择:
android.app.FragmentManager fm;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
JJSettings settings = new JJSettings();
fm = getFragmentManager();
FragmentTransaction fragTrans = fm.beginTransaction();
// I also tried `replace()` here as well. Same 'nothing happens' result.
fragTrans.add(settings, "settings");
fragTrans.commit();
return true;
case R.id.menu_help:
menuHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我的设置 Fragment
:
public class JJSettings extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
如果可能的话,我的偏好是坚持使用Fragments
,这意味着如果可能的话,我宁愿不扩展PreferenceActivity
或将用户带到另一个调用 的 Activity PreferenceFragment
。我只是希望我在研究中遗漏了一些东西。
编辑
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int _count = 2;
public SectionsPagerAdapter(FragmentManager fm) { super(fm); }
@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new JJMainFragment();
case 1:
return new JJPendingFragment();
default:
return null;
}
}
public void setCount(int count) { this._count = count; }
@Override
public int getCount() { return this._count; }
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.c_list).toUpperCase(Locale.ENGLISH);
case 1:
return getString(R.string.c_pending).toUpperCase(Locale.ENGLISH);
}
return null;
}
}