1. Introduction
Recently, we developed a very simple APP based on Wanandroid open API, using the model of Androidx + MVVM + Retrofit + dataBinding for development. The main function of the App is to browse various Android articles, as shown below:
1.1 Functional renderings display
The home page | project | Wechat official account | The knowledge system |
---|---|---|---|
The article details | my | registered | navigation |
---|---|---|---|
1.2 Project design mode
This project adopts the MVVM development mode, about the MVVM development mode, you can refer to: How to build the Android MVVM application framework, if you think it is not easy to understand, you can refer to the Android MVC MVP MVVM simple example
Typical architecture diagram of MVVM:Architecture diagram of the project:
2. Code parsing
For example: our home page, with banner advertising picture and homepage article, as shown below:
So to do this, we need to create HomeViewModel, HomeRepository, and HomeBeanFiles for HomeFragment, as follows:
See Wanandroid_Learning for the code
HomeFragment.java
public class HomeFragment extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/ / create the ViewModel
HomeViewModel homeVm = ViewModelProviders.of(this.new ViewModelFactory()).get(HomeViewModel.class);
// Observe banner data
homeVm.getHomeBannerListLd().observe(getViewLifecycleOwner(), bannerListDataObserver);
homeVm.setHomeBannerListLd();
// View Article datahomeVm.getHomeArticleListBeanLd().observe(getViewLifecycleOwner(), articleListBeanObserver); homeVm.setHomeArticleListBeanLd(); } ··· slightly ··}Copy the code
HomeViewModel.java
public class HomeViewModel extends ViewModel {
private static final String TAG = "HomeViewModel";
private MutableLiveData<HomeBannerListBean> homeBannerListLd;
private MutableLiveData<HomeArticleListBean> homeArticleListBeanLd;
public HomeViewModel(a) {
this.homeBannerListLd = new MutableLiveData<>();
this.homeArticleListBeanLd = new MutableLiveData<>();
}
public MutableLiveData<HomeBannerListBean> getHomeBannerListLd(a) {
return homeBannerListLd;
}
public void setHomeBannerListLd(a) {
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(a) {
return homeArticleListBeanLd;
}
public void setHomeArticleListBeanLd(a) {
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); }}); }}Copy the code
HomeRepository.java
public class HomeRepository {
/** * The volatile keyword is used to prevent DCL failures. * /
private volatile static HomeRepository instance;
public static HomeRepository newInstance(a) {
if (instance == null) {
synchronized (HomeRepository.class) {
if (instance == null) {
instance = newHomeRepository(); }}}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); }}); }}Copy the code
See Wanandroid_Learning for the code
Kotlin version
At the same time, in order to learn Kotlin, I also developed Kotlin version, also using MVVM development mode, see the specific code: Wanandroid_Learning_Kotlin
Project effect screenshot display:
The home page | project | Wechat official account | The knowledge system |
---|---|---|---|
The article details | my | Log in to | navigation |
---|---|---|---|
Flutter version
Now a version of Flutter has been developed, see my other article Wanandroid Flutter version
Effect display:
Again: the API of this project is from Teacher Hong Yang’s Wanandroid open API. Thanks again for teacher Hong Yang’s open source sharing.
In fact, the biggest purpose of sharing articles is to wait for someone to point out my mistakes. If you find any mistakes, please point them out without reservation and consult with an open mind.
Welcome to study and discuss, peace~