day10作业

day10作业

一、lib下的MVP层

M:model接口

package com.example.lib.model;

public interface IModel {
    //销毁
    void distroy();
}

M:BaseModel类

package com.example.lib.model;

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

    }
}



P:presenter接口

package com.example.lib.presenter;

public interface IPresenter {
    void distroy();//销毁
}


P:BasePresenter类

package com.example.lib.presenter;

import com.example.lib.model.IModel;
import com.example.lib.view.IView;

public class BasePresenter<M extends IModel,V extends IView> implements IPresenter {
    protected M mModel;
    protected V mView;

    public BasePresenter(M mModel, V mView) {
        this.mModel = mModel;
        this.mView = mView;
    }

    @Override
    public void distroy() {

    }
}


V:view接口

package com.example.lib.view;

public interface IView {
    void showToast();//输出数据
}



V:Activity接口

package com.example.lib.view;

public interface IActivity  {
    int bindLayout();//绑定
    void initView();//初始化组件
    void initData();//初始化数据
}


V:Fragment接口

package com.example.lib.view;

public interface IFragment extends IActivity {
}

V:BaseFragment类

package com.example.lib.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.lib.presenter.IPresenter;

public abstract class BaseFragment<P extends IPresenter> extends Fragment implements IFragment {
    protected P mPresenter;
    protected View inflate;


    public BaseFragment(P mPresenter, View inflate) {
        this.mPresenter = mPresenter;
        this.inflate = inflate;
    }

    protected BaseFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = inflater.inflate(bindLayout(), null);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }
    public  View findViewById(@IdRes int id) {
        return inflate.findViewById(id);
    }
}


V:BaseActivity类

package com.example.lib.view;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.lib.presenter.IPresenter;

public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity implements IActivity,IView {
    protected P mPresenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayout());
        initView();
        initData();
    }
}



二、app实现项目

contract

package com.example.app2.mvp.contract;

import com.example.app2.entity.Food;
import com.example.lib.model.IModel;
import com.example.lib.view.IView;

import retrofit2.Callback;

public interface FoodContract {
    interface FoodModel extends IModel{
        void getData(Callback<Food> callback,int num);
    }

    interface FoodView extends IView{
        void show(Food food);
    }

}


M:model

package com.example.app2.mvp.model;

import com.example.app2.entity.Food;
import com.example.app2.mvp.api.App;
import com.example.app2.mvp.contract.FoodContract;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodModel implements FoodContract.FoodModel {


    @Override
    public void distroy() {

    }

    @Override
    public void getData(Callback callback,int num) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.qubaobei.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        App app = retrofit.create(App.class);
        Call<Food> call = app.getCall();
        call.enqueue(callback);
    }
}


P:presenter

package com.example.app2.mvp.presenter;

import com.example.app2.entity.Food;
import com.example.app2.mvp.contract.FoodContract;
import com.example.lib.presenter.BasePresenter;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FoodPresenter extends BasePresenter<FoodContract.FoodModel,FoodContract.FoodView> {
    public FoodPresenter(FoodContract.FoodModel mModel, FoodContract.FoodView mView) {
        super(mModel, mView);
    }

    public void show(int num) {
        mModel.getData(new Callback<Food>() {
            @Override
            public void onResponse(Call<Food> call, Response<Food> response) {
                Food food1 = response.body();
                mView.show(food1);
            }

            @Override
            public void onFailure(Call<Food> call, Throwable t) {

            }
        },num);
    }

}

万能适配器

package com.example.app2.adapter;

import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.app2.R;
import com.example.app2.entity.Food;

import java.util.List;

public class FoodAdapter extends BaseQuickAdapter<Food.DataBean, BaseViewHolder> {
    //大虾串适配器
    public FoodAdapter(int layoutResId, @Nullable List<Food.DataBean> data) {
        super(layoutResId, data);
    }

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



实现类

package com.example.app2.fragment;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.app2.MainActivity;
import com.example.app2.R;
import com.example.app2.adapter.FoodAdapter;
import com.example.app2.entity.Food;
import com.example.app2.mvp.contract.FoodContract;
import com.example.app2.mvp.model.FoodModel;
import com.example.app2.mvp.presenter.FoodPresenter;
import com.example.lib.view.BaseFragment;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

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

/**
 * A simple {@link Fragment} subclass.
 */
public class OneFragment extends BaseFragment<FoodPresenter> implements FoodContract.FoodView {

    private SmartRefreshLayout sm;
    private RecyclerView rv;
    private int num=1;
    private FoodPresenter foodPresenter;
    private List<Food.DataBean> list=new ArrayList<>();
    private FoodAdapter foodAdapter;

    public OneFragment(FoodPresenter mPresenter, View inflate) {
        super(mPresenter, inflate);
    }
    public OneFragment() {
        // Required empty public constructor
    }


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

    @Override
    public void initView() {
        sm = (SmartRefreshLayout) findViewById(R.id.sm);
        rv = (RecyclerView) findViewById(R.id.rv);
    }

    @Override
    public void initData() {
        foodPresenter = new FoodPresenter(new FoodModel(), this);
        foodPresenter.show(1);
//        mPresenter.show(1);
    }


    @Override
    public void show(Food food) {
//        Toast.makeText(getActivity(), ""+food.toString(), Toast.LENGTH_SHORT).show();
        list.addAll(food.getData());
        foodAdapter = new FoodAdapter(R.layout.item, list);
        rv.setAdapter(foodAdapter);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        foodAdapter.notifyDataSetChanged();
        sm.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onl oadMore(@NonNull RefreshLayout refreshLayout) {
                num++;
                Toast.makeText(getActivity(), ""+num, Toast.LENGTH_SHORT).show();
                foodAdapter.notifyDataSetChanged();
                sm.finishLoadMore();
                foodPresenter.show(num);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                list.clear();
                num=1;
                Toast.makeText(getActivity(), ""+num, Toast.LENGTH_SHORT).show();
                foodAdapter.notifyDataSetChanged();
                sm.finishRefresh();
                foodPresenter.show(num);
            }
        });
    }

    @Override
    public void showToast() {

    }
}


效果图

day10作业

上一篇:蓝桥杯 拼接平方数 day10


下一篇:day10