我有一个TabLayout活动,并且对于其中一个选项卡(情况3),我想将PreferenceFragment
用于设置页面。在下面的情况3中,我已经声明了片段,但我该如何开始呢?如何从xml资源中启动一个片段
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
case 3: {
SettingsFragment fragment = new SettingsFragment();
// what goes here?
break;
}
}
return rootView;
}
全码:
public class Find extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
TabLayout.Tab tab1 = tabLayout.getTabAt(0);
tab1.setCustomView(R.layout.icon_view);
tab1.setIcon(R.drawable.profile);
TabLayout.Tab tab2 = tabLayout.getTabAt(1);
tab2.setCustomView(R.layout.icon_view);
tab2.setIcon(R.drawable.logo_a);
TabLayout.Tab tab3 = tabLayout.getTabAt(2);
tab3.setCustomView(R.layout.icon_view);
tab3.setIcon(R.drawable.settings);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
case 3: {
SettingsFragment fragment = new SettingsFragment();
// what goes here?
break;
}
}
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
你有没有使用过任何适配器?如果是,请发布您的适配器类 – FAT
在编辑中添加完整代码。 – Zorgan
看到我的答案。希望这将解决您的问题 – FAT