Actionbarsherlock + 选项卡 + 多片段? [英] Actionbarsherlock + tabs + multi fragments?

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

问题描述

我已经非常努力地让 actionbarsherlock + 选项卡 + 片段正常工作.

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.

这是我这里的代码示例(一个完整的工作示例).它是实现寻呼机的 Activity 的内部类:

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天全站免登陆