1.首先来看一看分包
先不白话那么多,来张效果图给段友们压压惊:
我们在这之前一定要做好准备工作:
a.添加联网权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
b.依赖:(带libs的依赖需要自己导入jar包,也可以自己在网上搜直接添加)
compile files('libs/glide-3.7.0.jar') compile files('libs/gson-2.2.4.jar') compile files('libs/okhttp-3.9.0.jar') compile files('libs/okio-1.13.0.jar') compile 'com.jcodecraeer:xrecyclerview:1.3.2'
2.首先来定义一个Bean实体类
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;
}
}
}
3.我们在看View层的IRecyView
public interface IRecyView {
void showRecy(InfoDatas infoDatas);
}
4.我们在看Model层的IRecyModel
public interface IRecyModel {
void recy(Callback callback);
}
5.我们再看IRecyModel接口的实现类
public class RecyModel implements IRecyModel {
@Override
public void recy(Callback callback) {
OkHttpUtils.getInstance().doGet("http://m.yunifang.com/yunifang/mobile/home", callback);
}
}
6.好了 到了Presenter层
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;
}
}
7.OkHttpUtils
public class OkHttpUtils {
private Handler handler = new Handler();
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);
}
}
8.OnUiCallback
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();
}
}
});
}
}
9.适配器Adapter
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);
}
}
}
10.RecyActivity
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);
}
}
11.布局文件
①:activity_recy.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.example.mvp2.activity.RecyActivity">
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrv"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>
②:recy_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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>