教你如何用android mvp分层架构优雅写代码( 三 )

fragment代码如下:
package com.ym.mvpdemo.module.views.userinfo;import android.app.Activity;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import android.widget.Toast;import com.ym.mvpdemo.R;import com.ym.mvpdemo.module.contract.UserInfoContract;import com.ym.mvpdemo.module.presenter.userinfo.UserInfoFragmentPresenter;import butterknife.BindView;import butterknife.ButterKnife;/** * @className: UserInfoFragment * @classDescription: Ui层(IFragment) * @author: leibing * @createTime: 2016/8/11 */public class UserInfoFragment extends Fragment implements UserInfoContract.IUserInfoFragment {// 页面常量public final static String PAGE_INDEX = "page_index";// 页面数字private int pageIndex;// UI回调private UserInfoContract.IUserInfoFragmentPresenter mIFragmentPresenter;// 判断是否当前Fragmentprivate boolean isVisibleToUser = false;@BindView(R.id.tv_fgm)TextView fgmTv;@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);pageIndex = (int) getArguments().getSerializable(PAGE_INDEX) + 1;}@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_main, null);// 绑定ButterKnifeButterKnife.bind(this, view);fgmTv.setText("第"+ pageIndex + "页");if (isVisibleToUser) {new UserInfoFragmentPresenter(this);mIFragmentPresenter.start();}return view;}@Overridepublic void showData() {Toast.makeText(getActivity(), "这是第" + pageIndex + "个页面", Toast.LENGTH_SHORT).show();}@Overridepublic void setPresenter(UserInfoContract.IUserInfoFragmentPresenter mIFragmentPresenter) {this.mIFragmentPresenter = mIFragmentPresenter;}@Overridepublic void setUserVisibleHint(boolean isVisibleToUser) {super.setUserVisibleHint(isVisibleToUser);this.isVisibleToUser = isVisibleToUser;}}view层分析完后,接下来我们来分析压轴戏:presenter(逻辑层),逻辑层分组情况如下图所示:

教你如何用android mvp分层架构优雅写代码

文章插图
 
我们可以看到逻辑层分组里面包含一个activity逻辑层类、fragment逻辑层类 。接下来我们看下activity逻辑层类,代码如下:
package com.ym.mvpdemo.module.presenter.userinfo;import android.os.Handler;import com.ym.mvpdemo.module.contract.IActivityLifeCycle;import com.ym.mvpdemo.module.contract.UserInfoContract;import com.ym.mvpdemo.module.model.UserInfoModel;/** * @className: UserInfoActivityPresenter * @classDescription: 用户信息activity逻辑层 * @author: leibing * @createTime: 2016/8/11 */public class UserInfoActivityPresenter implements UserInfoContract.IUserInfoActivityPresenter, IActivityLifeCycle {// 用户信息activity接口private UserInfoContract.IUserInfoActivity mIUserInfoActivity;/*** 构造函数* @author leibing* @createTime 2016/08/23* @lastModify 2016/08/23* @param mIUserInfoActivity 用户信息activity接口* @return*/public UserInfoActivityPresenter(UserInfoContract.IUserInfoActivity mIUserInfoActivity) {this.mIUserInfoActivity = mIUserInfoActivity;// 设置逻辑mIUserInfoActivity.setPresenter(this);// 设置生命周期mIUserInfoActivity.setILifeCycle(this);}@Overridepublic void loadUserInfo() {String userId = mIUserInfoActivity.loadUserId();System.out.println("ddddddddddddddddddddddddddd userId = " + userId);mIUserInfoActivity.showLoading();//接口请求前显示loading//这里模拟接口请求回调-new Handler().postDelayed(new Runnable() {@Overridepublic void run() {//模拟接口返回的json,并转换为javaBeanUserInfoModel userInfoModel = new UserInfoModel("小宝", 1, "杭州");mIUserInfoActivity.showUserInfo(userInfoModel);mIUserInfoActivity.dismissLoading();}}, 3000);}@Overridepublic void start() {loadUserInfo();}@Overridepublic void onRestart() {System.out.println("ddddddddddddddddddddd present onRestart");}@Overridepublic void onCreate() {System.out.println("ddddddddddddddddddddd present onCreate");}@Overridepublic void onStart() {System.out.println("ddddddddddddddddddddd present onStart");}@Overridepublic void onResume() {System.out.println("ddddddddddddddddddddd present onResume");}@Overridepublic void onPause() {System.out.println("ddddddddddddddddddddd present onPause");}@Overridepublic void onStop() {System.out.println("ddddddddddddddddddddd present onStop");}@Overridepublic void onDestroy() {System.out.println("ddddddddddddddddddddd present onDestroy");}}上面代码主要是将activity 界面层与逻辑层关联起来、实现生命周期映射接口、实现逻辑层接口,有不懂得童鞋可以结合view层多看看就明白了,fragment逻辑层类似activity,就不多做分析了 。
童鞋们,笔者写的这个mvp是不是很简单?大家可以尝试去写下 。


推荐阅读