FragmentPagerAdapter以一种奇怪的方式工作

时间:2015-02-18 09:46:12

标签: android android-viewpager android-5.0-lollipop android-tabs

因为在Android L中不推荐使用操作栏导航模式我正在搜索另一种方法来获取选项卡,我发现可以使用PagerTabStrip (android.support.v4.view.PagerTabStrip),所以我创建了一个FragmentPageAdapter这样:

public class TitleAdapter extends FragmentPagerAdapter {

    private final String titles[] = new String[] { "Home", "Events", "People", "Books"};
    private final Fragment frags[] = new Fragment[titles.length];
    Context context;
    public TitleAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Log.v("TitleAdapter - getPageTitle=", titles[position]);
        return titles[position];
    }

    @Override
    public Fragment getItem(int position) {
        Log.v("TitleAdapter - getItem=", String.valueOf(position));
        //return frags[position];
        switch (position) {
        case 0:
            return Home.newInstance(0, "Home");
        case 1:
            return Events.newInstance(1, "Events");
        case 2:
            return People.newInstance(2, "People");
        case 4:
            return Books.newInstance(3, "Books"); 
        }
        return null;
    }

    @Override
    public int getCount() {
        return frags.length;
    }
}

我在LogCat中看到的一种奇怪的方式是,当mainActivity启动时,方法getItem()被调用4次,所以我要等待很多,因为在每个Tab中都有一个很长的列表,这个列表通过调用Web服务的HTTP请求填充。

我希望每次只加载一个片段而不是全部。当我使用actionbar.Tablistener它是可能的,但现在该方法已被弃用,所以有办法吗?

我以这种方式在onCreate方法中设置了适配器和viewPager:

@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.prova_page_tab_stripes);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        titleAdapter = new TitleAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(titleAdapter);
        mViewPager.setOffscreenPageLimit(1);
    }

1 个答案:

答案 0 :(得分:0)

初始化的页数取决于setOffscreenPageLimit的{​​{1}}函数。根据{{​​3}}中的android doc,

public void setOffscreenPageLimit(int limit)

ViewPager

如果要限制要保留的页面,请将其设置为1或保持默认值。

但是如果你想一次仅在一个页面中加载数据,你可以确定何时片段变得可见,然后加载数据(在Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed. This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth. You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1. Parameters limit How many pages will be kept offscreen in an idle state.中加载)。 请参阅此问题:http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)