0

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.

Keyur Sureliya
  • 164
  • 3
  • 11
  • 1
    `mViewPager.setOffscreenPageLimit(1);` Have you tried setting this? – Dipali Shah Jan 24 '18 at 10:40
  • define *it calls 'TabX.class'* ... if it means creating fragment class instance then just think: how it would be posible to see both fragments on swipeing without creating them? – Selvin Jan 24 '18 at 10:41
  • @Dipalis. No, i haven't. where should i put it? after viewPager.setAdapter(pagerAdapter);?? – Keyur Sureliya Jan 24 '18 at 10:43
  • *ViewPager.setOffscreenPageLimit(1);* - [Obviously, it doesn't change anything ...](https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/view/ViewPager.java#805) ... [also it is 1 by default](https://android.googlesource.com/platform/frameworks/support/+/jb-dev/v4/java/android/support/v4/view/ViewPager.java#88) ... nice example of [Million Monkeys Programming Style](https://en.wikipedia.org/wiki/Programming_by_permutation) – Selvin Jan 24 '18 at 10:46
  • @Dipalis. Thank you, but its not working. – Keyur Sureliya Jan 24 '18 at 10:46
  • @KeyurSureliya did you solve this ? – Harin Kaklotar May 16 '19 at 10:22

3 Answers3

1

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);
Deep Lathia
  • 750
  • 7
  • 18
0

ViewPager is intented to work in that way to avoid lag in animations while switching from one fragment to other fragment, it will load atleast one extra fragment alongside your Visible Fragment by which it makes sure there is scope to swipe.

because, while swiping view pager has capability to show two fragments simultaneously hence loads the one extra always, without two pages view pager is useless.

Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

For Refresh Fragment When Fragment Visible

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (getFragmentManager() != null) {
        getFragmentManager()
                .beginTransaction()
                .detach(this)
                .attach(this)
                .commit();
    }
}
Mukesh Kumar
  • 133
  • 1
  • 2
  • 8