强制ViewPager在上升后台时调用getItem

时间:2015-06-03 11:14:02

标签: android android-fragments tabs android-viewpager fragmentstatepageradapter

我正在尝试创建一个包含带容器和导航抽屉的单个活动的Android应用程序。初始化的空容器加载片段,片段布局中有一个ViewPager,我在其中加载了一个带有FragmentTransaction的片段:

public static void replaceFragmentInContainer(FragmentManager fragmentManager, Fragment fragmentToShow,
        boolean addToBackStack)
{
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    if (addToBackStack)
    {
        transaction.addToBackStack(null);
    }
    transaction.replace(R.id.container, fragmentToShow);
    transaction.commit();
}

我面临的问题是背靠背。当一个片段被添加到backstack并且我按下后退按钮时,应用程序会像我想要的那样返回到上一个片段,我确实看到了标签布局本身,但是标签的内容是空的,就像没有加载任何内容一样到那个标签。当它发生时,我只能在使用导航抽屉再次选择该屏幕时重新加载标签的内容。

调试后,我看到按下后退按钮时没有调用寻呼机适配器的getItem方法。适配器本身是FragmentStatePagerAdapter

我尝试重写getItemPosition方法:

public int getItemPosition(Object object) 
{
    return POSITION_NONE;
}

但是按下后退按钮时没有调用该方法,所以我最终看到空标签。

这是标签适配器,它也是ViewPager适配器:

public static 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>();

    public TabsAdapter(final FragmentActivity activity, final TabHost tabHost, final ViewPager pager)
    {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mTabHost = tabHost;
        mViewPager = pager;
        mTabHost.setOnTabChangedListener(this);
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public ViewPager getViewPager()
    {
        return mViewPager;
    }

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

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

    static class TabFactory implements TabHost.TabContentFactory
    {
        private final Context mContext;

        public TabFactory(final Context context)
        {
            mContext = context;
        }

        @Override
        public View createTabContent(final String tag)
        {
            final View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

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

        final TabInfo info = new TabInfo(tag, clss, args);
        mTabs.add(info);
        mTabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

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

    @Override
    public Fragment getItem(final int position)
    {
        final TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    // @Override
    // public Object instantiateItem(ViewGroup container, int position)
    // {
    //
    // // Inflate a new layout from the resources.
    // View view = ((Activity) mContext).getLayoutInflater().inflate(
    // R.layout.fragment_single_conversation, container, false);
    // // Add the newly created View to the ViewPager
    // container.addView(view);
    //
    // // Populate the GUI and views now
    // return view;
    // }


    @Override
    public int getItemPosition(Object object)
    {
        return POSITION_NONE;
    }

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

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

    @Override
    public void onPageSelected(final 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.
        final TabWidget widget = mTabHost.getTabWidget();
        final int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        mTabHost.setCurrentTab(position);
        widget.setDescendantFocusability(oldFocusability);
    }

    @Override
    public void onPageScrollStateChanged(final int state)
    {
    }
}

如何解决这个问题?

0 个答案:

没有答案