我正在使用ViewPager类制作具有水平页面的应用。目前,在正确显示页面时遇到问题。此代码主要来自示例,几乎没有任何修改。我不确定片段“切换”应该如何发生,但我在onCreateView上进行了半工作。现在,当我启动应用程序时,我得到欢迎屏幕,下一页再次欢迎屏幕,然后是第1/2/3页。当“向后移动”时,它会像第3页,第2页,第3页,第2页,第1页那样。 可能是mViewPager.getCurrentItem()存在问题,但我为何无能为力。
public class MainActivity extends ActionBarActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
static ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
final ActionBar actionBar = getSupportActionBar();
actionBar.hide();
setContentView(R.layout.activity_main);
actionBar.setCustomView(R.layout.custom_actionbar);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 5;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "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 PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int page = mViewPager.getCurrentItem();
View rootView;
switch (page) {
case 0: rootView = inflater.inflate(R.layout.welcome_screen, container, false);
break;
case 1: rootView = inflater.inflate(R.layout.page1, container, false);
break;
case 2: rootView = inflater.inflate(R.layout.page2, container, false);
break;
case 3: rootView = inflater.inflate(R.layout.page3, container, false);
break;
case 4: rootView = inflater.inflate(R.layout.page4, container, false);
break;
case 5: rootView = inflater.inflate(R.layout.page5, container, false);
break;
default: rootView = inflater.inflate(R.layout.welcome_screen, container, false);
}
}
}
}
答案 0 :(得分:3)
问题是您根据用户正在查看的视图在ViewPager中创建视图。这显然不是您的意图,因为ViewPager会在认为最适合时生成视图,这与用户正在查看的内容无关。
为了说明这一点,假设应用程序启动,用户将查看第1页.ViewPager准备:
但是,由于您根据显示的项目对视图进行了充气,因此这两个视图都将成为欢迎页面:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int page = mViewPager.getCurrentItem();
View rootView;
switch (page) {
...
}
}
您应该选择页面索引,因为它是从片段的参数中提供的:
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private int mPage;
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;
}
@Override
public void onCreate(Bundle savedInstanceState) {
Bundle args = getArguments();
mPage = args.getInt(ARG_SECTION_NUMBER);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
switch (mPage) {
case 0:
rootView = inflater.inflate(R.layout.welcome_screen, container, false);
break;
case 1:
rootView = inflater.inflate(R.layout.page1, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.page2, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.page3, container, false);
break;
case 4:
rootView = inflater.inflate(R.layout.page4, container, false);
break;
case 5:
rootView = inflater.inflate(R.layout.page5, container, false);
break;
default:
rootView = inflater.inflate(R.layout.welcome_screen, container, false);
break;
}
}
}
答案 1 :(得分:0)
简单地传递ArrayList<整数>布局xml文件id的内容和扩展PagerAdapter
public class SectionsPagerAdapter extends PagerAdapter{
ArrayList<Integer> objects=null;
public SectionsPagerAdapter(Context ctx,ArrayList<Integer> objects) {
this.objects = objects;
}
@Override
public Integer getItem(int position) {
return objects.get(position);
}
@Override
public int getCount() {
return 5;
}
@Override
public View instantiateItem(ViewGroup container, final int position) {
View convertView = LayoutInflater.from(mContext).inflate(
this.objects.get(position), null);
((ViewPager) container).addView(convertView);
return convertView;
}
}
也可以在谷歌上搜索这样的样本。
答案 2 :(得分:0)
你只需要一个Bundle来保存你的数据。 例如:这是一个实例化的listfragment,它包含两个参数,然后我们需要使用这两个参数来实例化viewpager中的第一页
public static TitleFragment newInstance(int i,int k){
TitleFragment fragment = new TitleFragment();
//add Bundle
PAGE = k;
NTYPE = i;
Bundle args = new Bundle();
args.putInt("PAGE", PAGE);
args.putInt("NTYPE", NTYPE);
fragment.setArguments(args);
Log.d("list", "list" + i);
return fragment;
}
我们在onCreate Method
中获取数据@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int SomeInt = getArguments().getInt("PAGE", 1);
int someTitle = getArguments().getInt("NTYPE", NTYPE);
list = getNewList(someTitle,SomeInt);
myAdapter = new MyAdapter(getActivity(),list,R.layout.news_item);
}
它正常工作。