非常酷的tabbar源码

[复制链接]
来自: chino 分类: Android精品源码 上传时间: 2016-3-23 10:17:24
Tag:

项目介绍:



BottomBar


Does it work on my Grandpa Gary's HTC Dream?
Nope. The current minSDK version is API level 11 (Honeycomb).
Your uncle Bob's Galaxy S Mini will probably be supported in the future though.
Gimme that Gradle sweetness, pls?
[XML] 查看源文件 复制代码
compile 'com.roughike:bottom-bar:1.1.7'

Maven:
[XML] 查看源文件 复制代码

  com.roughike
  bottom-bar
  1.1.7
  pom


How?
BottomBar likes Fragments very much, but you can also handle your tab changes by yourself. You can add items by specifying an array of items or by xml menu resources.
Adding items from menu resource
res/menu/bottombar_menu.xml:
[XML] 查看源文件 复制代码

    
        ...

MainActivity.java

[XML] 查看源文件 复制代码
public class MainActivity extends AppCompatActivity {
    private BottomBar mBottomBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBottomBar = BottomBar.attach(this, savedInstanceState);
        mBottomBar.setItemsFromMenu(R.menu.bottombar_menu, new OnMenuTabSelectedListener() {
            @Override
            public void onMenuItemSelected(int resId) {
                if (resId == R.id.bottomBarItemOne) {
                    // the user selected item number one
                }
            }
        });

        // Setting colors for different tabs when there's more than three of them.
        // You can set colors for tabs in three different ways as shown below.
        mBottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorAccent));
        mBottomBar.mapColorForTab(1, 0xFF5D4037);
        mBottomBar.mapColorForTab(2, "#7B1FA2");
        mBottomBar.mapColorForTab(3, "#FF5252");
        mBottomBar.mapColorForTab(4, "#FF9800");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Necessary to restore the BottomBar's state, otherwise we would
        // lose the current tab on orientation change.
        mBottomBar.onSaveInstanceState(outState);
    }
}


Customization
[XML] 查看源文件 复制代码
// Disable the left bar on tablets and behave exactly the same on mobile and tablets instead.
mBottomBar.noTabletGoodness();

// Use the dark theme. Ignored on mobile when there are more than three tabs.
mBottomBar.useDarkTheme(true);

// Set the color for the active tab. Ignored on mobile when there are more than three tabs.
mBottomBar.setActiveTabColor("#009688");

// Use custom text appearance in tab titles.
mBottomBar.setTextAppearance(R.style.MyTextAppearance);

// Use custom typeface that's located at the "/src/main/assets" directory. If using with
// custom text appearance, set the text appearance first.
mBottomBar.setTypeFace("MyFont.ttf");


What about hiding it automatically on scroll?
Easy-peasy!
MainActivity.java:
[XML] 查看源文件 复制代码
// Instead of attach(), use attachShy:
mBottomBar = BottomBar.attachShy((CoordinatorLayout) findViewById(R.id.myCoordinator), 
    findViewById(R.id.myScrollingContent), savedInstanceState);
activity_main.xml:

[XML] 查看源文件 复制代码


    

        

    



Can it handle my Fragments and replace them automagically when a different tab is selected?
Yep yep yep! Just call setFragmentItems() instead of setItemsFromMenu():
[XML] 查看源文件 复制代码
// If you use normal Fragments, just change the first argument to getFragmentManager(). It's pure magic!
mBottomBar.setFragmentItems(getSupportFragmentManager(), R.id.fragmentContainer,
    new BottomBarFragment(SampleFragment.newInstance("Content for recents."), R.drawable.ic_recents, "Recents"),
    new BottomBarFragment(SampleFragment.newInstance("Content for favorites."), R.drawable.ic_favorites, "Favorites"),
    new BottomBarFragment(SampleFragment.newInstance("Content for nearby stuff."), R.drawable.ic_nearby, "Nearby")
);


I hate Fragments and wanna do everything by myself!
That's alright, you can also do it the hard way if you're living on the edge.
[XML] 查看源文件 复制代码
mBottomBar.setItems(
        new BottomBarTab(R.drawable.ic_recents, "Recents"),
        new BottomBarTab(R.drawable.ic_favorites, "Favorites"),
        new BottomBarTab(R.drawable.ic_nearby, "Nearby")
);

// Listen for tab changes
mBottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
    @Override
    public void onItemSelected(int position) {
        // user selected a different tab
    }
});


For a working example, refer to the sample app.
Common problems and solutions
Can I use it by XML?
No, but you can still put it anywhere in the View hierarchy. Just attach it to any View you want like this:
[XML] 查看源文件 复制代码
mBottomBar.attach(findViewById(R.id.myContent), savedInstanceState);


Why does the top of my content have sooooo much empty space?!
Probably because you're doing some next-level advanced Android stuff (such as using CoordinatorLayout and fitsSystemWindows="true") and the normal paddings for the content are too much. Add this right after calling attach():
[XML] 查看源文件 复制代码
mBottomBar.noTopOffset();
I don't like the awesome transparent Navigation Bar!
You can disable it.
[XML] 查看源文件 复制代码
mBottomBar.noNavBarGoodness();
Why is it overlapping my Navigation Drawer?
All you need to do is instead of attaching the BottomBar to your Activity, attach it to the view that has your content. For example, if your fragments are in a ViewGroup that has the id fragmentContainer, you would do something like this:
[XML] 查看源文件 复制代码
mBottomBar.attach(findViewById(R.id.fragmentContainer), savedInstanceState);
What about Tablets?
It works nicely with tablets straight out of the box. When the library detects that the user has a tablet, the BottomBar will become a "LeftBar", just like in the Material Design Guidelines.
What about the (insert thing that looks different than the specs here)?
Just give me some time and all your dreams will come true.
Apps using BottomBar
Send me a pull request with modified README.md to get a shoutout!
Contributions
Feel free to create issues.
Don't send me pull requests just yet, not until the dust settles.
I'm fixing issues and busting my ass to make this library better, several hours every day. Your hard work could be for nothing, as I'm probably fixing / implementing the same problems that you are.












相关源码推荐:

我来说两句
所有评论(112)
ushang000 2016-3-23 14:14:42
很给力,安卓巴士有你更精彩!
回复
xiyangyangdi 2016-3-23 14:38:59
我只是路过打酱油的。
回复
xiyangyangdi 2016-3-23 14:42:13
楼主威武啊,安卓巴士有你更给力!
回复
chino 2016-3-23 15:10:10
精华内容,楼主V5!
回复
潘先生 2016-3-23 17:15:47
精华内容,楼主V5!
回复
潘先生 2016-3-23 17:17:45
强烈支持楼主ing……
回复
boby~康 2016-3-24 09:25:44
感谢分享,安卓巴士有你更精彩:)
回复
提取码:  下载次数:73 状态:已购或VIP 售价:10(原价:10)金钱 下载权限:初级码农 
5198 0 73
代码贡献英雄榜
用户名 下载数
联系我们
首页/微信公众账号投稿
帖子代码编辑/版权问题
QQ:435399051,1294855032
如何获得代码达人称号?
如何成为签约作者?
领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )