本文介绍了片段Viewpager navigatation以打圈的方式在前进horizo​​naral方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许志国我的问题是我有6 frgments ,我使用 ViewPager 从一个页面导航到另一个台钳6日后-versa.but我想片段由一个水平向前滑动翻盘,以1 片段。像从 6> 1

Hii My question is i have 6 frgments and i am using ViewPager to navigate from one page to another vise-versa.but i want after 6th fragment by one horizontal forward swipe to comeback to 1 Fragment .like from 6->1

推荐答案

尝试添加的第一个片段在队列的最后一次,它加载时,使用viewPager.setCurrentItem(0,假)移动到第一个片段。

Try to add the first fragment one more time at the end of the queue and when it is loaded, use viewPager.setCurrentItem(0, false) to move to the first fragment.

,其实它并不总是工作。谈到这个答案ViewPager作为一个循环队列/包装,我做了一个简单的例子

Actualy, it does not always work. Referring to this answer ViewPager as a circular queue / wrapping, I made a simple example

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;
    }
}

}

和分片类

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;
}

}

这篇关于片段Viewpager navigatation以打圈的方式在前进horizo​​naral方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:42