先看效果
首先要添加依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
网络权限也不能忘
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
先看看MVP的分包
先看Model层的IRecyModel
import okhttp3.Callback; /** * Created by BAIPEI on 2017/12/7. */ public interface IRecyModel { void recy(Callback callback); }
再看IRecyModel接口的实现类
接下来我们来看view层 IRecyViewimport bwie.com.mvp.utils.OkHttpUtils; import okhttp3.Callback; /** * Created by BAIPEI on 2017/12/7. */ public class RecyModel implements IRecyModel { @Override public void recy(Callback callback) { OkHttpUtils.getInstance().doGet("http://m.yunifang.com/yunifang/mobile/home", callback); } }
下面就到了 Presenter层import bwie.com.mvp.bean.InfoDatas; /** * Created by BAIPEI on 2017/12/7. */ public interface IRecyView { void showRecy(InfoDatas infoDatas); }
import android.util.Log; import com.google.gson.Gson; import java.io.IOException; import bwie.com.mvp.bean.InfoDatas; import bwie.com.mvp.model.IRecyModel; import bwie.com.mvp.model.RecyModel; import bwie.com.mvp.utils.OnUiCallback; import bwie.com.mvp.view.IRecyView; import okhttp3.Call; /** * Created by BAIPEI on 2017/12/7. */ public class RecyPresenter { private IRecyView view; private IRecyModel model; public RecyPresenter(IRecyView view) { this.view = view; model = new RecyModel(); } public void showRecy(){ model.recy(new OnUiCallback() { private InfoDatas infoDatas; @Override public void onFailed(Call call, IOException e) { } @Override public void onSuccess(String result) throws IOException { Log.i("ss",result); Gson gson = new Gson(); infoDatas = gson.fromJson(result, InfoDatas.class); view.showRecy(infoDatas); } }); } //解绑 public void onDestory(){ view = null; } }
OkHttpUtils
切记这里的 Handler 导的是 import android.os.Handler;这个包import android.os.Handler; import android.os.Looper; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; /** * Created by BAIPEI on 2017/12/7. */ public class OkHttpUtils { private Handler handler = new Handler(Looper.getMainLooper()); public Handler getHandler(){ return handler; } //单例 private static OkHttpUtils okHttpUtils = new OkHttpUtils(); private OkHttpUtils(){}; public static OkHttpUtils getInstance(){ return okHttpUtils; } private OkHttpClient client; private void initOkHttpClient(){ if(client == null){ client = new OkHttpClient.Builder().build(); } } //公用的get请求方法 完成的功能不确定 public void doGet(String url, Callback callback){ initOkHttpClient(); Request request = new Request.Builder().addHeader("User-Agent","").url(url).build(); Call call = client.newCall(request); call.enqueue(callback); } }
OnUiCallback
import android.os.Handler; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; /** * Created by BAIPEI on 2017/12/7. */ public abstract class OnUiCallback implements Callback { private Handler handler = OkHttpUtils.getInstance().getHandler(); public abstract void onFailed(Call call, IOException e); public abstract void onSuccess(String result) throws IOException; @Override public void onFailure(final Call call, final IOException e) { //该方式 存在问题 网络请求也跑到了主线程 待解决 //该方法就是把 线程post到handler所在的线程 handler.post(new Runnable() { @Override public void run() { onFailed(call, e); } }); } @Override public void onResponse(final Call call, final Response response) throws IOException { final String result = response.body().string(); //该方式 存在问题 网络请求也跑到了主线程 待解决 handler.post(new Runnable() { @Override public void run() { try { onSuccess(result); } catch (IOException e) { e.printStackTrace(); } } }); } }
到了RecyActivity下面是适配器Adapterimport android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import com.jcodecraeer.xrecyclerview.XRecyclerView; import bwie.com.mvp.adapder.MyRecyAdapter; import bwie.com.mvp.bean.InfoDatas; import bwie.com.mvp.presenter.RecyPresenter; import bwie.com.mvp.view.IRecyView; public class RecyActivity extends AppCompatActivity implements IRecyView { private XRecyclerView xrv; private RecyPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recy); initView(); presenter = new RecyPresenter(this); presenter.showRecy(); } private void initView() { xrv = (XRecyclerView) findViewById(R.id.xrv); LinearLayoutManager manager = new LinearLayoutManager(this); manager.setOrientation(LinearLayoutManager.VERTICAL); xrv.setLayoutManager(manager); //下面是加载更多的方法 } @Override public void showRecy(InfoDatas infoDatas) { MyRecyAdapter myRecyAdapter = new MyRecyAdapter(this, infoDatas); xrv.setAdapter(myRecyAdapter); } }
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import bwie.com.mvp.R; import bwie.com.mvp.bean.InfoDatas; /** * Created by BAIPEI on 2017/12/7. */ public class MyRecyAdapter extends RecyclerView.Adapter<MyRecyAdapter.MyViewHolder> { private Context context; private InfoDatas infoDatas; public MyRecyAdapter(Context context, InfoDatas infoDatas) { this.context = context; this.infoDatas = infoDatas; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.recy_item, parent, false); MyViewHolder holder = new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { Glide.with(context).load(infoDatas.data.defaultGoodsList.get(position).goods_img).into(holder.iv_item); holder.tv_item.setText(infoDatas.data.defaultGoodsList.get(position).goods_name); } @Override public int getItemCount() { return infoDatas.data.defaultGoodsList.size(); } static class MyViewHolder extends RecyclerView.ViewHolder{ ImageView iv_item; TextView tv_item; public MyViewHolder(View itemView) { super(itemView); iv_item = itemView.findViewById(R.id.iv_item); tv_item = itemView.findViewById(R.id.tv_item); } } }
下面是bean类逻辑代码就这么多,下面是布局代码import java.util.List; /** * Created by BAIPEI on 2017/12/7. */ public class InfoDatas { public int code; public String msg; public DataEntity data; public static class DataEntity { public ActivityInfoEntity activityInfo; public boolean creditRecived; public GoodsSpreeActivityEntity goodsSpreeActivity; public SecondAdEntity secondAd; public List<SubjectsEntity> subjects; public List<Ad1Entity> ad1; public List<Ad5Entity> ad5; public List<Ad8Entity> ad8; public List<DefaultGoodsListEntity> defaultGoodsList; public static class ActivityInfoEntity { public String activityAreaDisplay; public List<ActivityInfoListEntity> activityInfoList; public static class ActivityInfoListEntity { public String id; public String activityImg; public String activityType; public String activityData; public String activityDataDetail; public String activityAreaDisplay; public String countDownEnable; public String remark; public int sort; } } public static class GoodsSpreeActivityEntity { public String id; public String name; public String startDate; public String endDate; public String status; public String startSeconds; public String endSeconds; public String isChecked; public List<GoodsListEntity> goodsList; public static class GoodsListEntity { public String id; public String goodsSpreeId; public String goodsId; public String goodsName; public String goodsImg; public double marketPrice; public double activityPrice; public int salesRatio; public int stockNumber; public int releaseNumber; } } public static class SecondAdEntity { public String id; public String image; public int ad_type; public int sort; public int position; public int enabled; public String ad_type_dynamic; public String ad_type_dynamic_data; public String ad_type_dynamic_detail; public GoodsEntity goods; public static class GoodsEntity { public int collect_count; public boolean reservable; public int restriction; public int restrict_purchase_num; public boolean is_coupon_allowed; public int allocated_stock; public int is_gift; } } public static class SubjectsEntity { public String id; public String title; public String detail; public String image; public String start_time; public String end_time; public int show_number; public String state; public int sort; public String descImage; public String template; public String url; public String wapUrl; public List<GoodsListEntity> goodsList; public List<String> goodsIdsList; public List<GoodsRelationListEntity> goodsRelationList; public static class GoodsListEntity { public String id; public String goods_name; public double shop_price; public double market_price; public String goods_img; public boolean reservable; public String efficacy; public int stock_number; public int restrict_purchase_num; public String goodsName; public String goodsImage; public String description; } public static class GoodsRelationListEntity { public String id; public String subject_id; public String goods_id; public String goodsName; public String goodsImage; public String description; } } public static class Ad1Entity { public String id; public String createtime; public String lastupdatetime; public String image; public int ad_type; public int sort; public int position; public int enabled; public String createuser; public String lastupdateuser; public String ad_type_dynamic; public String ad_type_dynamic_data; public String ad_type_dynamic_detail; public String show_channel; public String title; public String channelType; } public static class Ad5Entity { public String id; public String image; public int ad_type; public int sort; public int position; public int enabled; public String ad_type_dynamic; public String ad_type_dynamic_data; public String ad_type_dynamic_detail; public String show_channel; public String title; } public static class Ad8Entity { public String id; public String image; public int ad_type; public int sort; public int position; public int enabled; public String description; public String ad_type_dynamic; public String ad_type_dynamic_data; public String ad_type_dynamic_detail; public String show_channel; public String title; public GoodsEntity goods; public static class GoodsEntity { public int collect_count; public boolean reservable; public int restriction; public int restrict_purchase_num; public boolean is_coupon_allowed; public int allocated_stock; public int is_gift; } } public static class DefaultGoodsListEntity { public String id; public String goods_name; public double shop_price; public double market_price; public String goods_img; public boolean reservable; public String efficacy; public int stock_number; public int restrict_purchase_num; } } }
activity_recy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="bwie.com.mvp.RecyActivity"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xrv" android:layout_width="match_parent" android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView> </RelativeLayout>
recy_item
一个用MVP框架做的网络请求就出来了<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <ImageView android:id="@+id/iv_item" android:layout_width="50dp" android:layout_height="match_parent" /> <TextView android:id="@+id/tv_item" android:layout_width="match_parent" android:layout_height="match_parent" android:text="我是文本" android:textSize="20sp"/> </LinearLayout>