MyFragment currentFragment = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("f"+pagerMainAdapter.getItemId(0));
currentFragment.testRun("Hello world");
I'm trying to Migrate from ViewPager
to ViewPager2
and and I don't know how to get fragment in ViewPager2
.
To get the current fragment from ViewPager2:
The ViewPager2
has getCurrentItem()
which returns the current page number
So, we need to link each page fragment to the corresponding page number.
But we can get a ViewPager2
fragment by its id (item id), so the first step is to have page Ids that equals to the corresponding position, to do so override getItemId
in the ViewPager2
FragmentStateAdapter
.
@Override
public long getItemId(int position) {
return position;
}
Then to get the current fragment:
int pageId = viewpager.getCurrentItem();
MyFragment currentFragment = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("f" + pageId);
EDIT:
Apart from the question, getting the viewPager
fragment using "f$id"
can be discontinued some day by Google in internal APIs; so you should use another way for that; here is a one that can be used in API 24+:
Using newInstance
page fragment pattern, you can set some argument in the viewPager
page fragment, assuming viewPager
fragment is PagerFragment
:
Argument constant:
public static final String POSITION = "POSITION";
Adapter:
class ViewPagerFragmentAdapter extends FragmentStateAdapter {
....
@NonNull
@Override
public Fragment createFragment(int position) {
return PagerFragment.newInstance(position);
}
}
PageFragment:
public class PagerFragment extends Fragment {
public static Fragment newInstance(int position) {
PagerFragment fragment = new PagerFragment();
Bundle args = new Bundle();
args.putInt(POSITION, position);
fragment.setArguments(args);
return fragment;
}
}
And to get a fragment of a certain viewPager
item:
int id = pagerMainAdapter.getItemId(0); // id of your ViewPager that you need to get the corresponding PageFragment
Optional<Fragment> optionalFragment =
getSupportFragmentManager().getFragments().stream()
.filter(it -> it instanceof PagerFragment && it.getArguments() != null && it.getArguments().getInt(POSITION) == id)
.findFirst();
optionalFragment.ifPresent(fragment -> {
// This is your needed PageFragment
});