Getting the current Fragment instance in the viewpager

前端 未结 30 1916
醉话见心
醉话见心 2020-11-22 08:56

Below is my code which has 3 Fragment classes each embedded with each of the 3 tabs on ViewPager. I have a menu option. As shown in the onOpt

相关标签:
30条回答
  • 2020-11-22 09:20

    To get current fragment - get position in ViewPager at public void onPageSelected(final int position), and then

    public PlaceholderFragment getFragmentByPosition(Integer pos){
        for(Fragment f:getChildFragmentManager().getFragments()){
            if(f.getId()==R.viewpager && f.getArguments().getInt("SECTNUM") - 1 == pos) {
                return (PlaceholderFragment) f;
            }
        }
        return null;
    }
    

    SECTNUM - position argument assigned in public static PlaceholderFragment newInstance(int sectionNumber); of Fragment

    getChildFragmentManager() or getFragmentManager() - depends on how created SectionsPagerAdapter

    0 讨论(0)
  • 2020-11-22 09:21

    I know its too late but I have really simple ways of doing it,

    // for fragment at 0 possition

    ((mFragment) viewPager.getAdapter().instantiateItem(viewPager, 0)).yourMethod();
    
    0 讨论(0)
  • 2020-11-22 09:21

    You can declare an Array of fragment as register fragments

    class DashboardPagerAdapter(fm: FragmentManager?) : FragmentStatePagerAdapter(fm!!) {
        // CURRENT FRAGMENT
        val registeredFragments = SparseArray<Fragment>()
    
        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val fragment = super.instantiateItem(container, position) as Fragment
            registeredFragments.put(position, fragment)
            return fragment
        }
    
        override fun getItem(position: Int): Fragment {
            return when (position) {
                0 -> HomeFragment.newInstance()
                1 -> ConverterDashboardFragment.newInstance()
                2 -> CartFragment.newInstance()
                3 -> CustomerSupportFragment.newInstance()
                4 -> ProfileFragment.newInstance()
                else -> ProfileFragment.newInstance()
            }
        }
    
        override fun getCount(): Int {
            return 5
        }
    
    }
    

    Then you can use it as

    adapter?.let {
        val cartFragment = it.registeredFragments[2] as CartFragment?
        cartFragment?.myCartApi(true)
    }
    
    0 讨论(0)
  • 2020-11-22 09:22

    I have used the following:

     int index = vpPager.getCurrentItem();
     MyPagerAdapter adapter = ((MyPagerAdapter)vpPager.getAdapter());
     MyFragment suraVersesFragment = (MyFragment)adapter.getRegisteredFragment(index);
    
    0 讨论(0)
  • 2020-11-22 09:23

    In my Activity I have:

    int currentPage = 0;//start at the first tab
    private SparseArray<Fragment> fragments;//list off fragments
    viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    
    @Override
    public void onPageSelected(int pos) {
            currentPage = pos;//update current page
    }
    
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {}
    @Override
    public void onPageScrollStateChanged(int arg0) {}
    });
    
    
    
    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if(fragment instanceof Fragment1)
            fragments.put(0, fragment);
        if(fragment instanceof Fragment2)
            fragments.put(2, fragment);
        if(fragment instanceof Fragment3)
            fragments.put(3, fragment);
        if(fragment instanceof Fragment4)
            fragments.put(4, fragment);
    }
    
    Then I have the following method for getting the current fragment
    
    public Fragment getCurrentFragment() {
        return fragments.get(currentPage);
    }
    
    0 讨论(0)
  • 2020-11-22 09:23

    Simply get the current item from pager and then ask your adapter to the fragment of that position.

     int currentItem = viewPager.getCurrentItem();
        Fragment item = mPagerAdapter.getItem(currentItem);
        if (null != item && item.isVisible()) {
           //do whatever want to do with fragment after doing type checking
            return;
        }
    
    0 讨论(0)
提交回复
热议问题