AdaptableBottomNavigation

介绍:

使用Adapter的方式为BottomNavigation增加切换的页面,用法类似TabLayout。

运行效果:

使用说明:

xml

<org.buffer.adaptablebottomnavigation.view.ViewSwapper
    android:id="@+id/view_swapper"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/view_bottom_navigation" />
<org.buffer.adaptablebottomnavigation.AdaptableBottomNavigationView
    android:id="@+id/view_bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimary"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@drawable/selector_menu"
    app:itemTextColor="@drawable/selector_menu"
    app:menu="@menu/main" />

创建FragmentAdapter(或者FragmentStateAdapter)

public class ViewSwapperAdapter extends FragmentStateAdapter {
    private static final int INDEX_BUFFER = 0;
    private static final int INDEX_RETREAT = 1;
    private static final int INDEX_VALUES = 2;
    public ViewSwapperAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case INDEX_BUFFER:
                return ImageFragment.newInstance(R.drawable.cat);
            case INDEX_RETREAT:
                return ImageFragment.newInstance(R.drawable.buffer);
            case INDEX_VALUES:
                return ImageFragment.newInstance(R.drawable.android);
        }
        return ImageFragment.newInstance(R.drawable.cat);
    }
    @Override
    public int getCount() {
        return 3;
    }
}

关联

viewSwapper.setAdapter(viewSwapperAdapter);
bottomNavigationView.setupWithViewSwapper(viewSwapper);
已下载
0