1

I am trying to keep a view pager inside a fragment. And the View pager itself contains 3 other fragment. This is the Root fragment which contain the view pager

import android.support.v13.app.FragmentPagerAdapter;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

import com.android.browser1.UI.ComboViews;


public class MyFragment extends Fragment implements
        TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener, CombinedBookmarksCallbacks {
    private TabHost mTabHost;
    private ViewPager mViewPager;
    public static final String EXTRA_COMBO_ARGS = "combo_args";
    Bundle bundle;
    Bundle extra;
    public static final String EXTRA_INITIAL_VIEW = "initial_view";
    public static BookmarkFragment bookmarkFragmentForPageA = null;
    public static BookmarkFragment bookmarkFragmentForPageB = null;
    Controller controller;
    TabsAdapter mTabsAdapter;
    FirstFragment first;
    SecondFragment seconde;
    ThirdFragment third;

    public void setBundle(Bundle bundle) {
        extra = bundle;// getActivity().getIntent().getExtras();
        this.bundle = extra.getBundle(EXTRA_COMBO_ARGS);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mTabHost.setup();
        mViewPager.setOffscreenPageLimit(2);
        mTabsAdapter = new TabsAdapter(getActivity(), mTabHost, mViewPager);

        new setAdapterTask().execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bookmark_view_pager, null);
        mTabHost = (TabHost) view.findViewById(R.id.tabHost);
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        view.setBackgroundColor(Color.BLACK);
        return view;
    }

    private class setAdapterTask extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            String bookmark = getResources().getString(R.string.tab_bookmarks);
            String history = getResources().getString(R.string.tab_history);
            String scrapmemo = getResources().getString(R.string.tab_snapshots);
            mTabsAdapter.addTab(
                    mTabHost.newTabSpec(bookmark).setIndicator(bookmark), BrowserBookmarksPage.class, null);
            mTabsAdapter.addTab(
                    mTabHost.newTabSpec(history).setIndicator(history), BrowserHistoryPage.class, null);
            mTabsAdapter.addTab(
                    mTabHost.newTabSpec(scrapmemo).setIndicator(scrapmemo), BrowserSnapshotPage.class, null);
            String svStr = extra.getString(EXTRA_INITIAL_VIEW, null);
            ComboViews startingView = svStr != null ? ComboViews.valueOf(svStr)
                    : ComboViews.Bookmarks;
            switch (startingView) {
            case Bookmarks:
                mTabHost.setCurrentTab(0);
                mViewPager.setCurrentItem(0);
                break;
            case History:
                mTabHost.setCurrentTab(1);
                mViewPager.setCurrentItem(1);
                break;
            case ScrapMemo:
                mTabHost.setCurrentTab(2);
                mViewPager.setCurrentItem(2);
                break;
            }
        }
    }

    public void onTabChanged(String tag) {
        ComboViews startingView = tag != null ? ComboViews.valueOf(tag)
                : ComboViews.Bookmarks;
        switch (startingView) {
        case First:
            mTabHost.setCurrentTab(0);
            break;
        case Second:
            mTabHost.setCurrentTab(1);
            break;
        case Third:
            mTabHost.setCurrentTab(2);
            break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        this.mTabHost.setCurrentTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    public class TabsAdapter extends FragmentStatePagerAdapter
            implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        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 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 TabsAdapter(Activity activity, TabHost tabHost, ViewPager pager) {
            super(activity.getFragmentManager());
            mContext = activity;
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
            mViewPager.setOffscreenPageLimit(3);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mContext));
            String tag = tabSpec.getTag();
            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);
            mTabHost.addTab(tabSpec);
            notifyDataSetChanged();
            getItem(0);
        }

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

        @Override
        public Fragment getItem(int position) {
            Fragment fr = null;
            TabInfo info = mTabs.get(position);
            // Create a new fragment if necessary.
            switch (position) {
            case 0:
                fr = new FirstFragment();
                fr.setArguments(info.args);
                break;
            case 1:
                fr = new SecondFragment ();
                fr.setArguments(info.args);
                break;
            case 2:
                fr = new ThirdFragment ();
                fr.setArguments(info.args);
                break;
            default:
                fr = new FirstFragment ();
                fr.setArguments(info.args);
                break;
            }

            return fr; 
        }

        @Override
        public int getItemPosition(Object object) {
            if (object instanceof FirstFragment 
                    || object instanceof SecondFragment 
                    || object instanceof ThirdFragment )
                return POSITION_NONE;
            return POSITION_UNCHANGED;
        }

        @Override
        public void onTabChanged(String tabId) {
            // called when the user clicks on a tab.
            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) {
        }
    }
}

And when I attach this fragment in my activity for the first time we can see all the three fragment by swiping but after the back press we remove the fragment. After that again I attach the fragment in the same activity at run time but only first 2 fragments are visible and the content of 3rd one is empty.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Deepak Goel
  • 5,624
  • 6
  • 39
  • 53

1 Answers1

1

I am trying to keep a view pager inside a fragment. And the View pager itself contains 3 other fragment.

Sorry, but fragments inside of other fragments is officially not supported, as indicated by Dianne Hackborn, a core Android team member.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Several questions are available on this topic and the code i have provided is also displaying the fragment correctly for the first time. So disagree with your view – Deepak Goel Mar 25 '12 at 11:31
  • Possibly the above code may be the hack but it is working and apart from that the behavior is different when i press the back button then the third fragment UI is black and other two are still visible. – Deepak Goel Mar 25 '12 at 11:40