android 底部菜单 BottomNavigationView + Fragment

编译版本在21及以上
第一步、创建三个Fragment 自己自定义命名
HomeFragment

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.ms.lemonnote.R;
public class HomeFragment extends Fragment {
    private static final String TAG = "HomeFragment";
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_fragment, null);
    }

}

fragment_home.xml




    

FindFragMent、 MineFragment 和HomeFragment 一样

第二、在res资源文件下,创建menu资源文件并在其目录下面创建bottom_menu.xml,注意个数不能超过5个否则会报错



    
    
    

第三、创建MainActivity

... 省略引用
public class MainActivity extends AppCompatActivity {

    BottomNavigationView bottomNavigationView = null;
    private List mFragments;
    private Fragment mCurrFragment;
    private HomeFragment homeFragment;
    private MineFragment mineFragment;
    private FindFragment findFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigationView = findViewById(R.id.navigation);

        // ===============底部菜单 第一种  BottomNavigationView+Fragment start===================================

        //初始化Fragment及底部导航栏
        initFragments(savedInstanceState);
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> onTabItemSelected(item.getItemId()));

        // ===============底部菜单 第一种  BottomNavigationView+Fragment end===================================


    }

    private void switchFragment(Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            //第一次使用switchFragment()时currentFragment为null,所以要判断一下
            if (mCurrFragment != null) {
                transaction.hide(mCurrFragment);
            }
            transaction.add(R.id.fragment_container, targetFragment, targetFragment.getClass().getName());

        } else {
            transaction
                    .hide(mCurrFragment)
                    .show(targetFragment);
        }
        mCurrFragment = targetFragment;
        transaction.commit();
    }

    private boolean onTabItemSelected(int id) {
        Fragment fragment = null;
        switch (id) {
            case R.id.navigation_home: 
                fragment = mFragments.get(0);
                break;
            case R.id.navigation_find:
                fragment = mFragments.get(1);
                break;
            case R.id.navigation_mine:
                fragment = mFragments.get(2);
                break;
            default:
                break;
        }
        if(fragment == null) {
            return false;
        }
        switchFragment(fragment);
        return true;
    }




    private void initFragments(Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            homeFragment = new HomeFragment();
            findFragment = new FindFragment();
            mineFragment = new MineFragment();

            mFragments = new ArrayList<>(3);
            mFragments.add(homeFragment);
            mFragments.add(findFragment);
            mFragments.add(mineFragment);
            mCurrFragment = homeFragment;
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_container, homeFragment)
                    .commit();
        }
    }
}


activity_main.xml



    
    

    

你可能感兴趣的:(android 底部菜单 BottomNavigationView + Fragment)