我有这个问题,如下图所示:
1) http://imageshack.us/photo/my-images/19/schermata20120512a09444.png/ 2) http://imageshack.us/photo/my-images/651/schermata20120512a09444.png/
我已经用三个片段完成了一个 viewpager,但是当滚动时,我看到其中一个,我会看到两个片段。
我的问题是:
公共类 FragmentProjetSwipePageActivity 扩展 FragmentActivity {
// derived from http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/
// list contains fragments to instantiate in the viewpager
List<Fragment> fragmentsV1 = new Vector<Fragment>();
// page adapter between fragment list and view pager
private PagerAdapter mPagerAdapter;
// view pager
private ViewPager mPager;
// activity data
public String p2text,p3text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// creating fragments and adding to list
fragmentsV1.add(Fragment.instantiate(this,Page1Fragment.class.getName()));
fragmentsV1.add(Fragment.instantiate(this,Page2Fragment.class.getName()));
fragmentsV1.add(Fragment.instantiate(this,Page3Fragment.class.getName()));
// creating adapter and linking to view pager
mPager = (ViewPager) super.findViewById(R.id.pager1);
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(),fragmentsV1,this );
mPager.setAdapter(this.mPagerAdapter);
//Set a listener that will be invoked whenever the page changes or is incrementally scrolled.
} }
PagerAdapter.java
公共类 PagerAdapter 扩展 FragmentPagerAdapter {
// fragments to instantiate in the viewpager
private List<Fragment> fragments;
FragmentManager fm;
private static int NUM_PAGER_VIEWS = 2;
Context contex;
// constructor
public PagerAdapter(FragmentManager _fm,List<Fragment> fragments) {
super(_fm);
this.fragments = fragments;
fm=_fm;
}
// return access to fragment from position, required override
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
// number of fragments in list, required override
@Override
public int getCount() {
return NUM_PAGER_VIEWS;
//return this.fragments.size();
}
}
谢谢