Preface
In the project, sometimes it is used to display the same type of Fragment in the ViewPager. At the same time, the number of such Fragments is dynamic, but the PagerAdapter does not provide us with a similar method to getCurrentFragment. Let’s introduce to you how to get the currently displayed Fragment in ViewPager in Android, let’s take a look.
1. Use getSupportFragmentManager().findFragmentByTag() method
Viewpager + FragmentPagerAdapter is only good; FragmentPagerAdapter has a feature: Any loaded Fragment will be retained. Since the Fragment will not be destroyed, we can use it.findFragmentByTag()
The method finds it; but the problem is the setting of the tag if it is dynamically created; we know that tags can be set when it is dynamically loaded, but dynamic creation cannot be set; if the viewpager uses the adapter and FragmentPagerAdapter, the Fragment will be set on the tag. According to this tag, we can get the currently displayed fragment;
Let's take a look at this process
When newly created tag ==null
@Override public Fragment getItem(int position) { PageTab pageTab = (position); TabFragment tab = new TabFragment(); (TAG,"====getItem===Newly createdtag:=="+()); ().putSerializable("tab",pageTab); //Take out the data and create a new Fragment return tab; }
Log is null when viewing tags
E/FileListPagerAdapter: ====getItem===Newly createdtag:==null
Set the adapter to the tag after Viewpager
E/MainActivity: ==findCurrentFragment==pagerId:2131492976 E/MainActivity: ==findCurrentFragment==currentItem:0 E/MainActivity: =======findCurrentFragment=========wholeTag===android:switcher:2131492976:0 E/MainActivity: =======findCurrentFragment=========Currently displayedFragment oftag===:android:switcher:2131492976:0
You can see the composition of the tag; four parts
- android : Fixed part
- switcher:
- 2131492976 :
- 0 : ViewPager The itemPostion currently displayed
Since the FragmentPagerAdapter is characterized by saving fragment state. You can use this feature tofindFragmentByTag()
Find this Fragment
//Viewpager + FragmentPagerAdapter In case of Viewpager + FragmentPagerAdapter, you can only find the currently displayed Fragment that has been loadedFragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:"+()+":"+());
2. Rewrite the setPrimaryItem() method of the adapter;
This method will be called every time the viewpager slides, and the object parameter is the displayed Fragment; a variable can be defined in the adapter. CurrentFragment will be obtained every time the displayed Fragment is changed.
@Override public void setPrimaryItem(ViewGroup container, int position, Object object) { currentFragment = (TabFragment) object; (container, position, object); }
This method has a drawbacksetPrimaryItem()
It will not be called after the viewpager's sliding monitor is executed; so it is wrong to obtain the currently displayed fragment in the replaced sliding monitor.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to all Android developers. If you have any questions, you can leave a message to communicate.