当按下并重新打开后,片段不会在viewpager中恢复其状态

时间:2012-03-25 10:24:42

标签: android

我正在尝试将视图寻呼机保留在片段中。 View寻呼机本身包含3个其他片段。这是包含视图分页器

的Root片段
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) {
        }
    }
}

当我第一次将这个片段附加到我的活动中时,我们可以通过滑动看到所有三个片段,但在背面按下后我们删除片段。之后,我再次在运行时将片段附加到相同的活动中,但只有前2个片段可见,第3个片段的内容为空。

1 个答案:

答案 0 :(得分:1)

  

我正在尝试将视图寻呼机保留在片段中。 View寻呼机本身包含3个其他片段。

抱歉,其他片段中的片段是officially not supported, as indicated by Dianne Hackborn, a core Android team member