简化版MVP的使用-以瀑布流页面为例

首先区分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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,482评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,377评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,762评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,273评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,289评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,046评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,351评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,988评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,476评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,948评论 2 324
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,064评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,712评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,261评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,264评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,486评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,511评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,802评论 2 345

推荐阅读更多精彩内容