首先区分mvp分别是什么,M:Model层,处理数据。V:View层,处理视图(一般指Activity,Fragment)。P:Presenter层,处理M与V交互
以下使用一个简单的瀑布流Activity来进行说明
V层:WaterFallActivity(代码中使用了依赖注入、DataBinding、封装了基类Activity):
@AndroidEntryPoint
public class WaterFallActivity extends BaseActivity<MainModuleActivityWaterFallBinding> {
@Inject
WaterFallRefreshListViewPresenter waterFallRefreshListViewPresenter;
@Inject
WaterFallRefreshListViewAdapter waterFallRefreshListViewAdapter;
@Override
public void init(@Nullable Bundle savedInstanceState, View view) {
waterFallRefreshListViewPresenter.setActivity(WaterFallActivity.this);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
binding.ryWaterFall.setEnableLoadMore(false);
binding.ryWaterFall.setEnableRefresh(false);
binding.ryWaterFall.setLayoutManager(waterFallRefreshListViewPresenter,staggeredGridLayoutManager,waterFallRefreshListViewAdapter);
binding.ryWaterFall.loadMoreEnd();
waterFallRefreshListViewPresenter.getListData();
}
@Override
public int setLayoutId() {
return R.layout.main_module_activity_water_fall;
}
public void setData(List<WaterFallListBean> listBeans) {
waterFallRefreshListViewAdapter.addData(listBeans);
waterFallRefreshListViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
// 跳转详情页面
}
});
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.WaterFallActivity">
<include layout="@layout/title_layout_common" />
<com.pengxiang.common.widget.RefreshListView
android:id="@+id/ry_water_fall"
app:layout_constraintTop_toBottomOf="@id/title_bar"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="0dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
P层:WaterFallRefreshListViewPresenter
public class WaterFallRefreshListViewPresenter extends MvpBasePresenter<RefreshListViewActivity> {
@Inject
WaterFallRefreshListViewModel model;
@Inject
public WaterFallRefreshListViewPresenter() {
}
WaterFallActivity waterFallActivity;
public void setActivity(WaterFallActivity activity) {
waterFallActivity = activity;
model.getActivity(waterFallActivity);
}
public void getListData() {
model.getListData(new WaterFallRefreshListViewModel.OnListViewCallback() {
@Override
public void onResponseSuccess(List<WaterFallListBean> listBeans) {
waterFallActivity.setData(listBeans);
waterFallActivity.binding.ryWaterFall.loadMoreEnd();
}
@Override
public void onResponseError(String error) {
}
});
}
}
M层:
public class WaterFallRefreshListViewModel {
@Inject
public WaterFallRefreshListViewModel() {
}
public interface OnListViewCallback {
void onResponseSuccess(List<WaterFallListBean> list);
void onResponseError(String error);
}
WaterFallActivity waterFallActivity;
public void getActivity(WaterFallActivity activity) {
this.waterFallActivity = activity;
}
// 这里实际为网络请求接口,得到数据后返回给P层
public void getListData( final OnListViewCallback callback) {
String picurl1 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic41.nipic.com%2F20140512%2F18516832_134406337166_2.jpg&refer=http%3A%2F%2Fpic41.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648868034&t=cc6dcd256161b280ff1f97aa5efe5142";
String picurl2 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F1110200ZS0%2F2011100ZS0-12-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648867912&t=32d21d8e2dd8956a7646f0d57410de95";
String picurl3 = "http://t15.baidu.com/it/u=1363625410,3623888971&fm=224&app=112&f=JPEG?w=500&h=500";
String picurl4 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.shumiao.net%2Fuploads%2F20190222%2F19%2F1550835784-BhGEOluDXg.jpg&refer=http%3A%2F%2Fimage.shumiao.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648867963&t=26dc17b45ddc8c7611712714503ebe9c";
String picurl5 = "http://t15.baidu.com/it/u=8752746,1887106458&fm=224&app=112&f=JPEG?w=500&h=500";
String picurl6 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-091117%2Flrhs2wxug1ulrhs2wxug1u.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648868928&t=b8240234ad9fe065e942b7951cecdb12";
String picurl7 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F1Z9292123036409-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648867941&t=7f9426725fc6f23146d027389af0b949";
String picurl8 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.juimg.com%2Ftuku%2Fyulantu%2F110603%2F53-11060319520547.jpg&refer=http%3A%2F%2Fimg.juimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648867941&t=665249d1db6b8ab6e48a642296d72f59";
String picurl9 = "https://pics4.baidu.com/feed/b812c8fcc3cec3fd9f33d850b870643986942780.jpeg?token=4b25727ce9237a1df14d46085a20996b";
String picurl10 = "http://t13.baidu.com/it/u=392657742,326295515&fm=224&app=112&f=JPEG?w=500&h=500";
String headurl1 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.duoziwang.com%2F2016%2F12%2F14%2F14564656749.jpg&refer=http%3A%2F%2Fimg.duoziwang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648861328&t=3536cd0f5fadacdfe2e84ca9c2959aac";
String headurl2 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic2.zhimg.com%2F50%2Fv2-bba4f28ebfbabcabd47686185726e3df_hd.jpg&refer=http%3A%2F%2Fpic2.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648861328&t=617620601c2844eb189e3b32dc6ee7a6";
String headurl3 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_360_360%2F74%2F19%2Fdf%2F7419df22f9a3c1b4d408df2915064002.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648861328&t=75faac3abae4c71d8ce7c3cb6a256c3d";
String headurl4 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp6.itc.cn%2Fq_70%2Fimages03%2F20200619%2F90850510b01d46c0995b18c65dcb2569.jpeg&refer=http%3A%2F%2Fp6.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648861328&t=4a545c01180f1d22531845916af974c4";
String headurl5 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2F2021%2Fedpic_source%2F13%2Fbe%2Fd9%2F13bed91f71a45222fa8c629c77d74e29_16.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648861328&t=d863f96de4d073578de401056f309a58";
String headurl6 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2F31%2F77%2F69%2F3177690d85a6e2f37c934776b6e5125f.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648861328&t=528bd4f46a4043d8e02fa1ebc7b76e51";
String headurl7 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp7.itc.cn%2Fq_70%2Fimages03%2F20201026%2F4470a1b31439469ba35bb9a7b069bd6c.jpeg&refer=http%3A%2F%2Fp7.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648861328&t=243c06d7315d3cd05de2d1f8a376660c";
String headurl8 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic2.zhimg.com%2F50%2Fv2-04661141883819763d29180ccbd612c2_hd.jpg&refer=http%3A%2F%2Fpic2.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648867821&t=dde53e10c8595fb909cda9b1baeaf0ae";
String headurl9 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.zhimg.com%2Fv2-b6eae3250bb62fadb3d2527f466cf033_b.jpg&refer=http%3A%2F%2Fpic4.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648867821&t=d45e4b38c143c90764bcb20683a4b17e";
String headurl10 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fdingyue.ws.126.net%2F2019%2F1220%2F5f0e2134j00q2s577000sc200oo00oog00540054.jpg&refer=http%3A%2F%2Fdingyue.ws.126.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648867821&t=8ee0c1e1cdaea80d276c0daa3aed3aec";
ArrayList<WaterFallListBean> waterFallListBeans = new ArrayList<>();
WaterFallListBean waterFallListBean1 = new WaterFallListBean("于我、柒夏","我喜欢四月的风,六月的雨 ,不落的太阳和最好的你",headurl1,picurl1);
WaterFallListBean waterFallListBean3 = new WaterFallListBean("匿名情书","春天是宝藏 你也是",headurl2,picurl2);
WaterFallListBean waterFallListBean2 = new WaterFallListBean("北城不夏","有趣的地方,要和可爱的人一起走",headurl3,picurl3);
WaterFallListBean waterFallListBean4 = new WaterFallListBean("等風來","天空很蓝,太阳很暖,生活有你很甜",headurl4,picurl4);
WaterFallListBean waterFallListBean5 = new WaterFallListBean("柠檬影月","春风曾与我邀约,等梨花,枝上雪,便细雨黄昏,横笛一夜",headurl5,picurl5);
WaterFallListBean waterFallListBean6 = new WaterFallListBean("果绿森裙","碎花裙和野餐是我对春天最美好的期望",headurl6,picurl6);
WaterFallListBean waterFallListBean7 = new WaterFallListBean("柚子味的诗","换上喜欢的颜色,赴一场春天的约会",headurl7,picurl7);
WaterFallListBean waterFallListBean8 = new WaterFallListBean("七味少女","春天来得慢,春天才浪漫",headurl8,picurl8);
WaterFallListBean waterFallListBean9 = new WaterFallListBean("薄荷少年微微凉","春风绕转捎上暖意 与我撞了个满怀",headurl9,picurl9);
WaterFallListBean waterFallListBean10 = new WaterFallListBean("明月一轮","你每想念一次春天,枝头就攒了一朵花开",headurl10,picurl10);
waterFallListBeans.add(waterFallListBean1);
waterFallListBeans.add(waterFallListBean2);
waterFallListBeans.add(waterFallListBean3);
waterFallListBeans.add(waterFallListBean4);
waterFallListBeans.add(waterFallListBean5);
waterFallListBeans.add(waterFallListBean6);
waterFallListBeans.add(waterFallListBean7);
waterFallListBeans.add(waterFallListBean8);
waterFallListBeans.add(waterFallListBean9);
waterFallListBeans.add(waterFallListBean10);
callback.onResponseSuccess(waterFallListBeans);
}
}
Adapter:
public class WaterFallRefreshListViewAdapter extends BaseQuickAdapter<WaterFallListBean, BaseViewHolder> {
@Inject
public WaterFallRefreshListViewAdapter() {
super(R.layout.main_module_adapter_water_fall_listview_item);
}
@Override
protected void convert(@NonNull BaseViewHolder helper, WaterFallListBean item) {
ImageView ivpic = helper.getView(R.id.iv_pic);// 封面图片
ImageView ivhead = helper.getView(R.id.iv_my_dead_picture);// 头像
int screenWidth = ScreenUtils.getScreenWidth(mContext);
int width = (screenWidth-20)/2;
GlideUtils.disPlay(mContext,item.getHeadurl(),ivhead);
GlideUtils.disPlayZoom(mContext,item.getPicurl(),ivpic,width);
helper.setText(R.id.tv_title, item.getTitle());
helper.setText(R.id.tv_nick, item.getNick());
}
}
item布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_7"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginRight="@dimen/dp_5"
android:layout_marginBottom="@dimen/dp_7"
android:elevation="@dimen/dp_2"
android:background="@drawable/main_module_shape_ffffff_5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.pengxiang.common.widget.RoundImageView
android:id="@+id/iv_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cornerTopLeftRadius="@dimen/dp_5"
app:cornerTopRightRadius="@dimen/dp_5"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
<TextView
android:layout_below="@id/rl_title"
android:id="@+id/tv_title"
android:padding="5dp"
android:textSize="@dimen/sp_16"
android:textColor="@color/color_3"
android:maxLines="2"
android:ellipsize="end"
tools:text="唐山月坨岛,冬季北方的封冻之海"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/bottom"
android:layout_width="wrap_content"
android:layout_height="40dp">
<com.pengxiang.common.widget.RoundImageView
android:id="@+id/iv_my_dead_picture"
android:layout_margin="5dp"
app:isCircle="true"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:scaleType="centerInside"
android:src="@drawable/main_module_test_head_photo"/>
<TextView
android:id="@+id/tv_nick"
android:layout_toRightOf="@id/iv_my_dead_picture"
android:padding="5dp"
android:textSize="@dimen/sp_14"
android:textColor="@color/color_3"
android:gravity="center_vertical"
android:maxLines="1"
android:ellipsize="end"
tools:text="桔子大师"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</LinearLayout>