Actionbarsherlock + 标签 + 多片段? [英] Actionbarsherlock + tabs + multi fragments?

查看:22
本文介绍了Actionbarsherlock + 标签 + 多片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常努力地让 actionbarsherlock + tabs + fragment 工作.

I´ve tried so hard to get actionbarsherlock + tabs + fragments working.

我只能让这个设置作为静态工作,我想创建这个像 android 市场应用程序(滑动移动).

I only can make this set to work as static, I would like to create this like android market app (swipe movement).

当你需要扩展包含多个片段的布局时,我会卡住.

I get stuck when you need to inflate a layout with multi fragments inside.

在 Support4demos 中,我以 FragmentsTabsPager 为例.

In Support4demos, I got FragmentsTabsPager as example to follow.

推荐答案

您需要合适的库来实现您想要的.

You need the right libraries to implement what you want.

基本上,ViewPager 库是您所缺少的.我的建议:

Basically, a ViewPager library is what is missing from you. My suggestions:

1.ActionbarSherlock

它太容易使用了,我不会解释它.

It's so dead-easy to work with that I won't explain it.

2.ViewPagerExtensions

您可以在此处找到它.下载 ZIP 文件并从中创建一个新项目.

You can find it here. Download the ZIP files and create a new project from it.

我只能让这个设置作为静态工作,我想创建这个像 android 市场应用程序(swype 运动).

I only can make this set to work as static, I would like to create this like android market app (swype movement).

从这个项目中实现 com.astuetz.viewpager.extensions.SwipeyTabsView.有易于遵循的示例.它完全符合您的要求.甚至还有其他选项卡样式可供选择(包括 ICS 附带的新人员选项卡).此外,根据您的应用标识设置样式也很容易.

Implement com.astuetz.viewpager.extensions.SwipeyTabsView from this project. There are easy-to-follow examples. it does exactly what you want. There are even other tab styles to choose from (including the new People tab that comes with ICS). Besides, it's very easy to style it to match your app identity.

3.FragmentPagerAdapter

最后,支持库 (v4) 中的那个类.

Finally, that class from the support library (v4).

祝您好运,如果您需要更多帮助,请随时问我.

Good luck, and be free to ask me if you need more help.

如果您使用的是我建议的内容,则无需覆盖 FragmentPagerAdapter 中的 instantiateItem.你只需要

You don't need to override instantiateItem in FragmentPagerAdapter if you're using what I suggested. You only need to

  1. 提供一个带有FragmentManager的构造函数并调用超级实现;
  2. 覆盖 getCount 以返回寻呼机中的片段数,以及
  3. getItem您将使用它来返回片段以进行创建.
  1. Provide a constructor with a FragmentManager and call the super implementation;
  2. Override getCount to return the number of fragments in your pager, and
  3. getItem, which is what you'll use to return your fragments for creation.

这是我这里的代码示例(完整的生产示例).它是实现寻呼机的活动的内部类:

This is an example from code I have here (a full working, production example). It's an inner class to the activity that implements the pager:

static class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment f;
        switch(position) {
        case 0:
            f= new ItemSalesDataFragment();
            break;
        case 1:
            f= new DepartmentChooserFragment();
            break;
        default:
            throw new IllegalArgumentException("not this many fragments: " + position);
        }
        return f;
    }
}

如您所见,此寻呼机处理两个页面",左侧显示销售数据.右边的允许用户选择哪个部门.这些在 getItem 方法中返回.适当的片段(您可以使用任何已有的片段,无需修改).

As you can see, this pager handles two 'pages', the left displays sales data. The right one allows the user to select which department. Those are returned in the getItem method. The proper fragments (you can use whatever Fragments you already have, no modification needed).

正如预期的那样,您通过 1) 创建一个实例化 MyFragmentPagerAdapter 的对象,以及 2) 将适配器设置为您的 ViewPager 对象成为那个类你刚刚实例化.如您所见,它的 getItem 方法将给"寻呼机您需要的所有片段.示例:

As expected, you join all this together by 1) creating an object that instantiate MyFragmentPagerAdapter, and 2) setting the adapter to your ViewPager object to be that class that you just instantiated. As you can see, its getItem method will "give" the pager all the fragments you need. Example:

mFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mFragmentPagerAdapter);

当然,还有其他事情要做,例如自己创建标签按钮并加入所有这些以进行交叉通信.我建议查看您从 GitHub 下载的 ViewPagerExtensions ZIP 文件中提供的示例.这只是您评论中问题的答案.如您所见,使用此库的代码并不多.

这篇关于Actionbarsherlock + 标签 + 多片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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