nav_jectpack
创建MainFragment、CenterFragment、MeFragment
BottomBtnBean 用于设置导航的按钮信息
package com.zqq.h5shell.activity.jectpackdemo.bean;
import androidx.databinding.baseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.PropertyChangeRegistry;
import com.zqq.h5shell.BR;
public class BottomBtnBean extends baseObservable {
public int icon_id;
public String name;
public int width;
public int height;
private transient PropertyChangeRegistry propertyChangeRegistry = new PropertyChangeRegistry();
@Bindable
public int getIcon_id() {
return icon_id;
}
public void setIcon_id(int icon_id) {
this.icon_id = icon_id;
notifyChange(BR.icon_id);
}
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyChange(BR.name);
}
@Bindable
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
notifyChange(BR.width);
}
@Bindable
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
notifyChange(BR.height);
}
private synchronized void notifyChange(int propertyId) {
if (propertyChangeRegistry == null) {
propertyChangeRegistry = new PropertyChangeRegistry();
}
propertyChangeRegistry.notifyChange(this, propertyId);
}
@Override
public synchronized void addonPropertyChangedCallback(onPropertyChangedCallback callback) {
if (propertyChangeRegistry == null) {
propertyChangeRegistry = new PropertyChangeRegistry();
}
propertyChangeRegistry.add(callback);
}
@Override
public synchronized void removeonPropertyChangedCallback(onPropertyChangedCallback callback) {
if (propertyChangeRegistry != null) {
propertyChangeRegistry.remove(callback);
}
}
}
创建ViewModel
package com.zqq.h5shell.activity.jectpackdemo;
import android.graphics.Color;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.zqq.h5shell.R;
import com.zqq.h5shell.activity.jectpackdemo.bean.BottomBtnBean;
import com.zqq.h5shell.activity.jectpackdemo.bean.BottomStyleBean;
import java.util.ArrayList;
import java.util.List;
public class MyViewModel extends ViewModel {
private MutableLiveData> bottomBeans;
private MutableLiveData bottomStyleBean;
public MutableLiveData getBottomStyleBean() {
if(bottomStyleBean == null){
bottomStyleBean = new MutableLiveData<>();
initBottomStyle();
}
return bottomStyleBean;
}
private void initBottomStyle() {
if(bottomStyleBean!=null){
BottomStyleBean bottomStyle=new BottomStyleBean();
bottomStyle.setBg_color(R.drawable.bottom_bg);
bottomStyle.setNomal_color(Color.GRAY);
bottomStyle.setSelect_color(Color.BLUE);
bottomStyleBean.setValue(bottomStyle);
}
}
public void setBottomStyleBean(MutableLiveData bottomStyleBean) {
this.bottomStyleBean = bottomStyleBean;
}
public LiveData> getBottomBeans() {
if(bottomBeans == null){
bottomBeans = new MutableLiveData<>();
initBottomBeans();
}
return bottomBeans;
}
private void initBottomBeans() {
//自定义数据
List bottomBtnBeanList=new ArrayList<>();
BottomBtnBean bottomBtnBean=new BottomBtnBean();
bottomBtnBean.setName("首页");
bottomBtnBean.setIcon_id(R.drawable.home);
bottomBtnBeanList.add(bottomBtnBean);
BottomBtnBean bottomBtnBean1=new BottomBtnBean();
bottomBtnBean1.setName("中心");
bottomBtnBean1.setIcon_id(R.drawable.center);
bottomBtnBeanList.add(bottomBtnBean1);
BottomBtnBean bottomBtnBean2=new BottomBtnBean();
bottomBtnBean2.setName("我的");
bottomBtnBean2.setIcon_id(R.drawable.me);
bottomBtnBeanList.add(bottomBtnBean2);
bottomBeans.setValue(bottomBtnBeanList);
}
public void setBottomBeans(MutableLiveData> bottomBeans) {
this.bottomBeans = bottomBeans;
}
public void changeBottomBeans(){
if(bottomBeans!=null){
List value = bottomBeans.getValue();
for (BottomBtnBean bottomBean: value
) {
bottomBean.setName(bottomBean.getName()+"胡辣汤");
}
bottomBeans.setValue(value);
}
}
//改变底部导航选择按钮
public void changeBottomColor(int color){
if(bottomStyleBean!=null){
BottomStyleBean value = bottomStyleBean.getValue();
value.setSelect_color(color);
bottomStyleBean.setValue(value);
}
}
}
BottomStyleBean 用来修改导航的颜色信息
package com.zqq.h5shell.activity.jectpackdemo.bean;
import androidx.databinding.baseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.PropertyChangeRegistry;
import com.zqq.h5shell.BR;
public class BottomStyleBean extends baseObservable {
public int bg_color;
public int nomal_color;
public int select_color;
private transient PropertyChangeRegistry propertyChangeRegistry = new PropertyChangeRegistry();
@Bindable
public int getBg_color() {
return bg_color;
}
public void setBg_color(int bg_color) {
this.bg_color = bg_color;
notifyChange(BR.bg_color);
}
@Bindable
public int getNomal_color() {
return nomal_color;
}
public void setNomal_color(int nomal_color) {
this.nomal_color = nomal_color;
notifyChange(BR.nomal_color);
}
@Bindable
public int getSelect_color() {
return select_color;
}
public void setSelect_color(int select_color) {
this.select_color = select_color;
notifyChange(BR.select_color);
}
private synchronized void notifyChange(int propertyId) {
if (propertyChangeRegistry == null) {
propertyChangeRegistry = new PropertyChangeRegistry();
}
propertyChangeRegistry.notifyChange(this, propertyId);
}
@Override
public synchronized void addonPropertyChangedCallback(onPropertyChangedCallback callback) {
if (propertyChangeRegistry == null) {
propertyChangeRegistry = new PropertyChangeRegistry();
}
propertyChangeRegistry.add(callback);
}
@Override
public synchronized void removeonPropertyChangedCallback(onPropertyChangedCallback callback) {
if (propertyChangeRegistry != null) {
propertyChangeRegistry.remove(callback);
}
}
}
MainActivity
package com.zqq.h5shell.activity.jectpackdemo;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.zqq.h5shell.ARouterContent;
import com.zqq.h5shell.R;
import com.zqq.h5shell.activity.jectpackdemo.bean.BottomBtnBean;
import com.zqq.h5shell.activity.jectpackdemo.bean.BottomStyleBean;
import com.zqq.h5shell.databinding.ActivityJectpackMainBinding;
import java.util.List;
@Route(path = ARouterContent.MainActivity)
public class MainActivity extends AppCompatActivity {
private ActivityJectpackMainBinding binding;
private MyViewModel myViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_jectpack_main);
initView();
}
private void initView() {
myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
binding.setBottomStyleBean(myViewModel.getBottomStyleBean().getValue());
//监听底部数据变化,设置按钮和文字
myViewModel.getBottomBeans().observe(this, new Observer>() {
@Override
public void onChanged(List bottomBtnBeans) {
setMenuItem(bottomBtnBeans);
}
});
//监听变化后修改导航显示
myViewModel.getBottomStyleBean().observe(this, new Observer() {
@Override
public void onChanged(BottomStyleBean bottomStyleBean) {
Menu menu = binding.bottomView.getMenu();
setMenuBg(menu);
}
});
//navigation
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentview);
NavController navController = navHostFragment.getNavController();
binding.bottomView.setonNavigationItemSelectedListener(new BottomNavigationView.onNavigationItemSelectedListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case 0:
navController.navigate(R.id.mainFragment2);
break;
case 1:
navController.navigate(R.id.centerFragment);
break;
case 2:
navController.navigate(R.id.meFragment2);
break;
}
return true;
}
});
}
private void setMenuItem(List bottomBtnBeans) {
//设置bottom的menu的item信息
Menu menu = binding.bottomView.getMenu();
menu.clear();
for(int i=0;i bottomStyleBean = myViewModel.getBottomStyleBean();
BottomStyleBean value = bottomStyleBean.getValue();
// 设置颜色
int[][] states = new int[2][1];
states[0][0]=-android.R.attr.state_checked;
states[1][0]=android.R.attr.state_checked;
int[] colors =new int[]{value.getNomal_color(),value.getSelect_color()};
ColorStateList colorStateList = new ColorStateList(states,colors);
binding.bottomView.setItemTextColor(colorStateList);
binding.bottomView.setItemIconTintList(colorStateList);
binding.bottomView.setBackgroundResource(value.getBg_color());
menu.getItem(0).setChecked(true);
}
}
MeFragment 代码更新viewmodel
package com.zqq.h5shell.activity.jectpackdemo.fragment;
import android.app.StatusBarManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.zqq.h5shell.R;
import com.zqq.h5shell.activity.jectpackdemo.MyViewModel;
import com.zqq.h5shell.databinding.FragmentDemoMeBinding;
public class MeFragment extends Fragment {
FragmentDemoMeBinding binding;
MyViewModel myViewModel;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_demo_me,container,false);
initView();
return binding.getRoot();
}
private void initView() {
setStatusBar();
RequestOptions requestOptions=new RequestOptions()
.circleCrop();
Glide.with(requireActivity())
.load(R.drawable.head)
.apply(requestOptions)
.into(binding.ivHead);
binding.tvName.setText("东门吹冰");
myViewModel = new ViewModelProvider(requireActivity()).get(MyViewModel.class);
binding.tvRed.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
myViewModel.changeBottomColor(Color.RED);
}
});
binding.tvBlue.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
myViewModel.changeBottomColor(Color.BLUE);
}
});
binding.tvGreen.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
myViewModel.changeBottomColor(Color.GREEN);
}
});
}
protected void setStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
View decorView = requireActivity().getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
//根据上面设置是否对状态栏单独设置颜色
requireActivity(). getWindow().setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
WindowManager.LayoutParams localLayoutParams = requireActivity().getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android6.0以后可以对状态栏文字颜色和图标进行修改
requireActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
}
实现效果



