简体   繁体   English

使用TabLayout和ViewPager片段位置值

[英]Fragment position value, Using TabLayout and ViewPager

I made 4 Fragments which are contained TabLayout and ViewPager. 我制作了4个包含TabLayout和ViewPager的片段。 And when I click tab, I want to see position value. 当我单击选项卡时,我想查看位置值。 so I used "Log.d". 所以我用“ Log.d”。

But it isn't same what I think. 但这与我的想法不同。 In my case, when I click second tab (its position value is "1"), Log.d shows "1". 就我而言,当我单击第二个选项卡(其位置值为“ 1”)时,Log.d显示为“ 1”。 But Log.d shows another value like "2" or "3". 但是Log.d显示另一个值,例如“ 2”或“ 3”。 And a value which Log.d shows is not always same when I click same tab. 当我单击相同的选项卡时,Log.d显示的值并不总是相同的。

For example, I click first tab and click second tab later, position Log.d shows "2". 例如,我单击第一个选项卡,然后单击第二个选项卡,位置Log.d显示为“ 2”。 But I click third or fourth tab, and click second tab later, position Log.d shows another value like "3" or "1". 但是我单击第三个或第四个选项卡,然后单击第二个选项卡,位置Log.d显示了另一个值,例如“ 3”或“ 1”。 I'm very confused. 我很困惑 What's the problem? 有什么问题?

Here is my code. 这是我的代码。

(I'm sorry that I have poor English skill ^^;) (对不起,我的英语水平很差^^;)

@Override
public Fragment getItem(int position){

    switch (position){
        case 0:
            Fragment1 fragment1 = new Fragment1();
            Log.d("Location", "0 Location " + position);
            return fragment1;
        case 1:
            Fragment2 fragment2 = new Fragment2();
            Log.d("Location", "1 Location " + position);
            return fragment2;
        case 2:
            Fragment3 fragment3 = new Fragment3();
            Log.d("Location", "2 Location " + position);
            return fragment3;
        case 3:
            Fragment4 fragment4 = new Fragment4();
            Log.d("Location", "3 Location는 " + position);
            return fragment4;
        default:
            return null;
    }

}

The Viewpager can be very confusing sometimes. 有时Viewpager可能会非常混乱。 When you are on the first fragment, the Viewpager will load the adjacent fragments into the memory. 当您在第一个片段上时,Viewpager会将相邻的片段加载到内存中。

If you are on the first page, the Viewpager will load the second fragment as well. 如果您在第一页上,则Viewpager也将加载第二个片段。 Similarly, when you are on the second fragment, the Viewpager loads the third fragment. 同样,当您在第二个片段上时,Viewpager会加载第三个片段。

If you are using Viewpager and you want to get the current fragment , you can get it by using the following code: 如果您正在使用Viewpager并希望获取当前片段 ,则可以使用以下代码来获取它:

int page = viewpager.getCurrentItem();

In PagerAdapter , PagerFragmentAdapter and PagerFragmentStateAdapter , the Views or Fragments are recognised by a key Object not by their index or position in the adapter. PagerAdapterPagerFragmentAdapterPagerFragmentStateAdapterViewsFragments由键Object识别,而不是由它们在适配器中的indexposition识别。

It shouldn't matter what view is displaying. 显示什么视图无关紧要。 When user swipes from page 1 to page 2, ViewPager loads the view for page 3 in anticipation of the next user swipe. 当用户从页面1滑动到页面2时, ViewPager会在下次用户滑动时加载页面3的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM