Retrieve a Fragment from a ViewPager

前端 未结 23 2871
逝去的感伤
逝去的感伤 2020-11-21 11:13

I\'m using a ViewPager together with a FragmentStatePagerAdapter to host three different fragments:

  • [Fragment1]
  • [Fragment2]<
23条回答
  •  深忆病人
    2020-11-21 11:44

    in TabLayout there are multiple tab for Fragment. you can find the fragment by Tag using the index of the fragment.

    For ex. the index for Fragment1 is 0, so in findFragmentByTag() method, pass the tag for the Viewpager.after using fragmentTransaction you can add,replace the fragment.

    String tag = "android:switcher:" + R.id.viewPager + ":" + 0; Fragment1 f = (Fragment1) getSupportFragmentManager().findFragmentByTag(tag);

提交回复
热议问题