MVP框架+OKGO+RecyclerView+契约类

在这里插入图片描述V层

package com.example.recyclerview_demo.ui.activity;

        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.RecyclerView;
        import android.support.v7.widget.StaggeredGridLayoutManager;
        import android.view.View;
        import android.widget.Toast;

        import com.chad.library.adapter.base.BaseQuickAdapter;
        import com.example.recyclerview_demo.R;
        import com.example.recyclerview_demo.beans.JsonBean;
        import com.example.recyclerview_demo.di.contract.IMainContract;
        import com.example.recyclerview_demo.di.presenter.MainPresenter;
        import com.example.recyclerview_demo.ui.adapter.MainAdapter;

        import java.util.List;

        import butterknife.BindView;
        import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements IMainContract.MainView {


    @BindView(R.id.rv_container)
    RecyclerView rvContainer;
    private MainPresenter presenter;

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

        presenter = new MainPresenter();
        presenter.attachView(this);
        presenter.requestData();
    }

    public void showData(JsonBean jsonBean) {
        List<JsonBean.NewslistBean> newslist = jsonBean.getNewslist();
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        rvContainer.setLayoutManager(manager);
        MainAdapter adapter = new MainAdapter(R.layout.recyclerview_item_layout, newslist);
        rvContainer.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "单击", Toast.LENGTH_SHORT).show();
            }
        });
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "长安", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

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

P层

package com.example.recyclerview_demo.di.presenter;

import com.example.recyclerview_demo.beans.JsonBean;
import com.example.recyclerview_demo.di.contract.IMainContract;
import com.example.recyclerview_demo.di.model.MainModel;

import java.lang.ref.SoftReference;

/**
 * 作者:${橘子丶} on 2019/1/1 13:03
 * <p>
 * 邮箱:1242918242@qq.com
 */
public class MainPresenter implements IMainContract.MainPresenter {
    IMainContract.MainView mainView;
    private MainModel model;
    private SoftReference<IMainContract.MainView> softReference;

    @Override
    public void attachView(IMainContract.MainView mainView) {
        this.mainView = mainView;

        softReference = new SoftReference<>(mainView);

        model = new MainModel();
    }

    @Override
    public void detachView(IMainContract.MainView mainView) {
        softReference.clear();
    }

    @Override
    public void requestData() {
        model.responseData(new IMainContract.MainModel.CallBack() {

            @Override
            public void onCallBack(JsonBean jsonBean) {
                mainView.showData(jsonBean);
            }

        });
    }
}

M层

package com.example.recyclerview_demo.di.model;

import com.example.recyclerview_demo.beans.JsonBean;
import com.example.recyclerview_demo.data.Constant;
import com.example.recyclerview_demo.di.contract.IMainContract;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * 作者:${橘子丶} on 2019/1/1 13:03
 * <p>
 * 邮箱:1242918242@qq.com
 */
public class MainModel implements IMainContract.MainModel {

    @Override
    public void responseData(final CallBack callBack) {
        OkGo.<String>get(Constant.URL).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String string1 = response.body().toString();
                Gson gson = new Gson();
                JsonBean jsonBean = gson.fromJson(string1, JsonBean.class);
                callBack.onCallBack(jsonBean);
            }
        });
    }
}

Contract

package com.example.recyclerview_demo.di.contract;

import com.example.recyclerview_demo.beans.JsonBean;

/**
 * 作者:${橘子丶} on 2019/1/1 12:56
 * <p>
 * 邮箱:1242918242@qq.com
 */
public interface IMainContract {
    //V层
    public interface MainView{

      public void showData(JsonBean jsonBean);
    };

    //P层
    public interface MainPresenter<MainView>{

      public void attachView(IMainContract.MainView mainView);

      public void detachView(IMainContract.MainView mainView);

      public void requestData();
    };

    //M层
    public interface MainModel{

        public void responseData(CallBack callBack);

        public interface CallBack{

            public void onCallBack(JsonBean jsonBean);
        }
    }
}

适配器

package com.example.recyclerview_demo.ui.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.recyclerview_demo.R;
import com.example.recyclerview_demo.beans.JsonBean;
import java.util.List;

/**
 * 作者:${橘子丶} on 2019/1/1 13:20
 * <p>
 * 邮箱:1242918242@qq.com
 */
public class MainAdapter extends BaseQuickAdapter<JsonBean.NewslistBean, BaseViewHolder> {
    public MainAdapter(int layoutResId, @Nullable List data) {
        super(layoutResId, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, JsonBean.NewslistBean item) {
        helper.setText(R.id.tv_title,item.getDescription());
        Glide.with(mContext).load(item.getPicUrl()).into((ImageView) helper.getView(R.id.iv_icon));
        Glide
        .with(context)
        .load(UsageExampleListViewAdapter.eatFoodyImages[0])
        .placeholder(R.mipmap.ic_launcher) // 占位图
        .into(imageViewPlaceholder);
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值