1.BottomNavigationView+FragmentLayout

1.1 activity_main
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".base.MainActivity"><FrameLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginBottom="40dp"    android:id="@+id/frag1"/><com.google.android.material.bottomnavigation.BottomNavigationView    android:id="@+id/bnv1"    android:layout_width="match_parent"    android:layout_height="40dp"    android:layout_alignParentBottom="true"    app:menu="@menu/bottomnavigationmenu"    android:background="@color/white"/>RelativeLayout>
1.2 BottomNavigationView添加menu
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">   <item android:id="@+id/home"       android:title="主页"       android:icon="@mipmap/home_normal"/>    <item android:id="@+id/select"        android:title="精选"        android:icon="@mipmap/select_normal"/>    <item android:id="@+id/red_packet"        android:title="特惠"        android:icon="@mipmap/red_packet_normal"/>    <item android:id="@+id/search"        android:title="搜索"        android:icon="@mipmap/search_normal"/>menu>

#####1.3 Main_Activity

public class MainActivity extends AppCompatActivity {    private BottomNavigationView mBottomNavigationView;    private FragmentManager mFragmentManager;    private FragmentTransaction mTransaction;    private HomeFragment mHomeFragment;    private  static Context mContext;    private SelectFragment mSelectFragment;    private RedFragment mRedFragment;    private SearchFragment mSearchFragment;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initUI();        initListener();        mContext=getContext();    }    public static Context getContext(){        return mContext;    }    private void initListener() {        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {            @Override            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {                switch (menuItem.getItemId()){                    case R.id.home:                        switchFragment(mHomeFragment);                        break;                    case R.id.select:                        switchFragment(mSelectFragment);                        break;                    case R.id.red_packet:                        switchFragment(mRedFragment);                        break;                    case R.id.search:                        switchFragment(mSearchFragment);                        break;                }                return true;            }        });    }    private Fragment lastFramgent=null;    private void switchFragment(Fragment homeFragment) {        mTransaction = mFragmentManager.beginTransaction();        if (!homeFragment.isAdded()) {            mTransaction.add(R.id.frag1,homeFragment);        }else {            mTransaction.show(homeFragment);        }        if (lastFramgent!=null){            mTransaction.hide(lastFramgent);        }        lastFramgent=homeFragment;//        mTransaction.replace(R.id.frag1,homeFragment);        mTransaction.commit();    }    private void initUI() {        mBottomNavigationView = findViewById(R.id.bnv1);        mFragmentManager = getSupportFragmentManager();        mHomeFragment = new HomeFragment();        mSelectFragment = new SelectFragment();        mRedFragment = new RedFragment();        mSearchFragment = new SearchFragment();        switchFragment(mHomeFragment);    }}
1.4为每个menu添加Fragment

每个Fragment实现对应的功能,这里省略详细的布局。

更多相关文章

  1. Android(安卓)Studio 添加jniLib
  2. Android(安卓)CTS Debug
  3. android的一个编译问题
  4. Android(安卓)在Camera 的 SurfaceView添加控制面板
  5. Android名称、版本和API level的对应关系
  6. Android(安卓)SDcard 文件读写,RandomAccessFile操作
  7. android仿苹果Iphone桌面源码
  8. Android(安卓)MediaMuxer混合音频和视频
  9. Android(安卓)发送短信功能

随机推荐

  1. mysql 同一表中.两个字段值互相复制,从一
  2. MySQL笔记(二)查询操作
  3. MySQL在保存数据时添加时间戳
  4. mysql中在表中insert数据时,有重复主键id
  5. mysql 树形结构查询(存储过程)
  6. 如何将sql查询中的值存储到变量中?
  7. 数据库_MySQL_复杂SQL的书写顺序与执行过
  8. mysql(workbench)更新数据时候的一个异常
  9. mysql dos命令 创建表单,选择数据库
  10. 什么是最好的Node.js mysql模块通过ssl连