MVP

工具类
public class OkhttpUtil {
OkHttpClient okHttpClient;
static OkhttpUtil okhttpUtil;

public OkhttpUtil(){
    okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
}
public static synchronized OkhttpUtil getIntface(){
    if(okhttpUtil==null){
        okhttpUtil = new OkhttpUtil();
    }
    return okhttpUtil;
}

public void doGet(Callback callback){
    Request request= new Request.Builder()
            .get()
            .url("http://365jia.cn/news/api3/365jia/news/headline?page=1")
            .build();

    Call call = okHttpClient.newCall(request);
    call.enqueue(callback);
}

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

}
模型层

public class MyModel {
Mycallback mycallback;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String json = (String) msg.obj;
Gson gson =new Gson();
GoodsBean goodsBean = gson.fromJson(json, GoodsBean.class);
mycallback.seuuess(goodsBean);

    }
};
public void getData(){
    OkhttpUtil okhttpUtil = OkhttpUtil.getIntface();
    okhttpUtil.doGet(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            //请求I失败
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            //请求成功
            String json = response.body().string();
            Message message = new Message();
            message.obj =json;
            handler.sendMessage(message);

        }
    });
}
public void setMyCollback(Mycallback mycallback){
    this.mycallback=mycallback;

}

public interface Mycallback{
    public void seuuess(Object obj);
}

}

presenter层

public class MyPersenter implements ConaterInterface.PresenterInterface{
MyModel myModel;
ConaterInterface.ViewInterface viewInterface;

public MyPersenter(ConaterInterface.ViewInterface viewInterface) {
    this.viewInterface = viewInterface;
    myModel = new MyModel();
}

@Override
public void toModel() {
    myModel.setMyCollback(new MyModel.Mycallback() {
        @Override
        public void seuuess(Object obj) {

          viewInterface.toData(obj);

        }
    });
    myModel.getData();
}

}

public class MainActivity extends AppCompatActivity implements ConaterInterface.ViewInterface{
ConaterInterface.PresenterInterface presenterInterface;
public RecyclerView recycler_view;
List<GoodsBean.DataBeanX.DataBean> list = new ArrayList<>();
GoodsAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recycler_view = findViewById(R.id.recycler_view);
    init();

}

主页面

private void init() {
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recycler_view.setLayoutManager(layoutManager);
    adapter = new GoodsAdapter(list,this);
    recycler_view.setAdapter(adapter);
    presenterInterface = new MyPersenter(this);
    presenterInterface.toModel();

}


@Override
public void toData(Object obj) {

    GoodsBean goodsBean = (GoodsBean) obj;
    list.addAll(goodsBean.getData().getData());
    adapter.notifyDataSetChanged();

}

}

接口
public interface ConaterInterface {

public interface ViewInterface{
    public void toData(Object obj);

}
public interface PresenterInterface{
    public void toModel();

适配器

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.Holder>{

List<ProductBean.ProductData.Product> list;
int type = 1;
Context mContext;

public ProductAdapter(List<ProductBean.ProductData.Product> list , Context context){
    this.list = list;
    this.mContext = context;
}

@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    Log.e("tag" ,"onCreateViewHolder");
    View view = null;
    if(type == 1){
        view = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.item_1_layout , null);
    }else {
        view = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.item_2_layout , null);
    }
    return new Holder(view);
}

@Override
public void onBindViewHolder(@NonNull Holder holder, int i) {
    Log.e("tag" ,"onBindViewHolder");
    if(type == 1){
        String title = list.get(i).title;
        String imagUrl = "http://365jia.cn/uploads/" + list.get(i).pics.get(0);
        if( holder != null&&holder.title_1 != null){
            holder.title_1.setText(title);
            Glide.with(mContext).load(imagUrl).into(holder.imag_1);
        }
    }else {
        String title = list.get(i).title;
        String imagUrl = "http://365jia.cn/uploads/" + list.get(i).pics_new.get(0);
        if( holder != null&&holder.title_2 != null){
            holder.title_2.setText(title);
            Glide.with(mContext).load(imagUrl).into(holder.imag_2);
        }
    }
}

@Override
public int getItemCount() {
    Log.e("tag" ,"getItemCount");
    if(list != null){
        return  list.size();
    }
    return 0;
}

//重写条目类型的回调方法
@Override
public int getItemViewType(int position) {

    List<String> plist = list.get(position).pics_new ;
    if(plist == null || plist.size() == 0){
        type = 1;
    }else {
        type = 2;
    }
    Log.e("tag" ,"getItemViewType = " + type);

    return super.getItemViewType(position);
}


public class Holder extends RecyclerView.ViewHolder{

    public TextView title_1 , title_2;
    public ImageView imag_1 , imag_2;
    public Holder(@NonNull View itemView) {
        super(itemView);
        Log.e("tag" ,"Holder");
        if(type == 1){
            title_1 = itemView.findViewById(R.id.title_1_id);
            imag_1 = itemView.findViewById(R.id.imag_1_id);
        }else {
            title_2 = itemView.findViewById(R.id.title_2_id);
            imag_2 = itemView.findViewById(R.id.imag_2_id);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值