java Android Tabs + Sliding,如何实现 ActionBarSherlock?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12202834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:01:44  来源:igfitidea点击:

Android Tabs + Sliding, how do I implement ActionBarSherlock?

javaandroidandroid-actionbaractionbarsherlock

提问by EGHDK

I'm using Google's new tools to start an application that has the ability to switch between three tabs.

我正在使用 Google 的新工具来启动一个能够在三个选项卡之间切换的应用程序。

enter image description here

在此处输入图片说明

This is great, but lacks support for older devices.

这很棒,但缺乏对旧设备的支持。

1.I added ABS with the support library to the application.

1.我在应用程序中添加了带有支持库的 ABS。

2.I changed public class MainActivity extends FragmentActivity implements ActionBar.TabListener {to public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {

2.我改变了public class MainActivity extends FragmentActivity implements ActionBar.TabListener {public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {

3.I'm still left with a ton of errors, and I don't even know if this will work properly on older devices. Does anyone have any tips on how to implement sliding tabs that are compatible with 2.x and up?

3.我仍然有很多错误,我什至不知道这是否能在旧设备上正常工作。有没有人有关于如何实现与 2.x 及更高版本兼容的滑动选项卡的任何提示?

enter image description here

在此处输入图片说明

Update:

更新:

I'm stuck on step 6 of alextsc's answer enter image description here

我被困在 alexsc 答案的第 6 步 在此处输入图片说明

采纳答案by Amol Gupta

The exact same process worked for me. I removed all the imports and then pressed ctrl+shift+o and selected the compatibility classes. It worked absoulutely fine. see the post here.

完全相同的过程对我有用。我删除了所有导入,然后按 ctrl+shift+o 并选择了兼容性类。它工作得很好。请参阅此处的帖子。

回答by

I tried this wizard once and I think I threw away the generated code completely when I implemented this exact pattern with ActionBarSherlock, so I suggest you start with a normal "Blank" activity from scratch. Here is a small step-by-step guide. Not all steps are completely described, but you should find enough documentation with the keywords to get started.

我曾经尝试过这个向导,当我用 ActionBarSherlock 实现这个精确模式时,我想我完全抛弃了生成的代码,所以我建议你从头开始一个正常的“空白”活动。这是一个小的分步指南。并非所有步骤都得到完整描述,但您应该找到足够的包含关键字的文档以开始使用。

1) Add ActionBarSherlock to your project (obviously)

1) 将 ActionBarSherlock 添加到您的项目中(显然)

2) Create a new activity that extends SherlockFragmentActivityand set a proper abs theme

2)创建一个新的活动,扩展SherlockFragmentActivity并设置一个合适的 abs 主题

You should have a blank activity with an action bar at this point.

此时您应该有一个带有操作栏的空白活动。

3) Change the layout and include a ViewPager that fills the viewport

3) 更改布局并包含一个填充视口的 ViewPager

4) Write your fragments (or placeholders for now) and the adapter for the ViewPager, wire these together

4)编写您的片段(或现在的占位符)和 ViewPager 的适配器,将它们连接在一起

There are a lot of tutorials out there that explain everything neccessary here, e.g. this blog post.

有很多教程可以解释这里所有必要的内容,例如这篇博客文章

This should give you an activity with an action bar and a swipable layout. You can swipe between your fragments now.

这应该为您提供一个带有操作栏和可滑动布局的活动。您现在可以在片段之间滑动。

5) Add action bar tabs and attach a blank tab listener to them

5) 添加操作栏选项卡并为其附加一个空白选项卡侦听器

Example:

例子:

actionBar = getSupportActionBar();

sampleTab = actionBar.newTab()
    .setText(R.string.title)
    .setTag(TABTAG_SAMPLE)
    .setTabListener(tabListener);

actionBar.addTab(sampleTab);

Make sure that you give each of your tabs an individual tag (a string const is fine). This will be used to identify which tab is clicked in a second. Also make sure that you keep the created tab instances in a class variable. You will need them later on. Repeat the above snippet for each tab. You can use a normal TabListener, but I recomment using the SimpleTabListener since you only need to override one method later.

确保为每个选项卡提供一个单独的标签(字符串 const 很好)。这将用于识别一秒钟内单击了哪个选项卡。还要确保将创建的选项卡实例保留在类变量中。稍后您将需要它们。对每个选项卡重复上述代码段。您可以使用普通的 TabListener,但我建议使用 SimpleTabListener,因为您以后只需要覆盖一个方法。

