Viewpager with Tablayout not working proper

1.1k Views Asked by At

Here i am getting result like...

  1. Activity start..

    at at that time these two fragments calls -> Fragment 1 load -> Fragment 2 load in background

  2. swapping right (means going from frag 1 to fragment 2)

    at that time I am in fragment two and same time -> Fragment 3 load in background

  3. swapping in right (means going from Frag 2 to 3 then)

    --> there is nothing issue.

HERE IS MY CODE

Main Activity

@Override
protected void onCreate(Bundle savedInstanceState) {

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

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


}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");
    viewPager.setAdapter(adapter);
}

This is my adapter class

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

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

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
   // add fragment 
    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }
    // for title
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

And there is 3 fragment right now there is nothing into that fragment .....

3

There are 3 best solutions below

4
On BEST ANSWER

ViewPager setOffscreenPageLimit default is 1

Please view this link Visit Documentation

Update Please Override this method of fragment

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {

        // load data here
    }else{
        // fragment is no longer visible
    }
}
7
On

Replace your code with this. This runs well on my app :

@Override
protected void onCreate(Bundle savedInstanceState) {

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    assert tabLayout != null;
    tabLayout.setupWithViewPager(viewPager);

    tabLayout.setSelected(true);
}

And no need to add viewPager.setCurrentItem(0);

Because by default ViewPager will be set to the Starting index of 0. As already told by Ajay Jayendran.

Hope this will works. (:

1
On

Use this tutorial..this may helps you

http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

you need not include this lines in your onCreate Method (Remove this lines)

 viewPager.setCurrentItem(0);
 viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

By default,Viewpager will set '0' for starting index..