我的代码中有三个片段类。 FirstActivity,SecondActivity,ThirdActivity我想刷这三个活动..这三个是从Fragment类扩展的
public class ViewPagerFragmentActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager_layout);
initialisePaging();
}
/**
* Initialize the fragments to be paged
*/
List<Fragment> fragments = new Vector<Fragment>();
ViewPager pager;
private void initialisePaging() {
fragments.add(Fragment.instantiate(this, FirstActivity.class.getName()));
fragments.add(Fragment.instantiate(this, SecondActivity.class.getName()));
fragments.add(Fragment.instantiate(this, ThirdActivity.class.getName()));
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
}
}
这是我的FragmentPageAdapter类
public class MyPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
System.out.println("position"+position);
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
我的代码以这种方式工作A<->B<->c
但我的要求是这样运行C<->A<->B<->C<-A->
圆形碎片是可以转换的......
答案 0 :(得分:19)
循环ViewPager是可能的。 在ViewPager as a circular queue / wrapping的帮助下,此链接的编码如此。希望它有助于你所需要的。
CircluarPagerAdapter类:
&GT;
public class CircularPagerAdapter extends PagerAdapter{
private int[] pageIDsArray;
private int count;
public CircularPagerAdapter(final ViewPager pager, int... pageIDs) {
super();
int actualNoOfIDs = pageIDs.length;
count = actualNoOfIDs + 2;
pageIDsArray = new int[count];
for (int i = 0; i < actualNoOfIDs; i++) {
pageIDsArray[i + 1] = pageIDs[i];
}
pageIDsArray[0] = pageIDs[actualNoOfIDs - 1];
pageIDsArray[count - 1] = pageIDs[0];
pager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int position) {
int pageCount = getCount();
if (position == 0){
pager.setCurrentItem(pageCount-2,false);
} else if (position == pageCount-1){
pager.setCurrentItem(1,false);
}
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
}
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
});
}
public int getCount() {
return count;
}
public Object instantiateItem(View container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int pageId = pageIDsArray[position];
View view = inflater.inflate(pageId, null);
((ViewPager) container).addView(view, 0);
return view;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public void finishUpdate(View container) {
// TODO Auto-generated method stub
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public void startUpdate(View container) {
// TODO Auto-generated method stub
}
}
这是你的主要课程:
&GT;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager myPager = (ViewPager) findViewById(R.id.conpageslider);
PagerAdapter adapter = new CircularPagerAdapter(myPager, new int[]{R.layout.first_activity, R.layout.second_activity, R.layout.third_activity});
myPager.setAdapter(adapter);
myPager.setCurrentItem(3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 1 :(得分:14)
您可以尝试这样的事情:
1)让getCount()
返回一个巨大的价值(一些刷卡方式高于用户可能使用的方式)
2)让getItem
返回类似的内容。fragments.get(position % fragments.size())
3)让你的寻呼机从getCount()
定义的范围中间某处开始(为了允许从头开始从A滑动到C),确保选择一个给你第一个片段%的值3
我知道这个解决方案看起来有点脏,但我现在找不到更好的解决方案