最近基于 Wanandroid开放API 开发了一款十分简单的APP,, 采用 Androidx + MVVM + Retrofit + dataBinding 的模式进行开发。App的主要功能是就是浏览各类 Android 文章,效果图如下所示:
该项目采用 MVVM 的开发模式,关于 MVVM 开发模式,可以参考: 美团 如何构建Android MVVM 应用框架 ,如果你觉得不易理解,可以参考一下 Android MVC MVP MVVM简单例子
MVVM 的经典架构图:
项目中的架构图为:
举个例子:比如我们的主页,有 banner广告图以及首页文章,如下所示:
所以要实现这样的功能,我们需要为 HomeFragment 创建 HomeViewModel、HomeRepository、HomeBeanFiles,如下所示:
具体代码见: Wanandroid_LearningHomeFragment.java
public class HomeFragment extends Fragment { @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //创建 ViewModel HomeViewModel homeVm = ViewModelProviders.of(this, new ViewModelFactory()).get(HomeViewModel.class); //观察 banner data homeVm.getHomeBannerListLd().observe(getViewLifecycleOwner(), bannerListDataObserver); homeVm.setHomeBannerListLd(); //观察 Article data homeVm.getHomeArticleListBeanLd().observe(getViewLifecycleOwner(), articleListBeanObserver); homeVm.setHomeArticleListBeanLd(); } ···略··· } 复制代码
HomeViewModel.java
public class HomeViewModel extends ViewModel { private static final String TAG = "HomeViewModel"; private MutableLiveData<HomeBannerListBean> homeBannerListLd; private MutableLiveData<HomeArticleListBean> homeArticleListBeanLd; public HomeViewModel() { this.homeBannerListLd = new MutableLiveData<>(); this.homeArticleListBeanLd = new MutableLiveData<>(); } public MutableLiveData<HomeBannerListBean> getHomeBannerListLd() { return homeBannerListLd; } public void setHomeBannerListLd() { HomeRepository.newInstance().getHomeBannerList(new GetWebDataListener() { @Override public void getDataSuccess(Object object) { HomeBannerListBean homeBannerListBean = (HomeBannerListBean) object; homeBannerListLd.postValue(homeBannerListBean); } @Override public void getDataFailed(String failedMsg) { Log.e(TAG, "getDataFailed: " + failedMsg); } }); } public MutableLiveData<HomeArticleListBean> getHomeArticleListBeanLd() { return homeArticleListBeanLd; } public void setHomeArticleListBeanLd() { HomeRepository.newInstance().getHomeArticleList(0, new GetWebDataListener() { @Override public void getDataSuccess(Object object) { HomeArticleListBean homeArticleListBean = (HomeArticleListBean) object; homeArticleListBeanLd.postValue(homeArticleListBean); } @Override public void getDataFailed(String failedMsg) { Log.e(TAG, "getDataFailed: " + failedMsg); } }); } } 复制代码
HomeRepository.java
public class HomeRepository { /** * 为了防止出现DCL失效问题,所以用 volatile 关键字来修饰 instance。 */ private volatile static HomeRepository instance; public static HomeRepository newInstance() { if (instance == null) { synchronized (HomeRepository.class) { if (instance == null) { instance = new HomeRepository(); } } } return instance; } public void getHomeBannerList(final GetWebDataListener listener) { ApiService apiService = ApiWrapper.getRetrofitInstance().create(ApiService.class); apiService.getHomeBannerList().enqueue(new AbstractRetrofitCallback() { @Override public void getSuccessful(String responseBody) { Gson gson = new Gson(); HomeBannerListBean homeBannerListBean = gson.fromJson(responseBody, HomeBannerListBean.class); listener.getDataSuccess(homeBannerListBean); } @Override public void getFailed(String failedMsg) { listener.getDataFailed(failedMsg); } }); } public void getHomeArticleList(int pageNumber, final GetWebDataListener listener) { ApiService apiService = ApiWrapper.getRetrofitInstance().create(ApiService.class); apiService.getHomeArticleList(pageNumber).enqueue(new AbstractRetrofitCallback() { @Override public void getSuccessful(String responseBody) { Gson gson = new Gson(); HomeArticleListBean homeArticleListBean = gson.fromJson(responseBody, HomeArticleListBean.class); listener.getDataSuccess(homeArticleListBean); } @Override public void getFailed(String failedMsg) { listener.getDataFailed(failedMsg); } }); } } 复制代码
具体代码见: Wanandroid_Learning
同时,为了学习 Kotlin,我也开发了 Kotlin 版本,也是采用MVVM的开发模式,具体代码见: Wanandroid_Learning_Kotlin
再次说明:该项目的 API 都是来自泓洋老师的 Wanandroid开放API ,再次感谢泓洋老师的开源分享。
其实分享文章的最大目的正是等待着有人指出我的错误,如果你发现哪里有错误,请毫无保留的指出即可,虚心请教。