先写布局:
activity_main.xml:
adpter_item_layout.xml:
加权限导依赖:
依赖:
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
适配器:
MyAdpter
package com.bwie.dome1.adpter;
import android.content.Context;
import android.support.annotation.NonNull;
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 com.bwie.dome1.R;
import com.bwie.dome1.bean.Product;
import java.util.List;
/**
* @Auther: lenovo
* @Date: 2019/3/13
* @Description:
*/
public class MyAdpter extends RecyclerView.Adapter {
List mList;
Context mContext;
public MyAdpter(List list, Context context) {
this.mList = list;
this.mContext = context;
}
@NonNull
@Override
public holde onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//使用布局填充类 填充布局
View view = LayoutInflater.from(mContext).inflate(R.layout.adpter_item_layout,null);
//返回 holde对象
return new holde(view);
}
//为控件设置值
@Override
public void onBindViewHolder(@NonNull MyAdpter.holde holde, int i) {
//设置标题
String title = mList.get(i).getCommodityName();
String imgUrl = mList.get(i).getMasterPic();
holde.textView.setText(title);
Glide.with(mContext).load(imgUrl).into(holde.imageView);
}
@Override
public int getItemCount() {
if (mList != null){
return mList.size();
}
return 0;
}
public class holde extends RecyclerView.ViewHolder{
public ImageView imageView;
public TextView textView;
public holde(@NonNull View itemView){
super(itemView);
imageView = itemView.findViewById(R.id.ImageView_id);
textView = itemView.findViewById(R.id.TextView_id);
}
}
}
解析数据类:bena
Product类:
package com.bwie.dome1.bean;
import java.util.List;
/**
* @Auther: lenovo
* @Date: 2019/3/13
* @Description:
*/
public class Product {
/**
* result : {"rxxp":{"commodityList":[{"commodityId":17,"commodityName":"化妆镜","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/7/1.jpg","price":31,"saleNum":0},{"commodityId":27,"commodityName":"休闲马衔扣保暖绒里棉鞋懒人鞋毛毛鞋平底女雪地靴女短靴子豆豆鞋女鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/ddx/3/1.jpg","price":139,"saleNum":0},{"commodityId":21,"commodityName":"【加绒休闲 舒适轻便】秋冬增高休闲鞋厚底棉鞋运动户外通勤简约韩版女鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/bx/4/1.jpg","price":189,"saleNum":0}],"id":1002,"name":"热销新品"},"pzsh":{"commodityList":[{"commodityId":6,"commodityName":"轻柔系自然裸妆假睫毛","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/4/1.jpg","price":39,"saleNum":0},{"commodityId":5,"commodityName":"双头两用修容笔","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/3/1.jpg","price":39,"saleNum":156},{"commodityId":15,"commodityName":"玻儿精灵美妆蛋一个","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/5/1.jpg","price":6,"saleNum":0},{"commodityId":7,"commodityName":"蓝色之恋","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/5/1.jpg","price":29,"saleNum":0}],"id":1004,"name":"品质生活"},"mlss":{"commodityList":[{"commodityId":32,"commodityName":"唐狮女鞋冬季女鞋休闲鞋子女士女鞋百搭帆布鞋女士休闲鞋子女款板鞋休闲女鞋帆布鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/fbx/1/1.jpg","price":88,"saleNum":0},{"commodityId":8,"commodityName":"Lara style蜜颊润泽腮红","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/6/1.jpg","price":19,"saleNum":0}],"id":1003,"name":"魔力时尚"}}
* message : 查询成功
* status : 0000
*/
private ResultBean result;
private String message;
private String status;
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public static class ResultBean {
/**
* rxxp : {"commodityList":[{"commodityId":17,"commodityName":"化妆镜","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/7/1.jpg","price":31,"saleNum":0},{"commodityId":27,"commodityName":"休闲马衔扣保暖绒里棉鞋懒人鞋毛毛鞋平底女雪地靴女短靴子豆豆鞋女鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/ddx/3/1.jpg","price":139,"saleNum":0},{"commodityId":21,"commodityName":"【加绒休闲 舒适轻便】秋冬增高休闲鞋厚底棉鞋运动户外通勤简约韩版女鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/bx/4/1.jpg","price":189,"saleNum":0}],"id":1002,"name":"热销新品"}
* pzsh : {"commodityList":[{"commodityId":6,"commodityName":"轻柔系自然裸妆假睫毛","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/4/1.jpg","price":39,"saleNum":0},{"commodityId":5,"commodityName":"双头两用修容笔","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/3/1.jpg","price":39,"saleNum":156},{"commodityId":15,"commodityName":"玻儿精灵美妆蛋一个","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/5/1.jpg","price":6,"saleNum":0},{"commodityId":7,"commodityName":"蓝色之恋","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/5/1.jpg","price":29,"saleNum":0}],"id":1004,"name":"品质生活"}
* mlss : {"commodityList":[{"commodityId":32,"commodityName":"唐狮女鞋冬季女鞋休闲鞋子女士女鞋百搭帆布鞋女士休闲鞋子女款板鞋休闲女鞋帆布鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/fbx/1/1.jpg","price":88,"saleNum":0},{"commodityId":8,"commodityName":"Lara style蜜颊润泽腮红","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/6/1.jpg","price":19,"saleNum":0}],"id":1003,"name":"魔力时尚"}
*/
private RxxpBean rxxp;
private PzshBean pzsh;
private MlssBean mlss;
public RxxpBean getRxxp() {
return rxxp;
}
public void setRxxp(RxxpBean rxxp) {
this.rxxp = rxxp;
}
public PzshBean getPzsh() {
return pzsh;
}
public void setPzsh(PzshBean pzsh) {
this.pzsh = pzsh;
}
public MlssBean getMlss() {
return mlss;
}
public void setMlss(MlssBean mlss) {
this.mlss = mlss;
}
public static class RxxpBean {
/**
* commodityList : [{"commodityId":17,"commodityName":"化妆镜","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/7/1.jpg","price":31,"saleNum":0},{"commodityId":27,"commodityName":"休闲马衔扣保暖绒里棉鞋懒人鞋毛毛鞋平底女雪地靴女短靴子豆豆鞋女鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/ddx/3/1.jpg","price":139,"saleNum":0},{"commodityId":21,"commodityName":"【加绒休闲 舒适轻便】秋冬增高休闲鞋厚底棉鞋运动户外通勤简约韩版女鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/bx/4/1.jpg","price":189,"saleNum":0}]
* id : 1002
* name : 热销新品
*/
private int id;
private String name;
private List commodityList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getCommodityList() {
return commodityList;
}
public void setCommodityList(List commodityList) {
this.commodityList = commodityList;
}
public static class CommodityListBean {
/**
* commodityId : 17
* commodityName : 化妆镜
* masterPic : http://172.17.8.100/images/small/commodity/mzhf/mzgj/7/1.jpg
* price : 31
* saleNum : 0
*/
private int commodityId;
private String commodityName;
private String masterPic;
private int price;
private int saleNum;
public int getCommodityId() {
return commodityId;
}
public void setCommodityId(int commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getMasterPic() {
return masterPic;
}
public void setMasterPic(String masterPic) {
this.masterPic = masterPic;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSaleNum() {
return saleNum;
}
public void setSaleNum(int saleNum) {
this.saleNum = saleNum;
}
}
}
public static class PzshBean {
/**
* commodityList : [{"commodityId":6,"commodityName":"轻柔系自然裸妆假睫毛","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/4/1.jpg","price":39,"saleNum":0},{"commodityId":5,"commodityName":"双头两用修容笔","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/3/1.jpg","price":39,"saleNum":156},{"commodityId":15,"commodityName":"玻儿精灵美妆蛋一个","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/5/1.jpg","price":6,"saleNum":0},{"commodityId":7,"commodityName":"蓝色之恋","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/5/1.jpg","price":29,"saleNum":0}]
* id : 1004
* name : 品质生活
*/
private int id;
private String name;
private List commodityList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getCommodityList() {
return commodityList;
}
public void setCommodityList(List commodityList) {
this.commodityList = commodityList;
}
public static class CommodityListBeanX {
/**
* commodityId : 6
* commodityName : 轻柔系自然裸妆假睫毛
* masterPic : http://172.17.8.100/images/small/commodity/mzhf/cz/4/1.jpg
* price : 39
* saleNum : 0
*/
private int commodityId;
private String commodityName;
private String masterPic;
private int price;
private int saleNum;
public int getCommodityId() {
return commodityId;
}
public void setCommodityId(int commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getMasterPic() {
return masterPic;
}
public void setMasterPic(String masterPic) {
this.masterPic = masterPic;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSaleNum() {
return saleNum;
}
public void setSaleNum(int saleNum) {
this.saleNum = saleNum;
}
}
}
public static class MlssBean {
/**
* commodityList : [{"commodityId":32,"commodityName":"唐狮女鞋冬季女鞋休闲鞋子女士女鞋百搭帆布鞋女士休闲鞋子女款板鞋休闲女鞋帆布鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/fbx/1/1.jpg","price":88,"saleNum":0},{"commodityId":8,"commodityName":"Lara style蜜颊润泽腮红","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/6/1.jpg","price":19,"saleNum":0}]
* id : 1003
* name : 魔力时尚
*/
private int id;
private String name;
private List commodityList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getCommodityList() {
return commodityList;
}
public void setCommodityList(List commodityList) {
this.commodityList = commodityList;
}
public static class CommodityListBeanXX {
/**
* commodityId : 32
* commodityName : 唐狮女鞋冬季女鞋休闲鞋子女士女鞋百搭帆布鞋女士休闲鞋子女款板鞋休闲女鞋帆布鞋
* masterPic : http://172.17.8.100/images/small/commodity/nx/fbx/1/1.jpg
* price : 88
* saleNum : 0
*/
private int commodityId;
private String commodityName;
private String masterPic;
private int price;
private int saleNum;
public int getCommodityId() {
return commodityId;
}
public void setCommodityId(int commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getMasterPic() {
return masterPic;
}
public void setMasterPic(String masterPic) {
this.masterPic = masterPic;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSaleNum() {
return saleNum;
}
public void setSaleNum(int saleNum) {
this.saleNum = saleNum;
}
}
}
}
}
ProductBean类:
package com.bwie.dome1.bean;
/**
* @Auther: lenovo
* @Date: 2019/3/13
* @Description:
*/
public class ProductBean {
public String commodityName;
public String masterPic;
}
工具类:util
OkhttpUtile
package com.bwie.dome1.util;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* @Auther: lenovo
* @Date: 2019/3/13
* @Description:
*/
public class OkhttpUtile {
OkHttpClient okHttpClient;
public static OkhttpUtile okhttpUtile;
//构造方法私有化
private OkhttpUtile(){
okHttpClient = new OkHttpClient();
okHttpClient.newBuilder().addInterceptor(new MyInterceptor())
.build();
}
public static synchronized OkhttpUtile getInstace(){
if (okhttpUtile == null){
okhttpUtile = new OkhttpUtile();
}
return okhttpUtile;
}
public void doGet(String url, Map map, final Handler mH, final int type){
if (map != null && map.size() >0){
//遍历参数集合
String str = "";
StringBuilder builder = new StringBuilder();
for (String key : map.keySet()){
String pkey = key;
String valus = map.get(pkey);
builder.append(pkey)
.append("=")
.append(valus)
.append("&");
}
str = builder.toString();
//因为for循环遍历键值对 所以会多拼接一个&
int index = str.lastIndexOf("&");
str = str.substring(0, index);
url = url+"?" + str;
}
Request request = new Request.Builder()
.get()
.url(url)
.build();
Call call = okHttpClient.newCall(request);
//使用了OKhttp的异步请求方式
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("tag" ,"e == " + e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//请求成功 获取到json字符串
String json = response.body().string();
Message message = new Message();
message.obj = json;
message.arg1 = type;
mH.sendMessage(message);
}
});
}
public void doPost(String url,Map map , final int type , final Handler mH){
FormBody.Builder formBody = new FormBody.Builder();
for (String str : map.keySet()){
formBody.add(str,map.get(str));
}
RequestBody body = formBody.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.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;
message.arg1 = type;
mH.sendMessage(message);
}
});
}
public class MyInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException{
Request request = chain.request();
Response response = chain.proceed(request);
Log.e("tag" ,"Url = " + request.url());
Log.e("tag" ,"response = " + response.body().string());
return response;
}
}
}
MVP框架:m
MyMode1/MyModelInterFace
package com.bwie.dome1.m;
import android.os.Handler;
import android.os.Message;
import com.bwie.dome1.bean.Product;
import com.bwie.dome1.bean.ProductBean;
import com.bwie.dome1.util.OkhttpUtile;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Auther: lenovo
* @Date: 2019/3/13
* @Description:
*/
public class MyMode1 implements MyModelInterFace {
List list = new ArrayList<>();
//全局变量
MyCallBack myCallBack;
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int type = msg.arg1;
switch (type){
case 1:
String json = (String) msg.obj;
Gson gson = new Gson();
Product product = gson.fromJson(json ,Product.class );
myCallBack.succes(product);
break;
case 2:
String json_2 = (String) msg.obj;
Gson gson_2 = new Gson();
Product product_2 = gson_2.fromJson(json_2 ,Product.class );
myCallBack.succes(product_2);
break;
}
}
};
@Override
public void toRequset(String url , Map map, int type, final MyCallBack myCallBack) {
this.myCallBack = myCallBack;
OkhttpUtile.getInstace().doGet(url , map ,mHandler , type);
}
//创建接口
public interface MyCallBack{
public void succes(Object obj);
public void error(String str);
}
}
package com.bwie.dome1.m;
import java.util.Map;
/**
* @Auther: lenovo
* @Date: 2019/3/13
* @Description:
*/
public interface MyModelInterFace {
public void toRequset(String url , Map map , int type , MyMode1.MyCallBack myCallBack);
}
v
MyActivity/ViewIntface
package com.bwie.dome1.v;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.LinearLayout;
import com.bwie.dome1.R;
import com.bwie.dome1.adpter.MyAdpter;
import com.bwie.dome1.bean.Product;
import com.bwie.dome1.p.MyPresenter;
import com.bwie.dome1.p.PresenterIntface;
import java.util.ArrayList;
import java.util.List;
public class MyActivity extends Activity implements ViewIntface{
RecyclerView recyclerView;
MyAdpter myAdpter;
List mList = new ArrayList<>();
//生名p层接口对象
PresenterIntface presenterIntface;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
recyclerView = findViewById(R.id.RecyclerView_id);
//实例化布局管理器
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//设置管理器显示方式
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//衔接recyclerView
recyclerView.setLayoutManager(layoutManager);
//new 适配器
myAdpter = new MyAdpter(mList , this);
recyclerView.setAdapter(myAdpter);
//P接口对象实例化 new的是p层的实现类
presenterIntface = new MyPresenter(this);
//请求数据
presenterIntface.toModel();
}
@Override
public void refreDisply(List list) {
//数据添加到集合中
mList.addAll(list);
//刷新数据
myAdpter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
//回调p层接口把View接口对象置为null
presenterIntface.onDestroy();
//p层接口对象置为null
presenterIntface = null;
}
}
ViewIntface:
package com.bwie.dome1.v;
import com.bwie.dome1.bean.Product;
import java.util.List;
/**
* @Auther: lenovo
* @Date: 2019/3/13
* @Description:
*/
public interface ViewIntface {
public void refreDisply(List lsit);
}
p
MyPresenter/PresenterIntface
package com.bwie.dome1.p;
import android.util.Log;
import com.bwie.dome1.bean.Product;
import com.bwie.dome1.m.MyMode1;
import com.bwie.dome1.m.MyModelInterFace;
import com.bwie.dome1.v.ViewIntface;
import java.util.List;
public class MyPresenter implements PresenterIntface{
//获取model层的对象
MyModelInterFace myModel;
//获取view层的接口对象
ViewIntface viewIntface;
public MyPresenter(ViewIntface viewIntface){
myModel = new MyMode1();
//初始化接口对象
this.viewIntface = viewIntface;
}
@Override
public void toModel() {
// 调用请求数据的方法
myModel.toRequset("http://172.17.8.100/small/commodity/v1/commodityList" , null , 1 ,new MyMode1.MyCallBack() {
@Override
public void succes(Object obj) {
//拿到返回数据 list
//通过接口对象吧list 传到view层
if(obj instanceof Product){
Product product = (Product) obj;
viewIntface.refreDisply(product.getResult().getRxxp().getCommodityList());
}
}
@Override
public void error(String str) {
//拿到失败的异常信息
}
});
}
@Override
public void onDestroy() {
viewIntface = null;
}
}
PresenterIntface:
package com.bwie.dome1.p;
/**
* @Auther: lenovo
* @Date: 2019/3/13
* @Description:
*/
public interface PresenterIntface {
//去model层请求数据的方法
public void toModel();
public void onDestroy();
}