UserInfoContract作为一个契约类,将界面层与逻辑层接口进行集中管理,便于提高接口可读性 。
契约分组分析完了,然后我们再看model,model结构图如下所示:
文章插图
至此,我们看到httprequest、data两个分组,这两分组分别对应网络请求封装和数据持久化封装,这两块封装看个人了,没有一个绝对的方案,我们还看到一个UserInfoModel,这里我就先放外面了,没放httprequest和data里面去了,这里具体封装具体放对应的位置,网络请求封装可以参考基于Retrofit、OkHttp、Gson封装通用网络框架、持久化数据封装可以参考android基于xml实现的对象缓存方案 。
接下来我们分析View层,View分组如下图所示:
文章插图
view分组就一个activity和一个fragment,其实这层很简单,主要做更新ui的工作,代码结构也比较清晰,笔者在activity里面将对应生命周期映射到了其逻辑层上面去了,这样省去了在生命周期上面 view层往逻辑层写方法的麻烦 。
activity代码如下:
package com.ym.mvpdemo.module.views.userinfo;import android.os.Bundle;import android.support.v4.App.Fragment;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.widget.TextView;import android.widget.Toast;import com.ym.mvpdemo.R;import com.ym.mvpdemo.adapter.ViewpagerAdapter;import com.ym.mvpdemo.module.contract.IActivityLifeCycle;import com.ym.mvpdemo.module.contract.UserInfoContract;import com.ym.mvpdemo.module.model.UserInfoModel;import com.ym.mvpdemo.module.presenter.userinfo.UserInfoActivityPresenter;import JAVA.util.ArrayList;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;/** * @className: UserInfoActivity * @classDescription: UI层(Activity) * @author: leibing * @createTime: 2016/8/11 */public class UserInfoActivity extends AppCompatActivity implements UserInfoContract.IUserInfoActivity{// 切换Tab常量public final static int HOME_INDEX = 0;public final static int CZH_INDEX = 1;public final static int ME_INDEX = 2;// Activity逻辑层接口private UserInfoContract.IUserInfoActivityPresenter mIActivityPresenter;// 生命周期接口private IActivityLifeCycle mIActivityLifeCycle;// Fragmentprivate UserInfoFragment mHomeFragment;private UserInfoFragment mCzhFragment;private UserInfoFragment mMineFragment;// Fragement列表private List<Fragment> mFragmentList;// 标题列表private List<String> mTitleList;@BindView(R.id.tv_name) TextView nameTv;@BindView(R.id.tv_age) TextView ageTv;@BindView(R.id.tv_address) TextView addressTv;@BindView(R.id.vpg_main) ViewPager mainPager;@BindView(R.id.tv_main_home) TextView mainHomeTv;@BindView(R.id.tv_main_czh) TextView mainCzhTv;@BindView(R.id.tv_main_me) TextView mainMeTv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 绑定ButterKnifeButterKnife.bind(this);// 初始化listinitList();// 初始化FragmentinitFragment();// 初始化逻辑new UserInfoActivityPresenter(this);mIActivityPresenter.start();// View映射onCreate生命周期到PresentermIActivityLifeCycle.onCreate();}/*** 初始化列表* @author leibing* @createTime 2016/8/11* @lastModify 2016/8/11* @return*/private void initList() {mFragmentList = new ArrayList<>();mTitleList = new ArrayList<>();}/*** 初始化Fragment* @author leibing* @createTime 2016/8/11* @lastModify 2016/8/11* @param* @return*/private void initFragment() {// 首页mHomeFragment = new UserInfoFragment();Bundle bundle = new Bundle();bundle.putSerializable(UserInfoFragment.PAGE_INDEX, HOME_INDEX);mHomeFragment.setArguments(bundle);mFragmentList.add(mHomeFragment);// 车智汇mCzhFragment = new UserInfoFragment();bundle = new Bundle();bundle.putSerializable(UserInfoFragment.PAGE_INDEX, CZH_INDEX);mCzhFragment.setArguments(bundle);mFragmentList.add(mCzhFragment);// 我的mMineFragment = new UserInfoFragment();bundle = new Bundle();bundle.putSerializable(UserInfoFragment.PAGE_INDEX, ME_INDEX);mMineFragment.setArguments(bundle);mFragmentList.add(mMineFragment);// ViewPager适配ViewpagerAdapter mAdapter = new ViewpagerAdapter(getSupportFragmentManager(), mFragmentList, mTitleList);mainPager.setAdapter(mAdapter);mainPager.setOffscreenPageLimit(3);mainPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageSelected(int position) {}@Overridepublic void onPageScrollStateChanged(int state) {}@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {if (positionOffset == 0.0)switchTab(position);}});mainPager.setCurrentItem(0);}/*** 切换Tab页* @author leibing* @createTime 2016/5/6* @lastModify 2016/5/6* @param index* @return*/private void switchTab(int index){switch (index){case HOME_INDEX:mainHomeTv.setTextColor(getResources().getColor(R.color.main_home_text_blue));mainCzhTv.setTextColor(getResources().getColor(R.color.main_home_text_gray));mainMeTv.setTextColor(getResources().getColor(R.color.main_home_text_gray));break;case CZH_INDEX:mainHomeTv.setTextColor(getResources().getColor(R.color.main_home_text_gray));mainCzhTv.setTextColor(getResources().getColor(R.color.main_home_text_blue));mainMeTv.setTextColor(getResources().getColor(R.color.main_home_text_gray));break;case ME_INDEX:mainHomeTv.setTextColor(getResources().getColor(R.color.main_home_text_gray));mainCzhTv.setTextColor(getResources().getColor(R.color.main_home_text_gray));mainMeTv.setTextColor(getResources().getColor(R.color.main_home_text_blue));break;}}@Overrideprotected void onRestart() {// View映射onRestart生命周期到PresentermIActivityLifeCycle.onRestart();super.onRestart();}@Overrideprotected void onStart() {super.onStart();// View映射onStart生命周期到PresentermIActivityLifeCycle.onStart();}@Overrideprotected void onResume() {super.onResume();// View映射onResume生命周期到PresentermIActivityLifeCycle.onResume();}@Overrideprotected void onPause() {// View映射onPause生命周期到PresentermIActivityLifeCycle.onPause();super.onPause();}@Overrideprotected void onStop() {// View映射onStop生命周期到PresentermIActivityLifeCycle.onStop();super.onStop();}@Overrideprotected void onDestroy() {// View映射onDestroy生命周期到PresentermIActivityLifeCycle.onDestroy();super.onDestroy();}@Overridepublic void showLoading() {Toast.makeText(this, "正在加载", Toast.LENGTH_SHORT).show();}@Overridepublic void dismissLoading() {Toast.makeText(this, "加载完成", Toast.LENGTH_SHORT).show();}@Overridepublic void showUserInfo(UserInfoModel userInfoModel) {if (userInfoModel != null) {nameTv.setText(userInfoModel.getName());ageTv.setText(String.valueOf(userInfoModel.getAge()));addressTv.setText(userInfoModel.getAddress());}}@Overridepublic String loadUserId() {return "1000";//假设需要查询的用户信息的userId是1000}@Overridepublic void setPresenter(UserInfoContract.IUserInfoActivityPresenter mIActivityPresenter) {this.mIActivityPresenter = mIActivityPresenter;}public void setILifeCycle(IActivityLifeCycle mIActivityLifeCycle) {this.mIActivityLifeCycle = mIActivityLifeCycle;}@OnClick(R.id.ly_main_home) void mainOnClick() {mainPager.setCurrentItem(HOME_INDEX, false);}@OnClick(R.id.ly_main_czh) void czhOnClick() {mainPager.setCurrentItem(CZH_INDEX, false);}@OnClick(R.id.ly_main_me) void meOnClick() {mainPager.setCurrentItem(ME_INDEX, false);}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 新手如何运营淘宝网店 淘宝新店铺如何运营
- 淘宝怎样优化关键词 淘宝如何提升关键词排名
- 杜仲茶是什么茶,杜仲茶的春茶和秋茶如何选择
- 怎么入驻淘宝 淘宝服务商怎么入驻
- 大麦茶减肥效果如何,常喝大麦茶
- 番石榴茶如何做,玫瑰枸杞黄芪茶的做法
- 淘宝购物如何选择商家 淘宝怎么选性价比高商品
- 手机淘宝官网开店如何申请 手机淘宝怎么申请淘宝店铺步骤
- 美甲品牌以及美甲注意事项
- 如何开淘宝店步骤开淘宝网店 淘宝开店怎么开的