阅读 159

MVVMSmart系列文章--Jetpack的Navigation的使用

Navigation是谷歌AAC架构中的重要组件,中文名为"导航". 导航是指支持用户导航、进入和退出应用中不同内容片段的交互。Android Jetpack 的导航组件可帮助您实现导航,无论是简单的按钮点击,还是应用栏和抽屉式导航栏等更为复杂的模式,该组件均可应对。导航组件还通过遵循一套既定原则来确保一致且可预测的用户体验。

博文代码:在MVVMSmart项目中

  • MVVMsmart地址:github.com/wzqjava/MVV…
  • 分支名称:MVVMSmart-kotlin(切换分支,readme会自动切换成对应语言)
  • 分支名称:MVVMSmart-java(切换分支,readme会自动切换成对应语言)
  • 基于谷歌最新AAC架构,MVVM设计模式的一套快速开发库,整合ViewModel+Lifecycles+Navigation+DataBinding+LilveData+Okhttp+Retrofit+RxJava+Glide等主流模块,满足日常开发需求。使用该框架可以快速开发高质量、易维护的Android应用.

MVVMSmart中这个界面点击任何一个功能

先上几张图大家看看效果

使用步骤 1.添加依赖 '''

  1. "navigation-fragment" :"androidx.navigation:navigation-fragment:2.1.0",
  2. "navigation-ui" : "androidx.navigation:navigation-ui:2.1.0", '''
  3. 目前不建议整个app只使用一个Activity,要处理的的东西太多,可以一个功能使用一个Activit,里面的二三四级页面全部使用navigation导航Fragment,很舒服主布局中假如一下标签;

'''

    <fragment
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_home" />
复制代码

'''

nav_home.xml '''

<fragment
    android:id="@+id/homeFragment"
    android:name="com.wzq.sample.ui.main.HomeFragment"
    android:label="HomeFragment" >
    <action
        android:id="@+id/action_homeFragment_to_netWorkFragment"
        app:destination="@id/netWorkFragment" />
    <action
        android:id="@+id/action_homeFragment_to_multiRecycleViewFragment"
        app:destination="@id/multiRecycleViewFragment" />
    <action
        android:id="@+id/action_homeFragment_to_viewPagerGroupFragment"
        app:destination="@id/viewPagerGroupFragment" />
    <action
        android:id="@+id/action_homeFragment_to_formFragment"
        app:destination="@id/formFragment" />
</fragment>
<fragment
    android:id="@+id/netWorkFragment"
    android:name="com.wzq.sample.ui.network.NetWorkFragment"
    android:label="NetWorkFragment" >
    <action
        android:id="@+id/action_netWorkFragment_to_homeFragment"
        app:destination="@id/homeFragment" />
    <action
        android:id="@+id/action_netWorkFragment_to_detailFragment"
        app:destination="@id/detailFragment" />
</fragment>
<fragment
    android:id="@+id/multiRecycleViewFragment"
    android:name="com.wzq.sample.ui.rv_multi.MultiRecycleViewFragment"
    android:label="MultiRecycleViewFragment" >
    <action
        android:id="@+id/action_multiRecycleViewFragment_to_homeFragment"
        app:destination="@id/homeFragment" />
</fragment>
<fragment
    android:id="@+id/viewPagerGroupFragment"
    android:name="com.wzq.sample.ui.vp_frg.ViewPagerGroupFragment"
    android:label="ViewPagerGroupFragment" >
    <action
        android:id="@+id/action_viewPagerGroupFragment_to_homeFragment"
        app:destination="@id/homeFragment" />
</fragment>
<fragment
    android:id="@+id/formFragment"
    android:name="com.wzq.sample.ui.form.FormFragment"
    android:label="FormFragment" >
    <action
        android:id="@+id/action_formFragment_to_homeFragment"
        app:destination="@id/homeFragment" />
</fragment>
<fragment
    android:id="@+id/detailFragment"
    android:name="com.wzq.sample.ui.network.detail.DetailFragment"
    android:label="DetailFragment" >
    <action
        android:id="@+id/action_detailFragment_to_netWorkFragment"
        app:destination="@id/netWorkFragment" />
</fragment>
复制代码
'''

HomeFragment.java ''' package com.wzq.sample.ui.main;

import android.Manifest; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.os.Bundle; import android.view.LayoutInflater; import android.view.ViewGroup;

import androidx.annotation.Nullable; import androidx.lifecycle.Observer; import androidx.navigation.fragment.NavHostFragment;

import com.tbruyelle.rxpermissions2.RxPermissions; import com.wzq.mvvmsmart.base.BaseFragment; import com.wzq.mvvmsmart.http.DownLoadManager; import com.wzq.mvvmsmart.http.download.ProgressCallBack; import com.wzq.mvvmsmart.utils.KLog; import com.wzq.mvvmsmart.utils.ToastUtils; import com.wzq.sample.R; import com.wzq.sample.bean.FormEntity; import com.wzq.sample.databinding.FragmentHomeBinding; import com.wzq.sample.ui.tab_bar.activity.TabBarActivity;

import io.reactivex.functions.Consumer; import okhttp3.ResponseBody;

/**

  • 截止2019年12月21日累计投入时间:45小时

  • 本项目接口地址: www.oschina.net/action/apiv… */ public class HomeFragment extends BaseFragment<FragmentHomeBinding, HomeViewModel> {

    @Override public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return R.layout.fragment_home; }

    @Override public int initVariableId() { return com.wzq.sample.BR.viewModel; }

    @Override public void initData() { super.initData(); binding.setPresenter(new Presenter()); }

    @Override public void initViewObservable() { //注册监听相机权限的请求 viewModel.requestCameraPermissions.observe(this, new Observer() { @Override public void onChanged(@Nullable Boolean aBoolean) { requestCameraPermissions(); } });

     //注册文件下载的监听
     viewModel.loadUrlEvent.observe(this, new Observer<String>() {
         @Override
         public void onChanged(@Nullable String url) {
             downFile(url);
         }
     });
    复制代码

    }

    /**

    • 封装布局中的点击事件儿; */ public class Presenter {

      //网络访问点击事件 public void netWorkClick() { NavHostFragment .findNavController(HomeFragment.this) .navigate(R.id.action_homeFragment_to_netWorkFragment); }

      //RecycleView多布局 public void rvMultiClick() { NavHostFragment .findNavController(HomeFragment.this) .navigate(R.id.action_homeFragment_to_multiRecycleViewFragment); }

      //进入TabBarActivity public void startTabBarClick() { startActivity(TabBarActivity.class); }

      //ViewPager绑定 public void viewPagerBindingClick() { ToastUtils.showShort("点击跳转viewpager"); NavHostFragment .findNavController(HomeFragment.this) .navigate(R.id.action_homeFragment_to_viewPagerGroupFragment); }

      //ViewPager+Fragment public void viewPagerGroupBindingClick() { NavHostFragment .findNavController(HomeFragment.this) .navigate(R.id.action_homeFragment_to_viewPagerGroupFragment); }

      //表单提交点击事件 public void formSbmClick() { NavHostFragment .findNavController(HomeFragment.this) .navigate(R.id.action_homeFragment_to_formFragment); }

      //表单修改点击事件 public void formModifyClick() { //模拟一个修改的实体数据 FormEntity entity = new FormEntity(); entity.setId("12345678"); entity.setName("text"); entity.setSex("1"); entity.setBir("xxxx年xx月xx日"); entity.setMarry(true); //传入实体数据 Bundle mBundle = new Bundle(); mBundle.putParcelable("entity", entity); NavHostFragment .findNavController(HomeFragment.this) .navigate(R.id.action_homeFragment_to_formFragment, mBundle); }

      //权限申请 public void permissionsClick() { viewModel.requestCameraPermissions.call(); }

      //全局异常捕获 public void exceptionClick() { //伪造一个异常 Integer.parseInt("test"); }

      //文件下载 public void fileDownLoadClick() { viewModel.loadUrlEvent.setValue("gdown.baidu.com/data/wisega…");

      } }

    /**

    • 请求相机权限 */ @SuppressLint("CheckResult") private void requestCameraPermissions() { ToastUtils.showShort("请求相机权限"); //请求打开相机权限 RxPermissions rxPermissions = new RxPermissions(getActivity()); rxPermissions.request(Manifest.permission.CAMERA) .subscribe(new Consumer() { @Override public void accept(Boolean aBoolean) throws Exception { if (aBoolean) { ToastUtils.showShort("相机权限已经打开,直接跳入相机"); } else { ToastUtils.showShort("权限被拒绝"); } } }); }

    private void downFile(String url) { String destFileDir = getActivity().getApplication().getCacheDir().getPath(); KLog.e("destFileDir--" + destFileDir); String destFileName = System.currentTimeMillis() + ".apk"; final ProgressDialog progressDialog = new ProgressDialog(getActivity()); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setTitle("正在下载..."); progressDialog.setCancelable(false); progressDialog.show(); /** * ProgressCallBack构造方法中,LiveEventBus监听进度改变,调用ProgressCallBack的progress方法设置进度 */ DownLoadManager.getInstance().load(url, new ProgressCallBack(HomeFragment.this, destFileDir, destFileName) { @Override public void onStart() { super.onStart(); KLog.e("下载--onStart"); }

         @Override
         public void onSuccess(ResponseBody responseBody) {
             KLog.e("下载--onSuccess");
             ToastUtils.showShort("文件下载完成!");
         }
    
         @Override
         public void progress(final long progress, final long total) {
             KLog.e("下载--progress");
             progressDialog.setMax((int) total);
             progressDialog.setProgress((int) progress);
         }
    
         @Override
         public void onError(Throwable e) {
             e.printStackTrace();
             ToastUtils.showShort("文件下载失败!");
             progressDialog.dismiss();
         }
    
         @Override
         public void onCompleted() {
             progressDialog.dismiss();
             KLog.e("下载--onCompleted");
         }
     });
    复制代码

    } }

''' HomeFragment中,点击不同的功能,导航到不同的fragment, 看博客要看懂思路即可, 书上得来终觉浅,绝知此事要躬行, 要想清楚如何使用和更高级用法,请下载代码自己查看,才能做到掌握.

代码地址:github.com/wzqjava/MVV…