Implementing tab button with ViewPager.

Asked 1 years ago, Updated 1 years ago, 135 views

I implemented it in this way in the viewPager adapter

 @Override
    public Fragment getItem(int position) {

        Log.d("test","=====position====" + position);

        switch (position){
            case 0:
                fragement a = new a();
                return a;
            case 1:
                fragment b = new b();
                return bt;
            case 2:
                fragment c = new c();
                return c;
            case 3:
                fragment d = new d();
                return d;
            default:
                return null;
        }

I did it like this, but when the viewpager is called at first, the position is set to 0,1 Positions in getItem are 0, 1, 1, 2, 3, so there are cases where 3 is called at the release of number 2. The position value in the onPageSeleten is set to position 1 if you press the selected number once. GetItempos keeps getting 1, 2. Please help me with the solution.

android android-viewpager tab fragment java

2022-09-22 11:40

1 Answers

Calling 1 and 2 all at once within the getItem() function is the intended behavior of ViewPager. ViewPager creates the left and right pages of the current screen at once.

As shown above, ViewPager creates pages 1 and 3 on the left and right as well as page 2 on the current screen. Page 1 and 3 are called off-screen pages. Use the ViewPager.setOffscreenPageLimit(intlimit); function to control how many off-screen pages can be created.

It would be helpful if you read the following link about it.


2022-09-22 11:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.