Day3:MVP+ButterKnife+Dagger2的使用

一.MVP框架

1.框架发展过程:

mvc --> mvp–> mvvm -->mvpvm

2.MVP框架介绍:

MVP把Activity,Framgent中的UI逻辑抽象成View接口
把业务逻辑交给Presenter主持
Model类还是原来的Model

3.使用MVP的好处:

分离了视图逻辑和业务逻辑,降低了耦合
Activity只处理生命周期的任务,代码变得更加简洁
视图逻辑和业务逻辑分别抽象到了View和Presenter的接口中去,提高代码的可阅读性
业务逻辑抽到Presenter中去,避免后台线程引用着Activity导致Activity的资源无法被系统回收从而引起内存泄露和OOM

4.结构图:

在这里插入图片描述

5.目录结构

二. lib_core核心库封装

在这里插入图片描述

1.model层:

IModel:

/**
 * 数据层的公共方法
 */
public interface IModel {
    //数据销毁方法
    void destroy();
}

BaseModel:

public class BaseModel implements IModel {
    @Override
    public void destroy() {

    }
}

2.view层:

IView:

/**
 * 视图层的顶层. 是activity和 fragment的接口层
 */
public interface IView {
    //加载提示信息
    void showLoading();

    //取消加载提示信息
    void hideLoading();

    //吐司提示
    void showToast(String msg);
}


IActivity:

/**
 * 所有Activity的顶层接口
 * 放一些页面共有的方法
 * 必须加载布局是必须的
 * 查找组件是必须的
 * 初始化数据源是必须的
 */
public interface IActivity {
    //返回布局id,返回值int类型
    int bindLayout();

    //findviewbyid
    void initView();

    //数据
    void initData();
}

BaseActivity:


//所有的activity的基类:抽象类
public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity implements IView,IActivity{
    protected P mPreenter;//注意是protected不是private

    ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayout());
        initView();
        initData();
    }
    //当页面销毁的时候,将p层销毁:内存泄漏问题
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPreenter != null){
            mPreenter.destory();
            mPreenter = null;
        }
    }

    //数据加载前显示进度条
    @Override
    public void showLoading() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("数据加载中。。。。。。");
        progressDialog.show();
    }
    //数据加载成功之后
    @Override
    public void hideLoading() {
        progressDialog.dismiss();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();
    }
}

IFragment:

public interface IFragment extends IActivity {
    //fragment中需要单独封装方法getViewById
    View getViewById(int id);
}

BaseFragment:

/**
 所有fragemnt的base基类
 */
public abstract class BaseFragment<P extends IPresenter> extends Fragment implements IView,IFragment{
    protected P mPreenter;//注意是protected不是private
    ProgressDialog progressDialog;
    View view;
    public BaseFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(bindLayout(), container, false);
        initView();
        initData();
        return view;
    }

    //当页面销毁的时候,将p层销毁:内存泄漏问题
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mPreenter != null){
            mPreenter.destory();
            mPreenter = null;
        }
    }

    //根据id查找控件
    @Override
    public View getViewById(int id) {
        return view.findViewById(id);
    }

    @Override
    public void showLoading() {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("数据加载中。。。。。。");
        progressDialog.show();

    }

    @Override
    public void hideLoading() {
        progressDialog.dismiss();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(getActivity(), ""+str, Toast.LENGTH_SHORT).show();

    }
}

3.presenter层:

IPresenter:

//持有层P层顶层接口:负责model和view通信
public interface IPresenter {
    //定义销毁的方法:内存泄漏问题
    void destory();
}


BasePresenter:

//P层:持有M和V
public class BasePresenter<M extends IModel,V extends IView> implements IPresenter{
    //注意m和v不能是private,是protected受保护,让子类访问该属性,private是不能访问
    protected  M mModel;
    protected  V mView;

    public BasePresenter(M mModel, V mView) {
        this.mModel = mModel;
        this.mView = mView;
    }
    //当p层销毁的时候我们需要将m和v销毁:内存泄漏
    @Override
    public void destory() {
        if (mModel != null){
            mModel.destory();
            mModel = null;
        }
        if(mView != null){
            mView = null;
        }
    }
}

三. app模块中请求大虾

在这里插入图片描述
FoodAdapter:

public class FoodAdapter extends BaseQuickAdapter<FoodEntity.DataBean, BaseViewHolder> {
    public FoodAdapter(int layoutResId, @Nullable List<FoodEntity.DataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FoodEntity.DataBean item) {
        ImageView imageView = helper.getView(R.id.iv);
        Glide.with(mContext).load(item.getPic()).into(imageView);
        helper.setText(R.id.tv,item.getTitle()+"");
    }
}

FoodEntity:

package com.bawei.day2_mvp2.food.entity;

import java.util.List;

public class FoodEntity {

    /**
     * ret : 1
     * data : [{"id":"8289","title":"油焖大虾","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1670","food_str":"大虾 葱 生姜 植物油 料酒","num":1670},{"id":"2127","title":"四川回锅肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1592","food_str":"猪肉 青蒜 青椒 红椒 姜片","num":1592},{"id":"30630","title":"超简单芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1552","food_str":"QQ糖 牛奶 芒果","num":1552},{"id":"9073","title":"家常红烧鱼","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1426","food_str":"鲜鱼 姜 葱 蒜 花椒","num":1426},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1420","food_str":"豆腐 新鲜红椒 青椒 葱花 油","num":1420},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1342","food_str":"瘦猪肉 生菜 豆瓣酱 干辣椒 花椒","num":1342},{"id":"46968","title":"红糖苹果银耳汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1253","food_str":"银耳 苹果 红糖","num":1253},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1224","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1224},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1151","food_str":"大米 皮蛋 猪肉 油条 香葱","num":1151},{"id":"2166","title":"蚂蚁上树","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1145","food_str":"红薯粉 肉 姜 蒜 花椒","num":1145},{"id":"2262","title":"糖醋肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg","collect_num":"1082","food_str":"猪肉 红椒 黄椒 洋葱 蛋清","num":1082},{"id":"9971","title":"鱼香豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg","collect_num":"1010","food_str":"豆腐 木耳 胡萝卜 香葱 番茄酱","num":1010},{"id":"10172","title":"干煸四季豆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg","collect_num":"993","food_str":"四季豆 干辣椒 蒜头 酱油 糖","num":993},{"id":"2685","title":"胡萝卜肉末蒸蛋","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg","collect_num":"929","food_str":"胡萝卜 肉 蛋 生抽 盐","num":929},{"id":"9972","title":"虎皮青椒","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg","collect_num":"892","food_str":"青辣椒 大蒜 香醋 白糖 生抽","num":892},{"id":"10437","title":"叉烧排骨","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg","collect_num":"806","food_str":"排骨 李锦记叉烧酱 植物油 清水 油菜","num":806},{"id":"2892","title":"\u201c五行\u201d彩蔬汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg","collect_num":"761","food_str":"黑木耳 玉米 牛蒡 胡萝卜 西兰花","num":761},{"id":"2348","title":"麻辣肉丝面","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg","collect_num":"760","food_str":"面条 肉丝 淀粉 酱油 辣椒","num":760},{"id":"10044","title":"土豆炖翅根","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg","collect_num":"758","food_str":"土豆 翅根 葱 姜 料酒","num":758},{"id":"33783","title":"美人豆浆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg","collect_num":"758","food_str":"黄豆 红豆 绿豆 黑豆 黑米","num":758}]
     */

    private int ret;
    private List<DataBean> data;

    public int getRet() {
        return ret;
    }

    public void setRet(int ret) {
        this.ret = ret;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * id : 8289
         * title : 油焖大虾
         * pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
         * collect_num : 1670
         * food_str : 大虾 葱 生姜 植物油 料酒
         * num : 1670
         */

        private String id;
        private String title;
        private String pic;
        private String collect_num;
        private String food_str;
        private int num;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

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

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }

        public String getCollect_num() {
            return collect_num;
        }

        public void setCollect_num(String collect_num) {
            this.collect_num = collect_num;
        }

        public String getFood_str() {
            return food_str;
        }

        public void setFood_str(String food_str) {
            this.food_str = food_str;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }
}

FoodContract:

