如何实现在Android上标签之间刷卡? [英] How do I implement swiping between tabs on Android?

查看:111
本文介绍了如何实现在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的培训类的 <一个href="http://developer.android.com/training/implementing-navigation/lateral.html#swipe-tabs">Implementing有效的导航 的。

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

要实现这个(的Andr​​oid 3.0或以上),您可以使用 ViewPager 动作条标签API一起使用。

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

当观察到当前页面的变化,选择相应的选项卡。您可以通过设置此行为的<一个href="http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html">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 。要做到这一点,添加一个<一个href="http://developer.android.com/reference/android/app/ActionBar.TabListener.html">ActionBar.TabListener你的标签时,创建它使用 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天全站免登陆