Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to update middle fragment in View Pager.

I've been working on a TAB layout with 3 tabs.

My problem is I'm unable to update,refresh 2nd tab swiping between the pages.

As per my debugging I swipe from tab one to second, the third is being called at getItem(int position), similarly fragment one is being called swiping from third tab to second.

The default getOffscreenPageLimit of viewpager is 1. I couldn't even set it to zero count.

At getItem, position is 1 at the very beginning but as soon as I swipe page, it only calls position 0 and 2. 1 is never called.

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

This is my ViewPagerSetUp

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new 
ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new InvitedFragment(), "Invited");
    adapter.addFragment(new InterestedFragment(), "Interested");
    adapter.addFragment(new AttendingFragment(), "Attending");
    viewPager.setAdapter(adapter);
}

This is my ViewPager

 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);
    }

    public void refreshFragment(int position) {
        mFragmentList.get(position).onResume();
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

addOnPageChangeListener was one option but I couldn't refresh it using pageChangelistener too.

How can I cure offscreenpagelimit ? How can I refresh every tab on every page swipe?

Thank you.

like image 575
Rajendra Maharjan Avatar asked May 11 '17 08:05

Rajendra Maharjan


People also ask

How do I refresh a ViewPager fragment?

OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.

What is ViewPager for?

ViewPager is a layout manager that allows the user to flip left and right through pages of data. It is mostly found in apps like Youtube, Snapchat where the user shifts right – left to switch to a screen. Instead of using activities fragments are used.

How to set ViewPager in android?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


1 Answers

Try this,

Override the below method in fragment.It call when fragment is visible

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        //do your Stuffs
    }
}
like image 69
Pradeep Gupta Avatar answered Sep 26 '22 03:09

Pradeep Gupta