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.
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.
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.
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 .
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.
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 {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With