Now you should have an activity with the action bar, swipeable fragments and (non-functional) tabs.

现在您应该有一个带有操作栏、可滑动片段和(非功能性)选项卡的活动。

6) Fill the tab listener and connect it to the viewpager

6)填充tab监听器并连接到viewpager

private SimpleTabListener tabListener = new SimpleTabListener() {

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        final String tag = (String) tab.getTag();

        if (TABTAG_SAMPLE.equals(tag)) {
            viewPager.setCurrentItem(INDEX_SAMPLE);
        } else if (TABTAG_SECONDTAB.equals(tag)) {
            viewPager.setCurrentItem(INDEX_SECONDFRAGMENT);
        }
    }
};

This should be straightforward. You listen for a tab select event, check which tab is selected via the saved tag and call the viewpagers setCurrentItem()method with the index of the fragment that is associated with a certain tab.

这应该很简单。您侦听选项卡选择事件,检查通过保存的标记选择了哪个选项卡setCurrentItem(),并使用与某个选项卡关联的片段的索引调用 viewpagers方法。

Now you should be able to select a fragment via a tab as well as swipe around. You will note that swiping to a certain fragment wont sync the tabs accordingly, they wont get selected properly yet.

现在您应该能够通过选项卡选择片段以及左右滑动。您会注意到滑动到某个片段不会相应地同步选项卡,它们还不会被正确选择。

7) Attach an OnPageChangeListener to your ViewPager and select tabs accordingly

7) 将 OnPageChangeListener 附加到您的 ViewPager 并相应地选择选项卡

Again, you can use a SimpleOnPageChangeListener here as well instead of the interface. Short example:

同样,您也可以在此处使用 SimpleOnPageChangeListener 代替接口。简短示例:

private SimpleOnPageChangeListener onPageChangeListener 
        = new SimpleOnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        switch (position) {
        case INDEX_SAMPLE:
            actionBar.selectTab(sampleTab);
            break;

        case INDEX_SECONDFRAGMENT:
            actionBar.selectTab(secondTab);
            break;
        }
    };
};

This should also be self-explanatory. You watch for a swipe action that changes the displayed fragment, check it's index and select the appropriate tab. You see here why you had to keep the tab instances from step 5 around, you need them to select a tab.

这也应该是不言自明的。您观察改变显示片段的滑动操作,检查它的索引并选择适当的选项卡。您在这里看到了为什么必须保留第 5 步中的选项卡实例,您需要它们来选择选项卡。

Everything should work now.

现在一切都应该工作了。

回答by pcu

I have implemented very complicated application using actionbar sherlock. It works fine on 2.2 to ICS devices I have tested. I also used tabs and everything works fine. I changed it later to navigation list because there should be 6 tabs. Try demos included with actionbar to start with. This errors are probably some classpath problems. You need to add sherlock as lib project and support library must be included in both sherlock lib project and your project. Also check if both support libraries must have same versions.

我已经使用 actionbar sherlock 实现了非常复杂的应用程序。它在我测试过的 2.2 到 ICS 设备上运行良好。我还使用了标签,一切正常。后来我将其更改为导航列表,因为应该有 6 个选项卡。首先尝试操作栏附带的演示。这个错误可能是一些类路径问题。您需要将 sherlock 添加为 lib 项目,并且支持库必须包含在 sherlock lib 项目和您的项目中。还要检查两个支持库是否必须具有相同的版本。

回答by Luke

I would recommend taking a look at ViewPagerIndicatorit works with ActionBarSherlock and the compatibility library, I currently have an app using this for swiping tabs on 2.2 and up.

我建议看看它与 ActionBarSherlock 和兼容性库一起使用的ViewPagerIndicator,我目前有一个应用程序使用它来滑动 2.2 及更高版本的选项卡。

回答by pcu

I cannot add comments so this is my comment to ViewPagerIndicator. I used to it but when I started to use loaders to load data and fragmentadapter sometimes there were weird issues when new data came, orientation changed... But to use tabs with actionbarsherlock ViewPagerIndicator is not necessary.

我无法添加评论,所以这是我对 ViewPagerIndicator 的评论。我习惯了,但是当我开始使用加载器来加载数据和 fragmentadapter 时,有时会在新数据到来时出现奇怪的问题,方向改变了......但是没有必要使用带有 actionbarsherlock ViewPagerIndicator 的选项卡。