从内部片段

时间:2015-10-30 09:24:23

标签: android fragment fragmentpageradapter

我正在使用一个自定义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)
        {
        }
    }

每个片段都是在开始时创建的:

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

我想要做的是在初始化之后更新其中一个InsideFragments内的片段数量(添加/删除片段)。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我没有对它进行测试,但也许你可以将界面从MainFragment传递到ChildFragment。然后在ChildFragment中,您可以调用此interface的方法,listener.refreshParent()

看一下这个答案:https://stackoverflow.com/a/18585247/3465623