Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-tablayoutandroid-studio-3.0

View Pager is calling other fragments on startup and on swiping too


List of components in my app is following :

  1. TabLayout - Tab1, Tab2, Tab3.
  2. Fragments for Tabs - Tab1.xml & class, Tab2.xml & class, Tab3.xml & class.
  3. ViewPager to show Fragments on tabs.

So, when app starts as i debugged the startup process, it works like following :

  1. it calls 'Tab1.class',
  2. then it calls 'Tab2.class',

and when swipe from 'Tab1' to 'Tab2', it calls 'Tab3.class'.

and when swipe from 'Tab2' to 'Tab3', it calls nothing.

on reverse swiping :

when swipe from 'Tab3' to 'Tab2', it calls 'Tab1.class'.

then when swipe from 'Tab2' to 'Tab1', it calls nothing.

Questions :

  1. So, why does it do that?

  2. How can i call ONLY 'tab1.class' when app starts on 'tab1'?

  3. How can i call ONLY 'tab2.class' when i swipe from 'tab1' to 'tab2'?


MainActivity.java

    public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        PagerAdapter pagerAdapter = new FixedTabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);
    }

    class FixedTabsPagerAdapter extends FragmentPagerAdapter {

        public FixedTabsPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position)
            {
                case 0:
                    return new Tab1();
                case 1:
                    return new Tab2();
                case 2:
                    return new Tab3();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
             return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position)
            {
            case 0:
                return getString(R.string.tab1);
            case 1:
                return getString(R.string.tab2);
            case 2:
                return getString(R.string.tab3);
                default:
                    return null;
            }
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="vwc.com.newconcept.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.Toolbar
                android:id="@+id/main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabLayout"
                    app:tabMode="scrollable"
                    app:tabGravity="center"
                    android:layout_gravity="top"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </android.support.v4.view.ViewPager>

        </android.support.design.widget.AppBarLayout>

</android.support.v4.widget.DrawerLayout>

Please Do Tell Solution For This Problem.


Solution

  • setOffscreenPageLimit

    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.

    Hope this helps.

    ViewPager mViewpager = (ViewPager)findView....
    mViewPager.setOffscreenPageLimit(3);