将最后一个片段设置为ViewPager中的起始点

时间:2015-03-12 18:03:43

标签: android android-viewpager

我正在开发一个项目,我在一个屏幕上使用ViewPager。它有7页。

为此,我使用以下代码:

tabsViewPager = (MyViewPager)findViewById(R.id.tabspager);
mTabsAdapter = new Tabsadapter(getSupportFragmentManager());


//set adapter to tabs view
tabsViewPager.setAdapter(mTabsAdapter);

tabsViewPager.setEnabledSwipe(true);
tabsViewPager.setCurrentItem(6);

我的适配器是:

public class Tabsadapter extends FragmentStatePagerAdapter {

    private int TOTAL_TABS = 7;

    public Tabsadapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int index) {
        // TODO Auto-generated method stub
        Log.e("Index call", String.valueOf(index));
        Fragment fg=new ShowThoughtActivity();

         switch (index) {
            case 6:
                fg = new ShowThoughtActivity();
                break;
            case 5:
                fg = new Thought1Activity();
                break;
            case 4:
                fg = new Thought2Activity();
                break;
            case 3:
                fg = new Thought3Activity();
                break;
            case 2:
                fg = new Thought4Activity();
                break;
            case 1:
                fg = new Thought5Activity();
                break;
            case 0:
                fg = new Thought6Activity();
                break;
            }
         return fg;

    }

     @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return TOTAL_TABS;
    }

     @Override
        public int getItemPosition(Object object) {
            // TODO Auto-generated method stub
            return 0;
        }
     @Override
     public boolean isViewFromObject(View view, Object object) {
         if(object != null){
             return ((Fragment)object).getView() == view;
         }else{
             return false;
         }
     }

}

我的问题是当我运行此代码时,ViewPager会自动滚动到片段5,即案例5片段。

我知道FragmentStatePagerAdapter getItem()会创建两个实例。但是如何将当前最后一个片段设置为当前片段?

0 个答案:

没有答案