简体   繁体   English

从内部片段更新FragmentPagerAdapter

[英]Update FragmentPagerAdapter from an inside Fragment

I am using a custom FragmentPagerAdapter that in the beggining has a certain number of fragments in it by using a tabHost: 我使用的是一个自定义FragmentPagerAdapter,通过使用tabHost,它在开始时就有一定数量的片段:

public class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
    {
        private final Context mcon;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();


        final class TabInfo
        {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(String _tag, Class<?> _class, Bundle _args)
            {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        class DummyTabFactory implements TabHost.TabContentFactory
        {
            private final Context mcon;

            public DummyTabFactory(Context context)
            {
                mcon = context;
            }

            public View createTabContent(String tag)
            {
                View v = new View(mcon);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabsAdapter(Fragment fragment, TabHost tabHost, ViewPager pager) {
            super(fragment.getChildFragmentManager());
            mcon = fragment.getActivity();
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
        {
            tabSpec.setContent(new DummyTabFactory(mcon));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);


            notifyDataSetChanged();
            mTabHost.addTab(tabSpec);
        }

        @Override
        public int getCount()
        {
            return mTabs.size();
        }


        @Override
        public Fragment getItem(int position)
        {
            TabInfo info = mTabs.get(position);

            return Fragment.instantiate(mcon, info.clss.getName(), info.args);

        }

        public void onTabChanged(String tabId)
        {
            int position = mTabHost.getCurrentTab();
            mViewPager.setCurrentItem(position);


        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            View tabView = mTabHost.getTabWidget().getChildAt(position);
            if (tabView != null)
            {
                final int width = mHorizontalScroll.getWidth();
                final int scrollPos = tabView.getLeft() - (width - tabView.getWidth()) / 2;
                mHorizontalScroll.scrollTo(scrollPos, 0);
            } else {
                mHorizontalScroll.scrollBy(positionOffsetPixels, 0);
            }

        }

        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);
        }

        public void onPageScrollStateChanged(int state)
        {
        }
    }

Every fragment is created in the beggining by: 每个片段都是通过以下内容创建的:

mTabsAdapter.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab1")), InsideFragment.class, gargs);

What I want to do is to update the number of fragments (add/remove fragment) from inside one of the InsideFragments, after it has been initialized. 我想做的是在初始化之后从InsideFragments之一内部更新片段的数量(添加/删除片段)。 How could I do that? 我该怎么办?

I didn't test it but maybe you could pass an interface from the MainFragment to the ChildFragment . 我没有测试它,但是也许您可以将MainFragment的接口MainFragmentChildFragment Then in the ChildFragment you would call a method of this interface like, listener.refreshParent() . 然后,在ChildFragment您将调用此interface的方法,如listener.refreshParent()

Take a look at this answer: https://stackoverflow.com/a/18585247/3465623 看看这个答案: https : //stackoverflow.com/a/18585247/3465623

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM