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

我想要在三个页面实现不同的东西,并且通过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;
}

posted on   石墨方  阅读(563)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示