写点什么

一篇文章教你读懂 UI 绘制流程我的 Android 重构之旅:框架篇

用户头像
Android架构
关注
发布于: 2021 年 11 月 05 日


我们这里可以看到 BaseActivity 实现出的全部能力都与 View 相关,可能这会感到奇怪,不是有实现?ViewResult?跨界面刷新这个业务能力吗?我们来看下它是如何实现的。


/**


  • 全局刷新*/@Overridepublic void resultAll() {presenter.resultAll();}


/**


  • 部分刷新

  • @param resultData*/@Overridepublic void result(Map<String, String> resultData) {presenter.result(resultData);}


这里可以看到,我们委托了?presenter?去实现,保证了 BaseActivity 只存在 View 相关的操作。


BaseListActivity


public abstract class ActivityListBase extends ActivityBase implements ActivityRecyclerMvp {private RecyclerView rvIndexRecycler = null;private SmartRefreshLayout srlRefresh = null;private MultiTypeAdapter adapter = null;private PresenterListBase presenter = null;


@Overrideprotected final int getLayout() {return R.layout.activity_recycler_base;}


@Overrideprotected final void onBeforeInit(Bundle savedInstanceState, Intent intent) {presenter = getPresenter();presenter.onCreate(savedInstanceState);}


@Overrideprotected final void onInitComponen


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


t() {rvIndexRecycler = findViewById(R.id.rv_index_recycler);srlRefresh = findViewById(R.id.srl_index_refresh);onInitRecycler();onInitListComponent();}


@Overrideprotected final void onInitViewListener() {onInitRefresh();}


@Overrideprotected final void onLoadHttpData() {presenter.getData(PresenterListBase.INIT);}


/**


  • 初始化刷新布局*/protected final void onInitRefresh() {srlRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {@Overridepublic void onLoadMore(RefreshLayout refreshLayout) {presenter.getData(PresenterListBase.LOAD_MORE);}});srlRefresh.setOnRefreshListener(new OnRefreshListener() {@Overridepublic void onRefresh(RefreshLayout refreshLayout) {srlRefresh.setEnableLoadMore(true);srlRefresh.setNoMoreData(false);presenter.getData(PresenterListBase.REFRESH);}});}


/**


  • 初始化 Recycler*/protected final void onInitRecycler() {RecyclerView.LayoutManager layoutManager = getLayoutManager();rvIndexRecycler.setLayoutManager(layoutManager);rvIndexRecycler.setHasFixedSize(false);adapter = new MultiTypeAdapter(presenter.providerData());addRecyclerItem(adapter);rvIndexRecycler.setAdapter(adapter);}}


PresenterViewListImpl


public abstract class PresenterViewListImpl<T extends RespBase> implements PresenterListBase {


protected ActivityRecyclerMvp viewBase = null;// 布局内容 protected List<Object> data = null;// 布局起点 protected int pageStart = 1;// 加载更多 protected final int pageSize = PAGE_MAX_SIZE;// 加载数据类型 protected @LoadDataStateint loadState;


public PresenterViewListImpl(ActivityListBase activityListBase) {viewBase = activityListBase;data = new ArrayList<>();}


@Overridepublic void onCreate(Bundle savedInstanceState) {


}


@Overridepublic void result(Map<String, String> resultData) {RunTimeUtil.runTimeException("未实现 result 接口");}


@Overridepublic void resultAll() {RunTimeUtil.runTimeException("未实现 resultAll 接口");}


@Overridepublic void getData(int state) {loadState = state;switch (loadState) {case INIT: {processPreInitData();break;}case REFRESH: {pageStart = 1;break;}case LOAD_MORE: {pageStart = pageStart + 1;break;}}// 加载网络数据 loadData(new OnLoadDataListener<T>() {@Overridepublic void loadDataComplete(T t) {handleLoadData(loadState, t);}


@Overridepublic void loadDataError(@StringRes int errorInfo) {handleLoadDataError(loadState, errorInfo);}


@Overridepublic void loadDataEnd() {handleLoadDataEnd();}});}


/**


  • 开始加载*/protected final void processPreInitData() {pageStart = 1;viewBase.switchLoadLayout();}


/**


  • 处理加载完成的数据

  • @param loadState

  • @param t*/protected void handleLoadData(int loadState, T t) {switch (loadState) {case INIT: {viewBase.switchContentLayout();initView(t);break;}case REFRESH: {viewBase.finishRefresh();initView(t);break;}case LOAD_MORE: {viewBase.finishRefreshLoadMore();break;}}}


/**


  • 处理加载错误的情况

  • @param loadState

  • @param errorInfo*/protected void handleLoadDataError(int loadState, int errorInfo) {switch (loadState) {case INIT: {viewBase.switchReLoadLayout(errorInfo);break;}case REFRESH: {ToastUtil.showToast(viewBase.getContext(), viewBase.getContext().getString(errorInfo));viewBase.finishRefresh();break;}case LOAD_MORE: {pageStart = pageStart - 1;ToastUtil.showToast(viewBase.getContext(), viewBase.getContext().getString(errorInfo));viewBase.finishRefreshLoadMore();break;}


}}


protected void handleLoadDataEnd() {


}


@Overridepublic void onDestroy() {viewBase = null;data = null;}


@Overridepublic List<?> providerData() {return data;}


public abstract void loadData(OnLoadDataListener loadDataListener);


public abstract void initView(T t);


public void presenterLoadMoreData(T t) {


}


public interface OnLoadDataListener<Q extends RespBase> {


public void loadDataComplete(Q q);


public void loadDataError(@StringRes int errorInfo);


public void loadDataEnd();}}

Show Code

下面我们来针对一个简单的数据列表,使用全新的框架开发试试。


public class InformationListActivity extends BaseListActivity {


@InjectInformationActivityContract.Presenter mPresenter;


@Overridepublic void injectAndInit() {// 接口适配层 DaggerInformationListActivityComponent.builder().activeInformationActivityModule(new InformationModule(this)).build().inject(this);}


@Overridepublic BaseListPresenter getBaseListPresenter() {return mPresenter;}


@Overrideprotected void registerItem(MultiTypeAdapter adapter) {// 展示多 RecyclerViewadapter.register(ActiveDetailInfo.class,new ActiveAllListProvider(mActivity));adapter.register(NoMoreDataBean.class,new NoMoreDataProvider());}}


可以看到,我们很干净的抽离出了 View,接下来我们看看 Presenter 是如何实现的


public class InformationActivityPresenterImpl extends BaseListPresenterImpl<ResponseBean<ZoneActiveBean>> implements InformationActivityContract.Presenter {

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
一篇文章教你读懂UI绘制流程我的Android重构之旅:框架篇