MVP框架

/**
 * @Author: zhang
 * @Date: 2019/3/17 17:37
 * @Description:接口类
 */
public interface MyInterface {
    //view层接口
    public interface ViewInterface{
        public void reForDisplay(List vList);
    }
    //presenter层接口
    public interface PresenterInterface{
        public void toModel();
        public void onDestroy();
    }
    //model层接口
    public interface ModelInterface{
        public void toRequest(String url, MyModel.MyCallBack myCallBack);
    }
}

/**
 * @Author: zhang
 * @Date: 2019/3/17 17:37
 * @Description:model用来处理数据
 */
public class MyModel implements MyInterface.ModelInterface {
    MyCallBack mCallBack;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String json = (String) msg.obj;
            //gson解析传过来的数据
            Gson gson = new Gson();
            Product bean = gson.fromJson(json,Product.class);
            mCallBack.success(bean);
        }
    };
    @Override
    public void toRequest(String url,MyCallBack myCallBack) {
        mCallBack =  myCallBack;
        //工具类
        OkHttpUtil.getInstance().doGet(url,handler);
    }

    public interface MyCallBack{
        
        public void success(Object obj);
    }
}
/**
 * @Author: zhang
 * @Date: 2019/3/17 17:37
 * @Description:presenter用来处理逻辑业务
 */
public class MyPresenter implements MyInterface.PresenterInterface {
    获取model层的对象
    MyInterface.ModelInterface modelInterface;
    //获取view层对象
    MyInterface.ViewInterface viewInterface;
    T tt;

    public MyPresenter(T tt) {
        modelInterface = new MyModel();
        this.tt = tt;
    }

    @Override
    public void toModel() {
        //初始化接口
        viewInterface = (MyInterface.ViewInterface) tt;
        //调用请求数据的方法
        modelInterface.toRequest("http://365jia.cn/news/api3/365jia/news/headline?page=1", new MyModel.MyCallBack() {
            @Override
            public void success(Object obj) {
                if (obj instanceof Product){
                    //拿到返回数据(list)
                    //通过接口对象吧list传到view层
                    Product bean = (Product) obj;
                    viewInterface.reForDisplay(bean.getData().getExtras().getSlider());
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        if (viewInterface != null){
            viewInterface = null;
        }
    }
}
/**
 * view层初始化显示页面
 */
public class MainActivity extends AppCompatActivity implements MyInterface.ViewInterface {
    List list = new ArrayList<>();
    MyInterface.PresenterInterface presenterInterface;
    RecyclerView recyclerView;
    MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        presenterInterface = new MyPresenter(this);
        init();
    }

    private void init() {
        recyclerView = findViewById(R.id.recyclerView_id);
        //实例化布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //衔接recyclerView
        recyclerView.setLayoutManager(layoutManager);
        //设置管理器显示方式
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //创建适配器
        adapter = new MyAdapter(list,this);
        recyclerView.setAdapter(adapter);
        //请求数据
        presenterInterface.toModel();
    }

    @Override
    public void reForDisplay(List vList) {
        //将数据添加到集合
        list.addAll(vList);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenterInterface.onDestroy();
        presenterInterface = null;
    }
}

 

/**
 * @Author: zhang
 * @Date: 2019/3/15 8:54
 * @Description:
 */
public class MyAdapter extends RecyclerView.Adapter {

    List list;
    Context context;

    public MyAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        return position%4;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout,null);
        View view1 = LayoutInflater.from(context).inflate(R.layout.item_layout2,null);
        if (getItemViewType(i) < 3){
            return new ViewHolder(view);
        }else {
            return new ViewHolder1(view1);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof ViewHolder){
            ((ViewHolder)viewHolder).textView.setText(list.get(i).getCommodityName());
            Glide.with(context).load(list.get(i).getMasterPic()).into(((ViewHolder)viewHolder).imageView);
        }else if (viewHolder instanceof ViewHolder1){
            ((ViewHolder1)viewHolder).textView1.setText(list.get(i).getCommodityName());
            Glide.with(context).load(list.get(i).getMasterPic()).into(((ViewHolder1)viewHolder).imageView1);
        }
    }



    @Override
    public int getItemCount() {
        if (list != null){
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView_id);
            imageView = itemView.findViewById(R.id.image_id);
        }
    }
    public class ViewHolder1 extends RecyclerView.ViewHolder{

        ImageView imageView1;
        TextView textView1;
        public ViewHolder1(@NonNull View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.textView_id1);
            imageView1 = itemView.findViewById(R.id.image_id1);
        }
    }
}
/**
 * @Author: zhang
 * @Date: 2019/3/17 17:45
 * @Description:
 */
public class OkHttpUtil {
    OkHttpClient okHttpClient;
    static OkHttpUtil okHttpUtil;

    private OkHttpUtil() {
        okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInstance()).build();
    }
    public static synchronized OkHttpUtil getInstance(){
        if (okHttpUtil == null){
            okHttpUtil = new OkHttpUtil();
        }
        return okHttpUtil;
    }
    public void doGet(String url, final Handler mHandler){
        Request request = new Request.Builder().get().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Message message = new Message();
                message.obj = json;
                mHandler.sendMessage(message);
            }
        });
    }

    private class MyInstance implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Response proceed = chain.proceed(request);
            return proceed;
        }
    }
}

 

你可能感兴趣的:(MVP框架)