Replace Fragment inside a ViewPager

后端 未结 18 1586
别那么骄傲
别那么骄傲 2020-11-22 00:26

I\'m trying to use Fragment with a ViewPager using the FragmentPagerAdapter. What I\'m looking for to achieve is to replace a fragment, positioned

18条回答
  •  执笔经年
    2020-11-22 00:54

    I also made a solution, which is working with Stacks. It's a more modular approach so u don't have to specify each Fragment and Detail Fragment in your FragmentPagerAdapter. It's build on top of the Example from ActionbarSherlock which derives if I'm right from the Google Demo App.

    /**
     * This is a helper class that implements the management of tabs and all
     * details of connecting a ViewPager with associated TabHost.  It relies on a
     * trick.  Normally a tab host has a simple API for supplying a View or
     * Intent that each tab will show.  This is not sufficient for switching
     * between pages.  So instead we make the content part of the tab host
     * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
     * view to show as the tab content.  It listens to changes in tabs, and takes
     * care of switch to the correct paged in the ViewPager whenever the selected
     * tab changes.
     * 
     * Changed to support more Layers of fragments on each Tab.
     * by sebnapi (2012)
     * 
     */
    public class TabsAdapter extends FragmentPagerAdapter
            implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;
    
        private ArrayList mTabTags = new ArrayList();
        private HashMap> mTabStackMap = new HashMap>();
    
        static final class TabInfo {
            public final String tag;
            public final Class clss;
            public Bundle args;
    
            TabInfo(String _tag, Class _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }
    
        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;
    
            public DummyTabFactory(Context context) {
                mContext = context;
            }
    
            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }
    
        public interface SaveStateBundle{
            public Bundle onRemoveFragment(Bundle outState);
        }
    
        public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }
    
        /**
         * Add a Tab which will have Fragment Stack. Add Fragments on this Stack by using
         * addFragment(FragmentManager fm, String _tag, Class _class, Bundle _args)
         * The Stack will hold always the default Fragment u add here.
         * 
         * DON'T ADD Tabs with same tag, it's not beeing checked and results in unexpected
         * beahvior.
         * 
         * @param tabSpec
         * @param clss
         * @param args
         */
        public void addTab(TabHost.TabSpec tabSpec, Class clss, Bundle args){
            Stack tabStack = new Stack();
    
            tabSpec.setContent(new DummyTabFactory(mContext));
            mTabHost.addTab(tabSpec);
            String tag = tabSpec.getTag();
            TabInfo info = new TabInfo(tag, clss, args);
    
            mTabTags.add(tag);                  // to know the position of the tab tag 
            tabStack.add(info);
            mTabStackMap.put(tag, tabStack);
            notifyDataSetChanged();
        }
    
        /**
         * Will add the Fragment to Tab with the Tag _tag. Provide the Class of the Fragment
         * it will be instantiated by this object. Proivde _args for your Fragment.
         * 
         * @param fm
         * @param _tag
         * @param _class
         * @param _args
         */
        public void addFragment(FragmentManager fm, String _tag, Class _class, Bundle _args){
            TabInfo info = new TabInfo(_tag, _class, _args);
            Stack tabStack = mTabStackMap.get(_tag);   
            Fragment frag = fm.findFragmentByTag("android:switcher:" + mViewPager.getId() + ":" + mTabTags.indexOf(_tag));
            if(frag instanceof SaveStateBundle){
                Bundle b = new Bundle();
                ((SaveStateBundle) frag).onRemoveFragment(b);
                tabStack.peek().args = b;
            }
            tabStack.add(info);
            FragmentTransaction ft = fm.beginTransaction();
            ft.remove(frag).commit();
            notifyDataSetChanged();
        }
    
        /**
         * Will pop the Fragment added to the Tab with the Tag _tag
         * 
         * @param fm
         * @param _tag
         * @return
         */
        public boolean popFragment(FragmentManager fm, String _tag){
            Stack tabStack = mTabStackMap.get(_tag);   
            if(tabStack.size()>1){
                tabStack.pop();
                Fragment frag = fm.findFragmentByTag("android:switcher:" + mViewPager.getId() + ":" + mTabTags.indexOf(_tag));
                FragmentTransaction ft = fm.beginTransaction();
                ft.remove(frag).commit();
                notifyDataSetChanged();
                return true;
            }
            return false;
        }
    
        public boolean back(FragmentManager fm) {
            int position = mViewPager.getCurrentItem();
            return popFragment(fm, mTabTags.get(position));
        }
    
        @Override
        public int getCount() {
            return mTabStackMap.size();
        }
    
        @Override
        public int getItemPosition(Object object) {
            ArrayList> positionNoneHack = new ArrayList>();
            for(Stack tabStack: mTabStackMap.values()){
                positionNoneHack.add(tabStack.peek().clss);
            }   // if the object class lies on top of our stacks, we return default
            if(positionNoneHack.contains(object.getClass())){
                return POSITION_UNCHANGED;
            }
            return POSITION_NONE;
        }
    
        @Override
        public Fragment getItem(int position) {
            Stack tabStack = mTabStackMap.get(mTabTags.get(position));
            TabInfo info = tabStack.peek();
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }
    
        @Override
        public void onTabChanged(String tabId) {
            int position = mTabHost.getCurrentTab();
            mViewPager.setCurrentItem(position);
        }
    
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }
    
        @Override
        public void onPageSelected(int position) {
            // Unfortunately when TabHost changes the current tab, it kindly
            // also takes care of putting focus on it when not in touch mode.
            // The jerk.
            // This hack tries to prevent this from pulling focus out of our
            // ViewPager.
            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
        }
    
    }
    

    Add this for back button functionality in your MainActivity:

    @Override
    public void onBackPressed() {
      if (!mTabsAdapter.back(getSupportFragmentManager())) {
        super.onBackPressed();
      }
    }
    

    If u like to save the Fragment State when it get's removed. Let your Fragment implement the interface SaveStateBundle return in the function a bundle with your save state. Get the bundle after instantiation by this.getArguments().

    You can instantiate a tab like this:

    mTabsAdapter.addTab(mTabHost.newTabSpec("firstTabTag").setIndicator("First Tab Title"),
                    FirstFragmentActivity.FirstFragmentFragment.class, null);
    

    works similiar if u want to add a Fragment on top of a Tab Stack. Important: I think, it won't work if u want to have 2 instances of same class on top of two Tabs. I did this solution quick together, so I can only share it without providing any experience with it.

提交回复
热议问题