Get current fragment with ViewPager2

2020-05-25 07:55发布

I'm migrating my ViewPager to ViewPager2 since the latter is supposed to solve all the problems of the former. Unfortunately, when using it with a FragmentStateAdapter, I don't find any way to get the currently displayed fragment.

viewPager.getCurrentItem() gives the current displayed index and adapter.getItem(index) generally creates a new Fragment for the current index. Unless keeping a reference to all created fragments in getItem(), I have no idea how to access the currently displayed fragment.

With the old ViewPager, one solution was to call adapter.instantiateItem(index) which would return the fragment at the desired index.

Am I missing something with ViewPager2?

7条回答
Summer. ? 凉城
2楼-- · 2020-05-25 07:57

In ViewPager2 the FragmentManager by default have assigned tags to fragments like this:

Fragment in 1st position has a tag of "f0"

Fragment in 2nd position has a tag of "f1"

Fragment in 3rd position has a tag of "f2" and so on... so you can get your fragment's tag and by concatenating the "f" with position of your fragment. To get the current Fragment you can get current position from the viewPager2 position and make your tag like this (For Kotlin):

val myFragment = supportFragmentManager.findFragmentByTag("f" + viewpager.currentItem)

For fragment at a certain position

val myFragment = supportFragmentManager.findFragmentByTag("f" + position)

You can cast the Fragment and always check if it is not null if you are using this technique.

查看更多
你好瞎i
3楼-- · 2020-05-25 08:02

I had similar problem when migrating to ViewPager2.

In my case I decided to use parentFragment property (I think you can make it also work for activity) and hope, that ViewPager2 will keep only the current fragment resumed. (i.e. page fragment that was resumed last is the current one.)

So in my main fragment (HostFragment) that contains ViewPager2 view I created following property:

private var _currentPage: WeakReference<MyPageFragment>? = null
val currentPage
    get() = _currentPage?.get()

fun setCurrentPage(page: MyPageFragment) {
    _currentPage = WeakReference(page)
}

I decided to use WeakReference, so I don't leak inactive Fragment instances

And each of my fragments that I display inside ViewPager2 inherits from common super class MyPageFragment. This class is responsible for registering its instance with host fragment in onResume:

override fun onResume() {
    super.onResume()
    (parentFragment as HostFragment).setCurrentPage(this)
}

I also used this class to define common interface of paged fragments:

abstract fun someOperation1()

abstract fun someOperation2()

And then I can call them from the HostFragment like this:

currentPage?.someOperation1()

I'm not saying it's a nice solution, but I think it's more elegant than relying on internals of ViewPager's adapter with instantiateItem method that we had to use before.

查看更多
看我几分像从前
4楼-- · 2020-05-25 08:03

The ViewPagerAdapter is intended to hide all these implementation details which is why there is no straight-forward way to do it.

You could try setting and id or tag on the fragment when you instantiate it in getItem() then use fragmentManager.findFragmentById() or fragmentManager.findFragmentByTag() to retrieve.

Doing it like this, however, is a bit of a code smell. It suggests to me that stuff is being done in the activity when it should be done in the fragment (or elsewhere).

Perhaps there is another approach to achieve what you want but it's hard to give suggestions without knowing why you need to get the current fragment.

查看更多
劫难
5楼-- · 2020-05-25 08:05

I was able to get access to current fragment in FragmentStateAdapter using reflection.

Extension function in Kotlin:

fun FragmentStateAdapter.getItem(position: Int): Fragment? {
    return this::class.superclasses.find { it == FragmentStateAdapter::class }
        ?.java?.getDeclaredField("mFragments")
        ?.let { field ->
            field.isAccessible = true
            val mFragments = field.get(this) as LongSparseArray<Fragment>
            return@let mFragments[getItemId(position)]
        }
}

Add Kotlin reflection dependency if needed:

implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.61"

Example call:

val tabsAdapter = viewpager.adapter as FragmentStateAdapter
val currentFragment = tabsAdapter.getItem(viewpager.currentItem)
查看更多
成全新的幸福
6楼-- · 2020-05-25 08:13

I also had your problem and used this trick

    Map<Integer, Fragment> map = new HashMap<>();

@Override
        public Fragment createFragment(int position) {
            Fragment fragment;
            switch (position) {
                case 0:
                    fragment = new CircularFragment();
                    map.put(0, fragment);
                    return fragment;
                case 1:
                    fragment = new LocalFragment();
                    map.put(1, fragment);
                    return fragment;
                case 2:
                    fragment = new SettingsFragment();
                    map.put(2, fragment);
                    return fragment;
            }
            fragment = new CircularFragment();
            map.put(0, fragment);
            return fragment;
        }

and you can get fragment like this

 LocalFragment f = (LocalFragment) map.get(1);
查看更多
▲ chillily
7楼-- · 2020-05-25 08:16
supportFragmentManager.findFragmentByTag("f" + viewpager.currentItem)

with FragmentStateAdapter in placeFragmentInViewHolder(@NonNull final FragmentViewHolder holder)add Fragment

mFragmentManager.beginTransaction()
                    .add(fragment, "f" + holder.getItemId())
                    .setMaxLifecycle(fragment, STARTED)
                    .commitNow()
查看更多
登录 后发表回答