如何实现在Android上标签之间刷卡?如何实现、标签、Android

2023-09-12 07:05:11 作者:君踏桃花归

一个在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的培训类的 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.

当观察到当前页面的变化,选择相应的选项卡。您可以通过设置此行为的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你的标签时,创建它使用 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());
            }
            ...
        }));