Retrieve a Fragment from a ViewPager

前端 未结 23 2514
逝去的感伤
逝去的感伤 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:58

    Must extends FragmentPagerAdapter into your ViewPager adapter class.
    If you use FragmentStatePagerAdapter then you will not able to find your Fragment by its ID

    public static String makeFragmentName(int viewPagerId, int index) {
      return "android:switcher:" + viewPagerId + ":" + index;
    }
    

    How to use this method :-

    Fragment mFragment = ((FragmentActivity) getContext()).getSupportFragmentManager().findFragmentByTag(
           AppMethodUtils.makeFragmentName(mViewPager.getId(), i)
    );
    InterestViewFragment newFragment = (InterestViewFragment) mFragment;
    
    0 讨论(0)
  • 2020-11-21 11:58

    Hey I have answered this question here. Basically, you need to override

    public Object instantiateItem(ViewGroup container, int position)

    method of FragmentStatePagerAdapter.

    0 讨论(0)
  • 2020-11-21 11:59

    I handled it by first making a list of all the fragments (List<Fragment> fragments;) that I was going to use then added them to the pager making it easier to handle the currently viewed fragment.

    So:

    @Override
    onCreate(){
        //initialise the list of fragments
        fragments = new Vector<Fragment>();
    
        //fill up the list with out fragments
        fragments.add(Fragment.instantiate(this, MainFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, MenuFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, StoresFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, AboutFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ContactFragment.class.getName()));
    
    
        //Set up the pager
        pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        pager.setOffscreenPageLimit(4);
    }
    

    so then this can be called:

    public Fragment getFragment(ViewPager pager){   
        Fragment theFragment = fragments.get(pager.getCurrentItem());
        return theFragment;
    }
    

    so then i could chuck it in an if statement that would only run if it was on the correct fragment

    Fragment tempFragment = getFragment();
    if(tempFragment == MyFragmentNo2.class){
        MyFragmentNo2 theFrag = (MyFragmentNo2) tempFragment;
        //then you can do whatever with the fragment
        theFrag.costomFunction();
    }
    

    but thats just my hack and slash approach but it worked for me, I use it do do relevent changes to my currently displayed fragment when the back button is pushed.

    0 讨论(0)
  • 2020-11-21 11:59

    I couldn't find a simple, clean way to do this. However, the ViewPager widget is just another ViewGroup , which hosts your fragments. The ViewPager has these fragments as immediate children. So you could just iterate over them (using .getChildCount() and .getChildAt() ), and see if the fragment instance that you're looking for is currently loaded into the ViewPager and get a reference to it. E.g. you could use some static unique ID field to tell the fragments apart.

    Note that the ViewPager may not have loaded the fragment you're looking for since it's a virtualizing container like ListView.

    0 讨论(0)
  • 2020-11-21 11:59

    Easy way to iterate over fragments in fragment manager. Find viewpager, that has section position argument, placed in public static PlaceholderFragment newInstance(int sectionNumber).

    public PlaceholderFragment getFragmentByPosition(Integer pos){
        for(Fragment f:getChildFragmentManager().getFragments()){
            if(f.getId()==R.id.viewpager && f.getArguments().getInt("SECTNUM") - 1 == pos) {
                return (PlaceholderFragment) f;
            }
        }
        return null;
    }
    
    0 讨论(0)
提交回复
热议问题