Hii我的问题是我有6 frgments
我正在使用ViewPager
从一个页面导航到另一个页面反之亦然。但是我希望在第6个fragment
之后通过一个水平向前滑动回归到1 Fragment
。来自 6-> 1
答案 0 :(得分:4)
尝试在队列末尾再添加第一个片段,加载时,使用viewPager.setCurrentItem(0,false)移动到第一个片段。
Actualy,它并不总是有效。参考这个答案ViewPager as a circular queue / wrapping,
我做了一个简单的例子看看:
public class MainActivity extends FragmentActivity {
private static final int NUM_PAGES = 6;
private ViewPager pager;
private PagerAdapter pagerAdapter;
private List<SlideFragment> slideList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slideList = new ArrayList<SlideFragment>();
for (int i = 0; i<NUM_PAGES; i++){
SlideFragment slide = new SlideFragment();
slide.setIndex(i+1);
slideList.add(slide);
}
pager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
pager.setAdapter(pagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
int _pos = position % NUM_PAGES;
return slideList.get(_pos);
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
}
}
Fragment
类:
public class SlideFragment extends Fragment {
private int index;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup)inflater.inflate(R.layout.slide, container, false);
TextView tw = (TextView) root.findViewById(R.id.textView);
tw.setText(Integer.toString(index));
return root;
}
public void setIndex(int index){
this.index = index;
}
}