嗨,我的问题是我有 6 个frgments
,我正在使用ViewPager
从一个页面导航到另一个页面,反之亦然。但我希望在第 6个之后fragment
通过一次水平向前滑动以恢复到 1 Fragment
。就像从6->1
问问题
2235 次
1 回答
4
尝试在队列末尾再次添加第一个片段,当它加载时,使用 viewPager.setCurrentItem(0, false) 移动到第一个片段。
实际上,它并不总是有效。将此答案ViewPager 称为循环队列/包装,
我做了一个简单的例子看看:
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;
}
}
于 2013-02-11T09:57:20.400 回答