我正在使用Fragments做一个简单的3页应用程序,页面之间有幻灯片。
但是现在,我有3个片段,但应用程序只能使用de 2和3进行滑动。
必须1
- > 2
- > 3
,但3
- > 2
- > 3
。如果你知道我想说的话。
这是我的代码
public class main extends Activity {
public static SectionsPagerAdapter mSectionsPagerAdapter;
public static Boolean init = true;
static ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
[...]
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private static int ARG_NUMBER = 0;
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
ARG_NUMBER = sectionNumber;
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = new View(this.getActivity());
// THE PROBLEM SHOULD COME FROM HERE
switch (ARG_NUMBER) {
case 1:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_map, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.profile, container, false);
break;
}
return rootView;
}
}
}
所以,如果有人有想法......谢谢!
编辑:
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;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
答案 0 :(得分:0)
您应该use arguments
片段instead of
设置ARG_NUMBER = sectionNumber
您的代码应如下所示:
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private int ARG_NUMBER = 0;
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 PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = new View(this.getActivity());
ARG_NUMBER = getArguments().getInt(ARG_SECTION_NUMBER, 0);
switch (ARG_NUMBER) {
case 1:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_map, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.profile, container, false);
break;
}
return rootView;
}
}
注意:如果Switch case不允许非常量值,请使用if-else梯形图。