Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView which is in a Fragment which is in a ViewPager does not reload/refresh views if setOffscreenPageLimit is reached

I have a RecyclerView in a Fragment which again is in a ViewPager, when I swipe between ViewPager pages, I see that onDetach of Fragment is being called but for some reason RecyclerView is still present in that particular position for the Fragment in ViewPager, even the scroll position of RecyclerView is being preserved when I come back to the detached Fragment.

I am using FragmentStatePagerAdapter for ViewPager and I am returning POSITION_NONE from getItemPosition() but it has no effect.

Please help me understand the problem here.

like image 316
Arif Nadeem Avatar asked Jul 27 '15 12:07

Arif Nadeem


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 androidx ViewPager widget ViewPager?

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android. app.

What is SetOffscreenPageLimit?

SetOffscreenPageLimit(BindableObject, Int32) Sets the number of off-screen pages that are stored in memory to the provided value . SetOffscreenPageLimit(IPlatformElementConfiguration<Android,TabbedPage>, Int32) Sets the number of off-screen pages that are stored in memory to the provided value .

Why ViewPager is used for in Android?

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.


1 Answers

Try this points.

1) MyAdapter extends FragmentStatePagerAdapter

2) Set Adapter from fragment using getChildFragmentManager() eg : mViewPager.setAdapter(new MyAdapter(getChildFragmentManager(), getActivity(), this))

3) From ViewpagerFrgment Override setUserVisibleHint method

@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //Update Recyclerview
        } else {

        }
    }
like image 157
OMAK Avatar answered Oct 25 '22 12:10

OMAK