如何switch语句中FragmentPagerAdapter的getItem()时工作? [英] How switch statement in FragmentPagerAdapter's getItem() work?

查看:251
本文介绍了如何switch语句中FragmentPagerAdapter的getItem()时工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看一看这个code:

@Override
    public Fragment getItem(int index){

       Log.i("Tab position", String.valueOf(index));
        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new TopRatedFragment();
        case 1:
            // Games fragment activity
            return new GamesFragment();
        case 2:
            // Movies fragment activity
            return new MoviesFragment();
        }

        return null;
    }    

       @Override
        public int getCount() {
            // TODO Auto-generated method stub

            return 3;
        }

ViewPager 工作正常,但怎么样?

ViewPager works fine but how ?


  1. 当我开始我的应用我获得以下的logcat与TabPosition 0和1:

  1. When i start my app i get following Logcat with TabPosition 0 and 1:

15 09-16:28:20.353:I /制表位​​置(2737):0
 09-16 15:28:20.372:I /制表位​​置(2737):1

现在,当我选择第二个选项卡或滑动到第二页我获得以下与TabPosition 2 logcat的:

Now when i select second Tab or swipe to second page i get following Logcat with TabPosition 2:

15 09-16:28:29.072:I /制表位​​置(2737):2

当我选择第三个选项卡或滑动到第三页则没有logcat的,并且不打印TabPosition:

when i select third Tab or swipe to third page then there is no Logcat and TabPosition not printed :

我的困惑是我知道指数的getItem()返回的不是当前所选择的页面或标签的位置,但它是需要被渲染页面的位置。

My confusion is I know that index returned by getItem() is not a position of current selected Page or Tab but it is a position of pages that need to be rendered.

但如果这是switch语句那么如何正确返回片段的情况下,虽然指数是不正确的。

But if this is a case then how switch statement returns correct Fragment although index is not correct one.

例如:

最初有通过的getItem返回两个索引() 0和1,switch语句现在如何决定选择0或1。

Initially there are two indexes returned by getItem() 0 and 1, now how switch statement decides to select 0 or 1.

我希望你明白的问题。

推荐答案

它的工作原理是这样的:

It works like this:

当您设置适配器ViewPager会要求适配器第一个片段显示(索引0)和一个向右(指数1)。

When you set the adapter the ViewPager will ask the adapter for the first fragment to display (index 0) and for the one to the right (index 1).

这是因为ViewPager始终缓存之外,因此目前显示的某些页面。为默认值一些是1,但你可以改变它

That is because the ViewPager always caches some pages besides the one that's currently shown. The default value for "some" is 1 but you can change it with

<一个href=\"http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)\" rel=\"nofollow\">http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)

注意,0没有左邻居(索引-1),因此,只有索引0和1在开始被加载。

Note that 0 has no left neighbor (index -1), so only indices 0 and 1 are loaded at the beginning.

现在,当你页面,指标1,ViewPager已经在其高速缓存中的相应片段,并可以显示它的时候了。但它也将要求适配器指数2,因为这将是下一个。

Now, when you page to index 1, the ViewPager already has the corresponding fragment in its cache and can display it right away. But it will also ask the adapter for index 2, because it will be the next one.

同样的,当你页面索引2,它已经拥有的片段,并可以显示它。而且,由于2无权邻居(其中ViewPager从你知道的getCount)没有什么更多的加载...

Similarly, when you page to index 2, it already has the fragment and can display it. And since 2 has no right neighbor (which the ViewPager knows from your getCount) there is nothing more to load...

编辑:那么,更直接地回答你原来的问题:switch语句从来没有决定0和1之间的getItem 1被调用一次为0,一次,就返回适当的片段在每种情况下,然后ViewPager决定显示的这些。

So, to answer your original question more directly: The switch statement never has to decide between 0 and 1. getItem is called once for 0 and once for 1, it returns the appropriate fragment in each case and then the ViewPager decides which of these to display.

这篇关于如何switch语句中FragmentPagerAdapter的getItem()时工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