项目github地址https://github.com/SuperMan-42/MVP
App based on Material Design + MVP + Rxjava + Retrofit + Okhttp + Glide + Cache + theme + others
本项目本着简洁的思想,让开发更加简单,抽取出了core做为库,可以直接引入进行快捷开发,项目仍在改进中,如果有好的建议或者发现什么问题欢迎issue,email<[email protected]>,如果感觉对你有帮助也欢迎点个star,fork,本项目仅做学习交流使用
QQ群:482866708
Download APK
(Android 5.0 or above)
1.导入core库
repositories {
maven {
url 'https://dl.bintray.com/hpw42/maven'
}
}
dependencies {
compile 'com.hpw.mvpframe.core.1.0.0'
}
2.接口定义(demo)
abstract class DailyPresenter extends CoreBasePresenter<DailyModel, DailyView> {
public abstract void getDailyData();
public abstract void startInterval();
}
interface DailyModel extends CoreBaseModel {
Observable getDailyData();
Observable getZhihuDetails(int anInt);
}
interface DailyView extends CoreBaseView {
void showContent(DailyListBean info);
void doInterval(int i);
}
model(只处理数据)
presenter(用来处理vm的业务逻辑)
view(界面交互)
3.实现model(demo)
public class DailyModel implements ZhihuContract.DailyModel {
@Override
public Observable getDailyData() {
return RxService.createApi(ZhiHuApi.class).getDailyList().compose(RxUtil.rxSchedulerHelper());
}
@Override
public Observable getZhihuDetails(int anInt) {
return RxService.createApi(ZhiHuApi.class).getDetailInfo(anInt).compose(RxUtil.rxSchedulerHelper());
}
}
4.实现presenter(demo)
public class DailyPresenter extends ZhihuContract.DailyPresenter {
private int topCount = 0;
private int currentTopCount = 0;
@Override
public void onStart() {
}
@Override
public void getDailyData() {
mRxManager.add(mModel
.getDailyData()
.subscribe(
dailyListBean -> {
mView.showContent(dailyListBean);
topCount = dailyListBean.getTop_stories().size();
}, e -> mView.showError("数据加载失败ヽ(≧Д≦)ノ")
));
}
@Override
public void startInterval() {
mRxManager.add(Observable.interval(5, TimeUnit.SECONDS)
.compose(RxUtil.rxSchedulerHelper())
.subscribe(aLong -> {
if (currentTopCount == topCount)
currentTopCount = 0;
mView.doInterval(currentTopCount++);
}
));
}
}
三者的创建无先后顺序,按自己的业务逻辑来
RxManage用于管理订阅者,观察者以及事件
发送事件:mRxManage.post(Constants.msg, user);
接受事件:mRxManage.on(Constants.msg, arg ->mView.initUserInfo((_User) arg));
5.列表的实现
public class WechatFragment extends CoreBaseFragment<WechatPresenter, WechatModel> implements ZhihuContract.WechatView {
CoreRecyclerView coreRecyclerView;
private static int pageNum = 10;
@Override
public int getLayoutId() {
return 0;
}
@Override
public View getLayoutView() {
coreRecyclerView = new CoreRecyclerView(mContext).init(new BaseQuickAdapter(R.layout.item_weichat) {
@Override
protected void convert(BaseViewHolder helper, WXItemBean item) {
Glide.with(mContext).load(item.getPicUrl()).crossFade().into((ImageView) helper.getView(R.id.iv_wechat_item_image));
helper.setText(R.id.tv_wechat_item_title, item.getTitle())
.setText(R.id.tv_wechat_item_from, item.getDescription())
.setText(R.id.tv_wechat_item_time, item.getCtime())
.setOnClickListener(R.id.ll_click, v -> {
WechatDetailsActivity.start(mContext, item.getTitle(), item.getUrl());
});
}
}).openLoadMore(pageNum, page -> mPresenter.getWechatData(pageNum, page))
.openRefresh();
return coreRecyclerView;
}
@Override
public void initUI(View view, @Nullable Bundle savedInstanceState) {
}
@Override
public void showContent(List mList) {
coreRecyclerView.getAdapter().addData(mList);
}
@Override
public void showError(String msg) {
coreRecyclerView.showLoadMoreFailedView();
}
}
无需自己创建adapter,无需自己写下拉刷新和上拉加载更多的逻辑(只要添加openLoadMore(pageSize, addDataListener) openRefresh()
即可实现刷新和加载)
无需写xml布局文件(也可写,demo里两种实现方式)只需要在getLayoutView()
中 return
new CoreRecyclerView(mContext).init(new BaseQuickAdapter(R.layout.item_weichat) {
@Override
protected void convert(BaseViewHolder helper, WXItemBean item) {
//viewholder
}
})
即可实现列表(使用recyclerviewpager也是如初简单,具体看demo,recyclerviewpager可以实现viewpager所有功能)
知乎日报API 微信精选API
原来的github账号SuperMan42被封了 都迁移到了SuperMan-42