简体   繁体   English

可滑动和可替换的选项卡,片段和ActionBar

[英]Swipable and replacable tabs, Fragments and ActionBar

As topic states I'm trying to implement swipable tabs using Fragments and ActionBar. 正如主题所述,我正在尝试使用Fragments和ActionBar实现可滑动选项卡。 I've iplemented it successfully using ActionBarSherlock and code from examples with the TabsAdapter class (subclass to FragmentPageAdapter). 我已经使用ActionBarSherlock和来自带有TabsAdapter类(FragmentPageAdapter的子类)的示例中的代码成功实现了它。 Some of the fragments should be able to show a new tab in the same tab spor, and this is where the problem arises. 某些片段应该能够在同一选项卡中显示一个新的选项卡,这就是问题所在。 I really can't figure out how to replace a fragment in the pager with a new one. 我真的不知道如何用新的替换寻呼机中的一个片段。 I've googled and tried out a lot of stuff. 我用谷歌搜索并尝试了很多东西。

I can replacie the fragment in the internal collection holding the fragments classes inside the FragmentPageAdapter, thus making the new Fragment available to the getItem method but that doesn't do it as I can't figure out how to update the pager to show the new Fragment. 我可以替换包含FragmentPageAdapter内的fragments类的内部集合中的fragment,从而使新的Fragment可用于getItem方法,但这不能实现,因为我不知道如何更新寻呼机以显示新的分段。 Even after switching to another Tab and going back I still get the old Fragment which I suppose is being cached somewhere by the pager. 即使切换到另一个Tab并返回后,我仍然会得到旧的Fragment,我认为它是由寻呼机缓存在某个地方的。 So is there a way to notify an update to the pager? 那么有没有办法通知寻呼机更新呢?

Maybe I should just forget using FragmentPageAdapter ? 也许我应该忘记使用FragmentPageAdapter? In that case what is the right way to get all this functionallity happen? 在那种情况下,实现所有这些功能的正确方法是什么?

PS If you want to see my code let me know, but it's basicly the same as in the examples and the problem is more general I think PS:如果您想查看我的代码,请告诉我,但是它与示例中的代码基本相同,并且我认为问题更笼统

When you use a ViewPager it keeps the visible page/fragment in memory in addition to pages/fragments that you can navigate to. 使用ViewPager时,除了可以浏览的页面/片段外,它ViewPager可见的页面/片段保留在内存中。 In addition to this, if the ViewPager is using a FragmentPagerApdapter it keeps the views in memory for the life of the activity. 除此之外,如果ViewPager使用FragmentPagerApdapter它将在活动期间将视图保留在内存中。

I answered a similar post to yours above removing a fragment which can be found here. 我回答了与您上面类似的帖子,删除了可以在此处找到的片段。 Your case is an extension of this, removing and then adding something else. 您的案例是对此的扩展,删除然后添加其他内容。

https://stackoverflow.com/a/10399127/629555 https://stackoverflow.com/a/10399127/629555

Basically, you need to use a FragmentStatePagerAdapter , override the getItemPosition method to return PagerAdapter.POSITION_NONE and make sure you call .notifyDataSetChanged(); 基本上,您需要使用FragmentStatePagerAdapter ,重写getItemPosition方法以返回PagerAdapter.POSITION_NONE并确保您调用.notifyDataSetChanged(); on your adapter when you want it changed. 在适配器上更改时。

The answer to the link above covers this in much more detail and provides a code example. 上面链接的答案对此进行了更详细的介绍,并提供了代码示例。

What about keeping a reference to the fragments in the adapter my self with the FragmentStatePagerAdapter ? 如何使用FragmentStatePagerAdapter自己对适配器中的片段进行引用呢? Then I have them both saved in the memory but also have full control of deletion. 然后,我既将它们保存在内存中,又可以完全控制删除。 Is there anything bad I'm missing with this approach and will I run into problems later on? 这种方法有什么不好的地方吗?以后我会遇到问题吗? Also I've changed so the fragments are passed on as objects and not classes, thus leaving the initiation outside the pager. 另外,我也进行了更改,以便将片段作为对象而不是类传递,从而将启动保留在寻呼机之外。 Any problems with this? 有什么问题吗?

Attaching my code of the adapter: 附上我的适配器代码:

public static class TabsAdapter extends FragmentStatePagerAdapter  implements
ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<Fragment> mTabs = new ArrayList<Fragment>();


    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Fragment fragment) {          
        tab.setTag(fragment);
        tab.setTabListener(this);
        mTabs.add(fragment);
        mActionBar.addTab(tab);


        notifyDataSetChanged();
    }           

    public void replaceTab(ActionBar.Tab tab, Fragment fragment, int position) {

        tab.setTag(fragment);
        tab.setTabListener(this);
        mTabs.set(position, fragment);
        notifyDataSetChanged(); 
    } 

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

    @Override
    public Fragment getItem(int position) {
        return mTabs.get(position);
    }

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

    @Override
    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

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

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

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