When the item of ViewPager is Fragment, FragmentPagerAdapter is generally used as the Adapter, and this class will be used when adding Fragment To add a tag, see the instantiateItem() method in the source code of FragmentPagerAdapterViewPager的item是Fragment时一般使用FragmentPagerAdapter作为Adapter, 而这个类在添加Fragment的时候会添加tag, 看FragmentPagerAdapter的源码中的instantiateItem()方法
这个tag是通过一个makeFragmentName()的私有方法获取, 不过只要把这个方法直接复制出来就可以了, 这个方法的参数viewId应该是ViewPager的id, itemId是adapter的getItemId()
rrreee
This tag is obtained through a private method of makeFragmentName(), but you only need to copy this method directly. The parameter viewId of this method should be the id of ViewPager , itemId is the getItemId() method of the adapter, so you can assemble the tag yourself and get the Fragment.
I have also been thinking about this problem for a while, and after searching for it, I couldn’t find a good solution. The pagerfragmentadapter doesn’t seem to provide an API for getting the corresponding fragment. I made a list of weak references in the index and added it to the adapter when it initialized the fragment, and then rewritten the adapter's onDestroyItem (probably called this? I can't remember) to remove the corresponding entry in the list here. Once you get the fragment instance, you can operate it as you like.
When the Fragment is in the foreground, you can use LocalBroadcastManager / EventBus / otto / Rx to subscribe to an event (message)
When the Fragment switches to the background, cancel the subscription. When the Fragment switches to the foreground again, refresh the page data and resubscribe
When the item of
ViewPager
isFragment
,FragmentPagerAdapter
is generally used as the Adapter, and this class will be used when addingFragment
To add a tag, see theinstantiateItem()
method in the source code ofFragmentPagerAdapter
ViewPager
的item是Fragment
时一般使用FragmentPagerAdapter
作为Adapter, 而这个类在添加Fragment
的时候会添加tag, 看FragmentPagerAdapter
的源码中的instantiateItem()
方法这个tag是通过一个
That’s it.🎜makeFragmentName()
的私有方法获取, 不过只要把这个方法直接复制出来就可以了, 这个方法的参数viewId应该是ViewPager
的id, itemId是adapter的getItemId()
rrreeeThis tag is obtained through a private method of
makeFragmentName()
, but you only need to copy this method directly. The parameter viewId of this method should be the id ofViewPager
, itemId is thegetItemId()
method of the adapter, so you can assemble the tag yourself and get the Fragment.If you use RecyclerView, you can use Eventbus like this
I have also been thinking about this problem for a while, and after searching for it, I couldn’t find a good solution. The pagerfragmentadapter doesn’t seem to provide an API for getting the corresponding fragment.
I made a list of weak references in the index and added it to the adapter when it initialized the fragment, and then rewritten the adapter's onDestroyItem (probably called this? I can't remember) to remove the corresponding entry in the list here.
Once you get the fragment instance, you can operate it as you like.
When the Fragment is in the foreground, you can use LocalBroadcastManager / EventBus / otto / Rx to subscribe to an event (message)
When the Fragment switches to the background, cancel the subscription. When the Fragment switches to the foreground again, refresh the page data and resubscribe