//协议 规范方法名称
public interface FoodContract {
    //请求数据
    interface  IFoodModel extends IModel{
        void getFoodData(StringCallback stringCallback);
    }
    //展示数据
    interface IFoodView extends IView{
        void  showFoodData(FoodEntity foodEntity);
    }
    //p层规范方法
    interface IFoodPresenter extends IPresenter{
        void food();
    }

}

FoodModel:

//网络请求
public class FoodModel extends BaseModel implements FoodContract.IFoodModel {
    @Override
    public void getFoodData(StringCallback stringCallback) {
        OkGo.<String>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1")
                .execute(stringCallback);
    }
}

FoodPresenter:

//p持有m和v
public class FoodPresenter extends BasePresenter<FoodContract.IFoodModel,FoodContract.IFoodView> implements FoodContract.IFoodPresenter {
    public FoodPresenter(FoodContract.IFoodModel mModel, FoodContract.IFoodView mView) {
        super(mModel, mView);
    }

    @Override
    public void food() {
        mView.showLoading();   //view数据加载前展示进度条
        mModel.getFoodData(new StringCallback() {//model请求数据
            @Override
            public void onSuccess(Response<String> response) {
                String string = response.body();
                FoodEntity foodEntity = new Gson().fromJson(string, FoodEntity.class);
                mView.showFoodData(foodEntity);//view展示数据

                mView.hideLoading();//view隐藏进度条
            }
        });
    }
}

FoodActivity:

public class FoodActivity extends BaseActivity<FoodPresenter> implements FoodContract.IFoodView{
    private RecyclerView recyclerView;
    private List<FoodEntity.DataBean> list = new ArrayList<>();
    private FoodAdapter foodAdapter;


    @Override
    public int bindLayout() {
        return R.layout.activity_food;
    }

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.rv);
        foodAdapter = new FoodAdapter(R.layout.item,list);
        recyclerView.setAdapter(foodAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }
    //使用MVP请求数据
    @Override
    public void initData() {
        mPreenter = new FoodPresenter(new FoodModel(),this);
        mPreenter.food();

    }
    //展示数据
    @Override
    public void showFoodData(FoodEntity foodEntity) {
        List<FoodEntity.DataBean> data = foodEntity.getData();
        list.addAll(data);
        foodAdapter.notifyDataSetChanged();
    }
}

四.ButterKnife的使用

1.介绍

butter knife是出自JakeWharton的一个开源库,它通过注解的方式来替代android中view的相关操作。减少大量的findViewById以及setOnClickListener代码,且对性能的影响较小.

ButterKnife项目地址:https://github.com/JakeWharton/butterknife

ButterKnife的优势:
1、强大的View绑定和Click事件处理功能,简化代码,提升开发效率
2、方便的处理Adapter里的ViewHolder绑定问题
3、运行时不会影响APP效率,使用配置方便
4、代码清晰,可读性强

提示:

Butter Knife有多个版本,不同版本的初始化方法都不一样,且@Bind注解控件的后缀名也不同

2.使用

(1)依赖:

android {
  ...
  // Butterknife requires Java 8.
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  implementation 'com.jakewharton:butterknife:10.2.1'
  annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
}

(2)android studio 插件安装:

setttings–>Plugins–> 里搜 ButterKnife ,下载后,重启软件.
在这里插入图片描述

(3)activity中使用

在布局文件setContentView(R.layout.activity_buffer_knife)
加粗的斜体字体上,点击右键. -> 选择generate ->找到Buffer选项.
会弹出如下的对话框:
在这里插入图片描述
如果需要点击事件,记得给OnClick打上对勾即可.

public class BufferKnifeActivity extends AppCompatActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.btn1)
    Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buffer_knife);
        ButterKnife.bind(this);
    }


    @OnClick(R.id.btn)
    public void onViewClicked() {

    }
}

(4)fragment中使用:

public class BufferKnifeFragment extends Fragment {


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.btn)
    Button btn;

    Unbinder unbinder;

    public BufferKnifeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_buffer_knife, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        return inflate;
    }

    @OnClick(R.id.btn)
    public void onViewClicked() {
        
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}


五.Dagger2使用

https://www.jianshu.com/p/471b81ce6580

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值