简体   繁体   English

动作栏标签和ViewPager中的片段

[英]Fragment in Actionbar Tab and ViewPager

Is it possible to use a Fragment in both tablet Actionbar Tabs and phone ViewPager? 平板电脑的“操作栏”选项卡和手机ViewPager都可以使用“片段”吗? It appears that the phone uses the support library and that isn't compatible with the TabListener in the Actionbar. 手机似乎使用了支持库,并且与操作栏中的TabListener不兼容。 Is there a trick to getting both to use the same Fragment? 是否有使两者都使用相同片段的技巧?

The only possible workaround I've seen so far is this . 唯一可能的解决方法到目前为止,我所看到的是这个

Thanks 谢谢

Sure, check out the ActionBarTabsPager sample in the compatibility library source. 当然,请在兼容性库源代码中签出ActionBarTabsPager示例。 You need to implement both interfaces in your adapter to handle tab switching and paging. 您需要在适配器中实现两个接口,以处理选项卡切换和页面调度。 Something like this: 像这样的东西:

public class TabsPagerAdapter extends FragmentPagerAdapter implements
        ViewPager.OnPageChangeListener, ActionBar.TabListener {
        // implement both
}

You need to add tabs like this (using the support/compatibility library): 您需要添加以下标签(使用支持/兼容性库):

getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

viewPager = (ViewPager) findViewById(R.id.content);
tabsAdapter = new TabsPagerAdapter(this, getSupportActionBar(),
                viewPager);
ActionBar.Tab tab = getSupportActionBar().newTab();
MyFragment f = new MyFragment();
tabsAdapter.addTab(tab, f);

http://developer.android.com/training/backward-compatible-ui/index.html http://developer.android.com/training/backward-compatible-ui/index.html

This is the good way to use Fragment from compatibility library with ActionBar.Tab and TabListener. 这是将兼容库中的Fragment与ActionBar.Tab和TabListener一起使用的好方法。

The idea is to use android-support-v13.jar. 这个想法是使用android-support-v13.jar。 This file includes FragmentPagerAdapter for native fragments as well as ViewPager. 该文件包括用于本机片段的FragmentPagerAdapter以及ViewPager。

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

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