5

When I switched to viewPager2 I had to implement another type of adapter (I used FragmentPagerAdapter and now I use FragmentStateAdapter), now I want to call the instantiateItem element that had the old adapter but it doesn't appear in the new one.

public void onPageChange(int position) {
    //I cannot call instantiateItem from pagerAdapter
    Object object = pagerAdapter.instantiateItem(mMainViewPager,position);
    if (object instanceof MapFragment) {
        MapFragment fragment = (MapFragment) object;
        //fragment.setDevice(mDevice);
        //fragment.setLastTracking();
    }
    if (object instanceof DevicesFragment) {
        DevicesFragment fragment = (DevicesFragment) object;
        //fragment.refresh();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Did you check [the docs for `FragmentStateAdapter`](https://developer.android.com/reference/androidx/viewpager2/adapter/FragmentStateAdapter)? – Mike M. Sep 13 '19 at 22:41
  • 1
    I'm in the same situation and wonder what's the alternative of `instantiateItem` for FragmentStateAdapter to work with viewPager2. As suggested by @MikeM., I checked the docs but could not find anything helpful. – waseefakhtar Dec 19 '19 at 11:13

1 Answers1

0

I couldn't reach same method which has same functionality with instiateItem element although many tried in view pager 2.Instead of that,I created fragment list which is accept the view pager fragments element.You can ask where did you put them into this list.When the fragment state adapter class call createFragment() method, I pushed into the list.After that, I reached with get method from my host fragment

Enes Zor
  • 973
  • 8
  • 14
  • This does not sound like a good solution without deleting the fragments from that list, too. RecyclerView.Adapter.onViewRecycled(VH) might be a good starting point for deletion. – Dominikus K. Mar 29 '21 at 14:30