关闭

Android--MVP架构设计Listview

标签: mvplistviewAndroid
50人阅读 评论(1) 收藏 举报
分类:

MVP:Model View Presenter
Model:业务逻辑(加载数据)
View:视图(显示数据)
Presenter:中间者(绑定Model和View)

实现效果Demo运行效果

创建View 的接口

public interface JView {
    /*加载进度条*/
    void showLoading();
    /*显示数据*/
    void showData(List<JEntityData> datas);
}

创建Model的接口

public interface JModel {
    /**
     * 加载数据
     */
    void loadData(DataOnLoadListener listener);
    /**
     *加载完成的监听
     */
    interface DataOnLoadListener{
        void  onComplete(List<JEntityData> datas);

    }
}

创建ModelImpl的接口

public class JModelImpl implements JModel{
    @Override
    public void loadData(DataOnLoadListener listener) {
        List<JEntityData> datas=new ArrayList<JEntityData>();
        datas.add(new JEntityData("NAME11111111111111",1,"123"));
        datas.add(new JEntityData("NAME111222222222221",1,"123"));
        datas.add(new JEntityData("NAME111222222222221",1,"123"));
        datas.add(new JEntityData("NAME111222222222221",1,"123"));
        datas.add(new JEntityData("NAME111222222222221",1,"123"));
        datas.add(new JEntityData("NAME111222222222221",1,"123"));
        datas.add(new JEntityData("NAME111222222222221",1,"123"));

        //回调监听
        listener.onComplete(datas);
    }

}

创建Presenter接口

public class JPresenter extends BasePresenter<JView> {
//    view interface
    JView mView;
//    model interface
    JModel mModel =new JModelImpl();
/**
 * 通过构造方法实例化mView
 * */
    public JPresenter(JView mView) {
        this.mView = mView;
    }

    /*绑定view和model*/
    public void fetch(){
        //显示进度条
        mView.showLoading();
        //让Modle加载数据
        if(mModel!=null){
            mModel.loadData(new JModel.DataOnLoadListener() {
                @Override
                public void onComplete(List<JEntityData> datas) {
                    //给View显示
                    mView.showData(datas);
                }
            });
        }
    }
}

Activity代码

public class MainActivity extends MVPBaseActivity<JView,JPresenter> implements JView{
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView= (ListView) findViewById(R.id.listView_id);
//        listView.setAdapter(new ListViewAdapter(this));
        //中间者,让他触发加载数据
//    不继承 MVPBaseActivity    new JPresenter(this).fetch();
        mPresenter.fetch();
    }

    @Override
    protected JPresenter createPresenter() {
        return new JPresenter(this);
    }

    @Override
    public void showLoading() {
        Toast.makeText(this,"正在加载",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showData(List<JEntityData> datas) {
        listView.setAdapter(new ListViewAdapter(this,datas));
    }


}

为了防止View进程在Model(加载数据)未完成时,View被回收。需要将actvity继承MVPBaseActivity以及persenter继承BasePersenter 代码如下:
BasePresenter代码

public abstract class BasePresenter<T> {
  /*
  * 当内存不足可释放内存
  * */
    protected WeakReference<T> mViewRef;
    /*
    * 绑定presenter View
    * */
    public void attchView(T view){
        mViewRef=new WeakReference<T>(view);
    }

    /*
    * 解除管理
    * */
    public void detachView(){
        if (mViewRef!=null){
            mViewRef.clear();
            mViewRef=null;
        }
    }

    protected T getView(){
        return mViewRef.get();
    }
}

MVPBaseActivity代码

public abstract class MVPBaseActivity<A,T extends BasePresenter<A>> extends AppCompatActivity {
    protected T mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        创建Presenter
        mPresenter=createPresenter();
//        内存泄漏
//        关联view
        mPresenter.attchView((A) this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        解除关联
        mPresenter.detachView();
    }

    protected abstract T createPresenter();
}

Adapter代码 (实现简单的2个TextView)

public class ListViewAdapter extends BaseAdapter{
    private List<JEntityData> list;
    private LayoutInflater inflater;
    public ListViewAdapter(Context context,List<JEntityData> list) {
        this.inflater=LayoutInflater.from(context);
        this.list=list;
    }


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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder=null;
        if (view==null){
            viewHolder=new ViewHolder();
            view=inflater.inflate(R.layout.item_list,null);
            viewHolder.name= (TextView) view.findViewById(R.id.name);
            viewHolder.text= (TextView) view.findViewById(R.id.text);
            view.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder) view.getTag();
        }
        viewHolder.name.setText(list.get(i).getName());
        viewHolder.name.setText(list.get(i).getPhoto());
        return view;
    }

    class ViewHolder{
        TextView name;
        TextView text;
    }
}
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:51次
    • 积分:11
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论