利用BottomNavigationBar实现不同的fragment之间的转换

本文介绍如何使用BottomNavigationBar组件实现在三个Fragment之间的切换。通过在MainActivity中设置不同状态的颜色、背景样式等属性,添加三个Fragment并配置底部导航栏的监听器,实现页面间的平滑切换。
摘要由CSDN通过智能技术生成

我想要在三个页面实现不同的东西,并且通过bottomNavigationBar进行切换

1、在build.gradle中引入bottom-navigation-bar

dependencies中加入
compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.1'

 2、创建三个fragmentActivity

其中之一

public class FragActivity_Me extends Fragment {

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
@Nullable Bundle savedInstanceState) { View view
= inflater.inflate(R.layout.fragment_3, container, false); return view; } }

3、在activity_maiin.xml中插入fragment

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".TabBarActivity">

        <FrameLayout android:id="@+id/fragment_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
        </FrameLayout>

        <com.ashokvarma.bottomnavigation.BottomNavigationBar
            android:id="@+id/bottom_navigation_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />

    </LinearLayout>

4、修改其中一个FragmentActivity

public class FragActivity_Look extends Fragment {

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
     btn = view.findViewById(R.id.bt_set);
return view;
}
}

 

5、修改MainActivity

首先public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener

申明变量

    private BottomNavigationBar bottomNavigationBar;
    private FrameLayout frameLayout;
    private FragmentTransaction transaction;
    private FragActivity_Look fragActivity_look;
    private FragActivity_Map fragActivity_map;
    private FragActivity_Me fragActivity_me;
    private Fragment mFragment;

 

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frameLayout = (FrameLayout) findViewById(R.id.fragment_content);
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setActiveColor(R.color.colorAccent)//设置Item选中颜色方法
                .setInActiveColor(R.color.colorPrimary)//设置Item未选中颜色方法
                .setBarBackgroundColor("#FFFFFF");//背景颜色
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.bt_look, "Look").
                setActiveColorResource(R.color.teal)) .addItem(new BottomNavigationItem(R.mipmap.bt_map, "Map").
                setActiveColorResource(R.color.blue)) .addItem(new BottomNavigationItem(R.mipmap.bt_user, "Me").
                setActiveColorResource(R.color.grey)) .setFirstSelectedPosition(1) .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
        //initFragment();

        fragActivity_look=new FragActivity_Look();
        fragActivity_map = new FragActivity_Map();
        fragActivity_me = new FragActivity_Me();
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_content, fragActivity_map).commit();
        mFragment = fragActivity_map;
    }

切换fragment

private void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是不是切换的Fragment
        if(mFragment != fragment) {
            //判断切换的Fragment是否已经添加过
            if (!fragment.isAdded()) {
                //如果没有,则先把当前的Fragment隐藏,把切换的Fragment添加上
                getSupportFragmentManager().beginTransaction().hide(mFragment)
                        .add(R.id.fragment_content,fragment).commit();
            } else {
                //如果已经添加过,则先把当前的Fragment隐藏,把切换的Fragment显示出来
                getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
            }
            mFragment = fragment;
        }
    }

实现OnTabSelectedListener接口所需要的方法

@Override
    public void onTabSelected(int position) {
        switch (position){
            case 0:
                switchFragment(fragActivity_look);
                break;
            case 1:
                switchFragment(fragActivity_map);
                break;
            case 2:
                switchFragment(fragActivity_me);
                break;
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

还需要三个小图片(24像素左右)及定义colors即可

 

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.fragment_content);
bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
bottomNavigationBar.setActiveColor(R.color.colorAccent)//设置Item选中颜色方法
.setInActiveColor(R.color.colorPrimary)//设置Item未选中颜色方法
.setBarBackgroundColor("#FFFFFF");//背景颜色
bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.bt_look, "Look").
setActiveColorResource(R.color.teal)) .addItem(new BottomNavigationItem(R.mipmap.bt_map, "Map").
setActiveColorResource(R.color.blue)) .addItem(new BottomNavigationItem(R.mipmap.bt_user, "Me").
setActiveColorResource(R.color.grey)) .setFirstSelectedPosition(1) .initialise();
bottomNavigationBar.setTabSelectedListener(this);
//initFragment();

fragActivity_look=new FragActivity_Look();
fragActivity_map = new FragActivity_Map();
fragActivity_me = new FragActivity_Me();
transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_content, fragActivity_map).commit();
mFragment = fragActivity_map;
}

转载于:https://www.cnblogs.com/Nora-F/p/8629678.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值