android之MVP设计模式详解

       MVP是微软提出的一种架构设计模式。在android应用的开发中目前使用非常广泛。分别为Modle:业务逻辑,View:视图,Presenter:中间着。(用于绑定Modle和View)Modle用来加载数据,View用来显示数据,Presenter用来绑定Modle和View的联系。

     下面来看具体的代码实现:

  一,上图看一下简单的项目结构

    

 二,定义一个GirlModle接口
/**
 * Created by 晁东洋 on 2016/10/9.
 */

public interface GirleModle {
    //加载数据
    void loadGirl( GirlonLoadListener loadListener);
    //加载完成的监听
    interface GirlonLoadListener{
       void onComplate(List<Girl> list);
    }
}
三,定义一个GrilView的接口
/**
 * Created by 晁东洋 on 2016/10/9.
 */

public interface GirlView {
    //加载进度条
    void showLoading();
    //加载数据
    void showGilrs(List<Girl> list);
}
四,定义一个Gril的bean数据源
package com.hdceping.www.mytest.bean;

/**
 * Created by 晁东洋 on 2016/10/9.
 */

public class Girl {
    private String imageUrl;
    private String title;
    private String name;

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Girl(String imageUrl, String title, String name) {
        this.imageUrl = imageUrl;
        this.title = title;
        this.name = name;
    }
}

五,定义GrileModleImpl类,实现GrileModel接口。
   
package com.hdceping.www.mytest.modle.impl;

import com.hdceping.www.mytest.bean.Girl;
import com.hdceping.www.mytest.modle.GirleModle;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 晁东洋 on 2016/10/9.
 */

public class GirleModleImpl implements GirleModle {

    @Override
    public void loadGirl(GirlonLoadListener loadListener) {
        List<Girl> data = new ArrayList<>();
        data.add(new Girl("http://a4.topitme.com/o/201012/20/12928333231233.jpg","图片","美女图片"));
        data.add(new Girl("http://tp.85814.com/d/file/meinv/2016-10-09/100567-coL4Krz.jpg","图片","美女图片"));
        data.add(new Girl("http://tp.85814.com/d/file/meinv/2016-10-09/100567-rtUVwh5.jpg","图片","美女图片"));
        data.add(new Girl("http://tp.85814.com/d/file/meinv/2016-05-31/77772-Qu2Dx3h.jpg","图片","美女图片"));
        data.add(new Girl("http://tp.85814.com/d/file/meinv/2016-10-09/100567-Olhb7Ll.jpg","图片","美女图片"));
        data.add(new Girl("http://tp.85814.com/d/file/meinv/2016-10-09/100567-coL4Krz.jpg","图片","美女图片"));
        data.add(new Girl("http://tp.85814.com/d/file/meinv/2016-10-09/100567-rtUVwh5.jpg","图片","美女图片"));
        data.add(new Girl("http://tp.85814.com/d/file/meinv/2016-05-31/77772-Qu2Dx3h.jpg","图片","美女图片"));
        //回掉完成监听
        loadListener.onComplate(data);
    }

}
六,定义GrilPersenter类,实现业务逻辑和View的绑定:
 
package com.hdceping.www.mytest.persenter;

import com.hdceping.www.mytest.bean.Girl;
import com.hdceping.www.mytest.modle.GirleModle;
import com.hdceping.www.mytest.modle.impl.GirleModleImpl;
import com.hdceping.www.mytest.view.GirlView;

import java.util.List;

/**
 * Created by 晁东洋 on 2016/10/9.
 */

public class GirlPersenter {
    GirlView mGirlView;
    GirleModle mGirleModle = new GirleModleImpl();

    public GirlPersenter(GirlView girlView) {
        mGirlView = girlView;
    }
    public void fetch(){
        //显示进度条
        mGirlView.showLoading();
        //加载数据
        if (mGirleModle != null){
            mGirleModle.loadGirl(new GirleModle.GirlonLoadListener() {
                @Override
                public void onComplate(List<Girl> list) {
                    mGirlView.showGilrs(list);
                }
            });
        }
    }
}

七,MainActivity类。
package com.hdceping.www.mytest;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.hdceping.www.mytest.adapter.ListGirlAdapter;
import com.hdceping.www.mytest.bean.Girl;
import com.hdceping.www.mytest.persenter.GirlPersenter;
import com.hdceping.www.mytest.view.GirlView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements GirlView{

   private ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.mvp_Listview);
        new GirlPersenter(this).fetch();
    }

    @Override
    public void showLoading() {
        Toast.makeText(this,"拼命加载中",Toast.LENGTH_LONG);
    }

    @Override
    public void showGilrs(List<Girl> list) {
        mListView.setAdapter(new ListGirlAdapter(this,this,list));
    }
}


八,我的布局文件。
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hdceping.www.mytest.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mvp_Listview"></ListView>
</RelativeLayout>
九,adapter的代码:
 
package com.hdceping.www.mytest.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.hdceping.www.mytest.R;
import com.hdceping.www.mytest.bean.Girl;

import java.util.List;

/**
 * Created by 晁东洋 on 2016/10/9.
 */

public class ListGirlAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<Girl> mList;
    private Activity mActivity;
    private Context mContext;

    public ListGirlAdapter(Activity activity, Context mContext, List<Girl> list) {
        mInflater = LayoutInflater.from(mContext);
        mList = list;
        mActivity = activity;
        this.mContext  = mContext;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder =null;
        if (viewHolder == null){
            convertView = mInflater.inflate(R.layout.layout_item,null);
            viewHolder = new ViewHolder();
            viewHolder.imageview_mvp = (ImageView)convertView.findViewById(R.id.imageview_mvp);
            viewHolder.text_mvp = (TextView)convertView.findViewById(R.id.text_mvp);
            viewHolder.text2_mvp = (TextView)convertView.findViewById(R.id.text2_mvp);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();

        }
        Log.e("网络地址",mList.get(position).getImageUrl());
        Glide.with(mContext).load(mList.get(position).getImageUrl()).listener(mRequestListener).error(R.mipmap.ic_launcher).into(viewHolder.imageview_mvp);
        viewHolder.text_mvp.setText(mList.get(position).getTitle());
        viewHolder.text2_mvp.setText(mList.get(position).getName());
        return convertView;
    }

    private RequestListener<String, GlideDrawable> mRequestListener = new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            //显示错误信息
            Log.e("TAG", "onException: ", e);
            //打印请求URL
            Log.e("TAG", "onException: " + model);
            //打印请求是否还在进行
            Log.e("TAG", "onException: " + target.getRequest().isRunning());
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            return false;
        }
    };

    class ViewHolder{
        private ImageView imageview_mvp;
        private TextView  text_mvp;
        private TextView  text2_mvp;
    }
}
   大家可以看到MVP设计模式,其实有大量的接口和回掉编写代码。主要是设计思路。仔细看还是能看看懂的。如果有问题一起探讨。



     
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值