我正在开发一个多页应用程序,我需要放一个页面视图,我是android的新手,所以我不知道更多关于它,我研究了很多并找到了一些相关的解决方案,但这不起作用对我来说。
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Simple"),
CountingFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("List"),
FragmentPagerSupport.ArrayListFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Cursor"),
CursorFragment.class, null);
但我仍然没有得到预期的结果。
答案 0 :(得分:1)
试试这个:
setContentView(R.layout.viewpager);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
LayoutInflater mLi = LayoutInflater.from(this);
View view1 = mLi.inflate(R.layout.whats1, null);
View view2 = mLi.inflate(R.layout.whats2, null);
View view3 = mLi.inflate(R.layout.whats3, null);
View view4 = mLi.inflate(R.layout.whats4, null);
View view5 = mLi.inflate(R.layout.whats5, null);
final ArrayList<View> views = new ArrayList<View>();
views.add(view1);
views.add(view2);
views.add(view3);
views.add(view4);
views.add(view5);
PagerAdapter mPagerAdapter = new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0.equals(arg1);
}
@Override
public int getCount() {
return views.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(views.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(views.get(position));
return views.get(position);
}
};
mViewPager.setAdapter(mPagerAdapter);
请注意,PagerAdapter中的4覆盖方法是完成ViewPager所必需的。