Jam*_*ade 14 tabs android actionbarsherlock android-actionbar
我一直在寻找有关如何始终在ActionBar下方显示ActionBar选项卡的任何信息,即使手机朝向横向也是如此.我知道它会自动将标签放在ActionBar中,具体取决于屏幕上是否有足够的空间来容纳它们,但我希望它们始终固定在ActionBar下面,即使在横向上也是如此.我发现了一个类似的问题没有明确答案:如何在操作栏下方显示标签.我也知道很多谷歌应用程序,如; Google Play,Google Music等实现了这种类型的设计模式,因此它显然是可以实现的,并且可以作为设计模式使用.
我目前正在使用ActionBarSherlock库来创建我的滑动标签导航,如果有人知道如何自己做这件事,我真的很感激.提前致谢.
这是我的活动代码:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
setContentView(viewPager);
tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view
actionBarTabs = getSupportActionBar();
actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBarTabs.setCustomView(spinnerView);
actionBarTabs.setDisplayShowCustomEnabled(true);
/* Adds fragments to the tabs adapter */
tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG1"), Fragment_1.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG2"), Fragment_2.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG3"), Fragment_3.class, null);
}
Run Code Online (Sandbox Code Playgroud)
这是我的TabsAdapter代码:
public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener , ViewPager.OnPageChangeListener
{
private final Context context;
private final ActionBar actionBar;
private final ViewPager viewPager;
private final ArrayList<TabInfo> tabsList = new ArrayList<TabInfo>();
/**
* TabInfo class
*
* Static class containing the tab information.
*
* @since 1.0
*/
static final class TabInfo
{
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args)
{
clss = _class;
args = _args;
}
}
/**
* Tabs adapter overload constructor.
*
* @param fragmentActivity sets the fragment activity to the adapter.
* @param refViewPager sets the viewPager variable.
* @see
* @since 1.0
*/
public TabsAdapter(SherlockFragmentActivity fragmentActivity, ViewPager refViewPager)
{
super(fragmentActivity.getSupportFragmentManager());
context = fragmentActivity;
actionBar = fragmentActivity.getSupportActionBar();
viewPager = refViewPager;
viewPager.setAdapter(this);
viewPager.setOnPageChangeListener(this);
}
/**
* Add tab method to add a tab to the list.
*
* @param tab sets the tab to be added to the tabs in the action bar.
* @param clss sets the class variable in the TabInfo class.
* @param args sets the bundle variable in the TabInfo class.
* @see
* @since 1.0
*/
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args)
{
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
tabsList.add(info);
actionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state)
{
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
@Override
public void onPageSelected(int position)
{
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
viewPager.setCurrentItem(tab.getPosition());
Object tag = tab.getTag();
for (int i = 0; i<tabsList.size(); i++)
{
if (tabsList.get(i) == tag)
{
viewPager.setCurrentItem(i);
}
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
}
@Override
public Fragment getItem(int position)
{
TabInfo info = tabsList.get(position);
return Fragment.instantiate(context, info.clss.getName(), info.args);
}
@Override
public int getCount()
{
return tabsList.size();
}
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ang 12
我在横向模式下遇到了同样的问题,我在这里找到了解决方案:http://andreimihu.com/blog/2013/10/17/android-always-embed-tabs-in-actionbar/
基本上,当你和我想在外面时,作者想要操纵栏内的选项卡.所以,只需将方法调用更改为false(来自上面链接的代码,但有点修改):
// This is where the magic happens!
public void forceTabs() {
try {
final ActionBar actionBar = getActionBar();
final Method setHasEmbeddedTabsMethod = actionBar.getClass()
.getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
setHasEmbeddedTabsMethod.setAccessible(true);
setHasEmbeddedTabsMethod.invoke(actionBar, false);
}
catch(final Exception e) {
// Handle issues as needed: log, warn user, fallback etc
// This error is safe to ignore, standard tabs will appear.
}
}
Run Code Online (Sandbox Code Playgroud)
首先,您所描述的“许多 Google 应用程序,例如 Google Play、Google Music 等都实现了这种类型的设计模式”不一定是与 ActionBar 相关的导航选项卡。
有很多使用带有标题指示器的viewPager 的实现。请参阅PagerSlidingTabStrip库以获得良好的实现。
另外,您应该查看官方 Android 文档,其中深入讨论了选项卡导航并显示了获取它的不同方法。检查文档中的这些页面:提供向下和横向导航以及使用选项卡创建滑动视图
此外, stackoverflow.com上有很多关于此主题的答案。我建议的库已经在这个答案中提到: https: //stackoverflow.com/a/16544501/529138。
检查此问题和相关答案:操作栏下方的选项卡
这些资源应该足以帮助您实现所需的功能:)
| 归档时间: |
|
| 查看次数: |
8691 次 |
| 最近记录: |