如何在 Android 上实现选项卡之间的滑动? [英] How do I implement swiping between tabs on Android?

查看:24
本文介绍了如何在 Android 上实现选项卡之间的滑动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 4.0 中针对标签的关键设计建议之一是允许滑动 在适当的地方.此行为使用户可以水平滑动选定选项卡的内容以导航到相邻选项卡,而无需直接与选项卡本身交互.

One of the key design recommendations in Android 4.0 for tabs is to allow swiping between them where appropriate. This behavior enables users to swipe horizontally across the selected tab's contents to navigate to adjacent tabs, without needed to directly interact with the tabs themselves.

如何实现?

推荐答案

注意:这是摘自 Android 培训实施有效导航.

NOTE: This is an excerpt from the Android Training class Implementing Effective Navigation.

要实现这一点(在 Android 3.0 或更高版本中),您可以使用 ViewPagerActionBar 选项卡 API 结合使用.

To implement this (in Android 3.0 or above), you can use a ViewPager in conjunction with the ActionBar tabs API.

观察当前页面变化后,选择相应的标签.您可以使用 ViewPager.OnPageChangeListener 在活动的onCreate() 方法:

Upon observing the current page changing, select the corresponding tab. You can set up this behavior using an ViewPager.OnPageChangeListener in your activity's onCreate() method:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    mViewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When swiping between pages, select the
                    // corresponding tab.
                    getActionBar().setSelectedNavigationItem(position);
                }
            });
    ...
}

在选择一个标签后,切换到 ViewPager.为此,请在使用 ActionBar.TabListener 创建标签时将code>newTab() 方法:

And upon selecting a tab, switch to the corresponding page in the ViewPager. To do this, add an ActionBar.TabListener to your tab when creating it using the newTab() method:

actionBar.newTab()
        ...
        .setTabListener(new ActionBar.TabListener() {
            public void onTabSelected(ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // When the tab is selected, switch to the
                // corresponding page in the ViewPager.
                mViewPager.setCurrentItem(tab.getPosition());
            }
            ...
        }));

这篇关于如何在 Android 上实现选项卡之间的滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