效果图

vew
package com.bawei.mvp.view;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:47
*/
public interface MainView {
void sueccss(int type,String data);
void fail(int type,String error);
}
model
package com.bawei.mvp.model;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:50
*/
public interface MainModel {
interface CallBackLister{
void sueccss(int type,String data);
void fail(int type,String error);
}
void doBaner(int type,String url,CallBackLister callBackLister);
void doData(int type,String url,CallBackLister callBackLister);
}
package com.bawei.mvp.model;
import com.bawei.mvp.utils.OkHttpUtils;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:52
*/
public class MainModelIml implements MainModel {
@Override
public void doBaner(final int type, String url, final CallBackLister callBackLister) {
new OkHttpUtils().get(url).result(new OkHttpUtils.HttpListener() {
@Override
public void sueccss(String data) {
callBackLister.sueccss(type,data);
}
@Override
public void fail(String error) {
callBackLister.fail(type,error);
}
});
}
@Override
public void doData(final int type, String url, final CallBackLister callBackLister) {
new OkHttpUtils().get(url).result(new OkHttpUtils.HttpListener() {
@Override
public void sueccss(String data) {
callBackLister.sueccss(type,data);
}
@Override
public void fail(String error) {
callBackLister.fail(type,error);
}
});
}
}
presenter
package com.bawei.mvp.presenter;
import com.bawei.mvp.model.MainModel;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:53
*/
public interface MainPresenter {
void doBaner(int type,String url);
void doData(int type,String url);
}
package com.bawei.mvp.presenter;
import com.bawei.mvp.model.MainModel;
import com.bawei.mvp.view.MainView;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:54
*/
public class MainPresenterIml implements MainPresenter, MainModel.CallBackLister {
private MainModel mainModel;
private MainView mainView;
public MainPresenterIml(MainModel mainModel, MainView mainView){
this.mainModel=mainModel;
this.mainView=mainView;
}
@Override
public void doBaner(int type, String url) {
mainModel.doBaner(type,url,this);
}
@Override
public void doData(int type, String url) {
mainModel.doData(type,url,this);
}
@Override
public void sueccss(int type, String data) {
mainView.sueccss(type,data);
}
@Override
public void fail(int type, String error) {
mainView.fail(type,error);
}
public void destroy(){
if (mainView!=null){
mainView=null;
}
if (mainModel!=null){
mainModel=null;
}
System.gc();
}
}
utils
package com.bawei.mvp.utils;
import android.os.Handler;
import android.os.Message;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 10:04
*/
public class OkHttpUtils {
int HTTP_SUECCSS=200;
int HTTP_FAIL=201;
private HttpListener httpListener;
public OkHttpUtils get(String url){
doHttp(url,0,null);
return this;
}
public OkHttpUtils post(String url,int type, FormBody.Builder bodyBuilder){
doHttp(url,1,bodyBuilder);
return this;
}
private void doHttp(String url,int type,FormBody.Builder bodyBuilder) {
//拦截器
OkHttpClient client=new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request=chain.request();
return chain.proceed(request);
}
}).build();
Request.Builder builder=new Request.Builder();
if (type==0){
builder.get();
}else {
builder.post(bodyBuilder.build());
}
builder.url(url);
Request request=builder.build();
final Message message=new Message();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
message.obj=e.getMessage();
message.what=HTTP_FAIL;
handler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
message.obj=response.body().string();
message.what=HTTP_SUECCSS;
handler.sendMessage(message);
}
});
}
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==HTTP_SUECCSS){
String data= (String) msg.obj;
httpListener.sueccss(data);
}else {
String error= (String) msg.obj;
httpListener.fail(error);
}
}
};
//接口回调的方法
public void result(HttpListener httpListener){
this.httpListener=httpListener;
}
//接口
public interface HttpListener{
void sueccss(String data);
void fail(String error);
}
}
bean
package com.bawei.mvp.bean;
import com.stx.xhb.xbanner.entity.SimpleBannerInfo;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:54
*/
public class BannerBean implements BaseBean{
/**
* result : [{"imageUrl":"http://172.17.8.100/images/small/banner/cj.png","jumpUrl":"http://172.17.8.100/htm/lottery/index.html","rank":5,"title":"抽奖"},{"imageUrl":"http://172.17.8.100/images/small/banner/hzp.png","jumpUrl":"wd://commodity_list?arg=1001007005","rank":5,"title":"美妆工具"},{"imageUrl":"http://172.17.8.100/images/small/banner/lyq.png","jumpUrl":"wd://commodity_info?arg=83","rank":5,"title":"连衣裙"},{"imageUrl":"http://172.17.8.100/images/small/banner/px.png","jumpUrl":"wd://commodity_info?arg=165","rank":5,"title":"跑鞋"},{"imageUrl":"http://172.17.8.100/images/small/banner/wy.png","jumpUrl":"wd://commodity_list?arg=1001002004","rank":5,"title":"卫衣"}]
* message : 查询成功
* status : 0000
*/
private String message;
private String status;
private List<ResultBean> 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 List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean extends SimpleBannerInfo {
/**
* imageUrl : http://172.17.8.100/images/small/banner/cj.png
* jumpUrl : http://172.17.8.100/htm/lottery/index.html
* rank : 5
* title : 抽奖
*/
private String imageUrl;
private String jumpUrl;
private int rank;
private String title;
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getJumpUrl() {
return jumpUrl;
}
public void setJumpUrl(String jumpUrl) {
this.jumpUrl = jumpUrl;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public Object getXBannerUrl() {
return null;
}
}
}
package com.bawei.mvp.bean;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:54
*/
public interface BaseBean {
}
package com.bawei.mvp.bean;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:54
*/
public class HomeListBean {
/**
* result : {"rxxp":{"commodityList":[{"commodityId":5,"commodityName":"双头两用修容笔","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/3/1.jpg","price":39,"saleNum":156},{"commodityId":25,"commodityName":"秋冬季真皮兔毛女鞋韩版休闲平底毛毛鞋软底百搭浅口水钻加绒棉鞋毛毛鞋潮鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/ddx/1/1.jpg","price":158,"saleNum":0},{"commodityId":19,"commodityName":"环球 时尚拼色街拍百搭小白鞋 韩版原宿ulzzang板鞋 女休闲鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/bx/2/1.jpg","price":78,"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":3,"commodityName":"Lara style女神的魔盒全套彩妆","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/1/1.jpg","price":3499,"saleNum":2000},{"commodityId":13,"commodityName":"贝览得美妆蛋","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/3/1.jpg","price":44,"saleNum":0},{"commodityId":11,"commodityName":"盒装葫芦粉扑美妆蛋化妆海绵","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/1/1.jpg","price":9,"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":4,"commodityName":"佩佩防晕染眼线液笔","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/2/1.jpg","price":19,"saleNum":845}],"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":5,"commodityName":"双头两用修容笔","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/3/1.jpg","price":39,"saleNum":156},{"commodityId":25,"commodityName":"秋冬季真皮兔毛女鞋韩版休闲平底毛毛鞋软底百搭浅口水钻加绒棉鞋毛毛鞋潮鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/ddx/1/1.jpg","price":158,"saleNum":0},{"commodityId":19,"commodityName":"环球 时尚拼色街拍百搭小白鞋 韩版原宿ulzzang板鞋 女休闲鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/bx/2/1.jpg","price":78,"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":3,"commodityName":"Lara style女神的魔盒全套彩妆","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/1/1.jpg","price":3499,"saleNum":2000},{"commodityId":13,"commodityName":"贝览得美妆蛋","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/3/1.jpg","price":44,"saleNum":0},{"commodityId":11,"commodityName":"盒装葫芦粉扑美妆蛋化妆海绵","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/1/1.jpg","price":9,"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":4,"commodityName":"佩佩防晕染眼线液笔","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/2/1.jpg","price":19,"saleNum":845}],"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 implements BaseBean{
/**
* commodityList : [{"commodityId":5,"commodityName":"双头两用修容笔","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/3/1.jpg","price":39,"saleNum":156},{"commodityId":25,"commodityName":"秋冬季真皮兔毛女鞋韩版休闲平底毛毛鞋软底百搭浅口水钻加绒棉鞋毛毛鞋潮鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/ddx/1/1.jpg","price":158,"saleNum":0},{"commodityId":19,"commodityName":"环球 时尚拼色街拍百搭小白鞋 韩版原宿ulzzang板鞋 女休闲鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/bx/2/1.jpg","price":78,"saleNum":0}]
* id : 1002
* name : 热销新品
*/
private int id;
private String name;
private List<CommodityListBean> 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<CommodityListBean> getCommodityList() {
return commodityList;
}
public void setCommodityList(List<CommodityListBean> commodityList) {
this.commodityList = commodityList;
}
public static class CommodityListBean {
/**
* commodityId : 5
* commodityName : 双头两用修容笔
* masterPic : http://172.17.8.100/images/small/commodity/mzhf/cz/3/1.jpg
* price : 39
* saleNum : 156
*/
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 implements BaseBean{
/**
* commodityList : [{"commodityId":6,"commodityName":"轻柔系自然裸妆假睫毛","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/4/1.jpg","price":39,"saleNum":0},{"commodityId":3,"commodityName":"Lara style女神的魔盒全套彩妆","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/1/1.jpg","price":3499,"saleNum":2000},{"commodityId":13,"commodityName":"贝览得美妆蛋","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/3/1.jpg","price":44,"saleNum":0},{"commodityId":11,"commodityName":"盒装葫芦粉扑美妆蛋化妆海绵","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/mzgj/1/1.jpg","price":9,"saleNum":0}]
* id : 1004
* name : 品质生活
*/
private int id;
private String name;
private List<CommodityListBeanX> 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<CommodityListBeanX> getCommodityList() {
return commodityList;
}
public void setCommodityList(List<CommodityListBeanX> 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 implements BaseBean{
/**
* commodityList : [{"commodityId":32,"commodityName":"唐狮女鞋冬季女鞋休闲鞋子女士女鞋百搭帆布鞋女士休闲鞋子女款板鞋休闲女鞋帆布鞋","masterPic":"http://172.17.8.100/images/small/commodity/nx/fbx/1/1.jpg","price":88,"saleNum":0},{"commodityId":4,"commodityName":"佩佩防晕染眼线液笔","masterPic":"http://172.17.8.100/images/small/commodity/mzhf/cz/2/1.jpg","price":19,"saleNum":845}]
* id : 1003
* name : 魔力时尚
*/
private int id;
private String name;
private List<CommodityListBeanXX> 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<CommodityListBeanXX> getCommodityList() {
return commodityList;
}
public void setCommodityList(List<CommodityListBeanXX> 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;
}
}
}
}
}
adapter
package com.bawei.mvp.adapter;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bawei.R;
import com.bawei.mvp.bean.BannerBean;
import com.bawei.mvp.bean.BaseBean;
import com.bawei.mvp.bean.HomeListBean;
import com.bumptech.glide.Glide;
import com.stx.xhb.xbanner.XBanner;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:54
*/
public class MainAdapter extends RecyclerView.Adapter {
private Context context;
private List<BaseBean> list = new ArrayList<>();
public MainAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
int type = getItemViewType(i);
RecyclerView.ViewHolder viewHolder = null;
switch (type) {
case 0://Banner图
View view0 = View.inflate(context, R.layout.adapter_banner, null);
viewHolder = new MyViewHolder_0(view0);
break;
case 1://热销新品
View view1 = View.inflate(context, R.layout.adapter_rxxp_mlss_pzss, null);
viewHolder = new MyViewHolder_1(view1);
break;
case 2://魔力时尚
View view2 = View.inflate(context, R.layout.adapter_rxxp_mlss_pzss, null);
viewHolder = new MyViewHolder_1(view2);
break;
case 3://品质生活
View view3 = View.inflate(context, R.layout.adapter_rxxp_mlss_pzss, null);
viewHolder = new MyViewHolder_1(view3);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof MyViewHolder_0 && list.get(i) instanceof BannerBean) {//加载Banner
BannerBean bannerBean = (BannerBean) list.get(i);
((MyViewHolder_0) viewHolder).mXBanner.setBannerData(bannerBean.getResult());
((MyViewHolder_0) viewHolder).mXBanner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
BannerBean.ResultBean bean = (BannerBean.ResultBean) model;
Glide.with(context).load(bean.getImageUrl()).into((ImageView) view);
}
});
} else if (viewHolder instanceof MyViewHolder_1 &&
list.get(i) instanceof HomeListBean.ResultBean.RxxpBean) {//热销新品
HomeListBean.ResultBean.RxxpBean rxxpBean = (HomeListBean.ResultBean.RxxpBean) list.get(i);
((MyViewHolder_1) viewHolder).mTitle.setText(rxxpBean.getName());
((MyViewHolder_1) viewHolder).mLayoutTitle.setBackgroundColor(Color.WHITE);
RxxpAdapter rxxpAdapter = new RxxpAdapter(context, rxxpBean.getCommodityList());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
((MyViewHolder_1) viewHolder).mRecycler.setLayoutManager(linearLayoutManager);
((MyViewHolder_1) viewHolder).mRecycler.setAdapter(rxxpAdapter);
} else if (viewHolder instanceof MyViewHolder_1 &&
list.get(i) instanceof HomeListBean.ResultBean.MlssBean) {//魔力时尚
HomeListBean.ResultBean.MlssBean mlssBean = (HomeListBean.ResultBean.MlssBean) list.get(i);
((MyViewHolder_1) viewHolder).mTitle.setText(mlssBean.getName());
((MyViewHolder_1) viewHolder).mLayoutTitle.setBackgroundColor(Color.WHITE);
MlssAdapter mlssAdapter = new MlssAdapter(context, mlssBean.getCommodityList());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
((MyViewHolder_1) viewHolder).mRecycler.setLayoutManager(linearLayoutManager);
((MyViewHolder_1) viewHolder).mRecycler.setAdapter(mlssAdapter);
} else if (viewHolder instanceof MyViewHolder_1 &&
list.get(i) instanceof HomeListBean.ResultBean.PzshBean) {//品质生活
HomeListBean.ResultBean.PzshBean pzshBean = (HomeListBean.ResultBean.PzshBean) list.get(i);
((MyViewHolder_1) viewHolder).mTitle.setText(pzshBean.getName());
((MyViewHolder_1) viewHolder).mLayoutTitle.setBackgroundColor(Color.WHITE);
PzshAdapter pzshAdapter = new PzshAdapter(context, pzshBean.getCommodityList());
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
((MyViewHolder_1) viewHolder).mRecycler.setLayoutManager(gridLayoutManager);
((MyViewHolder_1) viewHolder).mRecycler.setAdapter(pzshAdapter);
}
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public int getItemViewType(int position) {
int type = 0;
if (list.get(position) instanceof BannerBean) {
type = 0;
} else if (list.get(position) instanceof HomeListBean.ResultBean.RxxpBean) {
type = 1;
} else if (list.get(position) instanceof HomeListBean.ResultBean.MlssBean) {
type = 2;
} else {
type = 3;
}
return type;
}
//传递数据
public void setList(List<BaseBean> list) {
this.list = list;
notifyDataSetChanged();
}
private class MyViewHolder_0 extends RecyclerView.ViewHolder {
XBanner mXBanner;
public MyViewHolder_0(@NonNull View itemView) {
super(itemView);
mXBanner = (XBanner) itemView.findViewById(R.id.xbanner);
}
}
private class MyViewHolder_1 extends RecyclerView.ViewHolder {
RelativeLayout mLayoutTitle;
TextView mTitle;
RecyclerView mRecycler;
public MyViewHolder_1(@NonNull View itemView) {
super(itemView);
mRecycler = (RecyclerView) itemView.findViewById(R.id.adapter_recycler);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mLayoutTitle = (RelativeLayout) itemView.findViewById(R.id.layout_title);
}
}
}
package com.bawei.mvp.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bawei.R;
import com.bawei.mvp.bean.HomeListBean;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:54
*/
public class MlssAdapter extends RecyclerView.Adapter<MlssAdapter.RxxpViewHolder> {
private Context context;
private List<HomeListBean.ResultBean.MlssBean.CommodityListBeanXX> commodityList = new ArrayList<>();
public MlssAdapter(Context context, List<HomeListBean.ResultBean.MlssBean.CommodityListBeanXX> commodityList) {
this.context = context;
this.commodityList = commodityList;
}
@NonNull
@Override
public RxxpViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.adapter_mlss, null);
RxxpViewHolder rxxpViewHolder = new RxxpViewHolder(view);
return rxxpViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RxxpViewHolder rxxpViewHolder, int i) {
rxxpViewHolder.mTitle.setText(commodityList.get(i).getCommodityName());
rxxpViewHolder.mDesc.setText(commodityList.get(i).getPrice() + "");
Glide.with(context).load(commodityList.get(i).getMasterPic()).into(rxxpViewHolder.mImage);
}
@Override
public int getItemCount() {
return commodityList.size();
}
public class RxxpViewHolder extends RecyclerView.ViewHolder {
private ImageView mImage;
private TextView mTitle, mDesc;
public RxxpViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (ImageView) itemView.findViewById(R.id.image_view);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
}
}
}
package com.bawei.mvp.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bawei.R;
import com.bawei.mvp.bean.HomeListBean;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:54
*/
public class PzshAdapter extends RecyclerView.Adapter<PzshAdapter.RxxpViewHolder> {
private Context context;
private List<HomeListBean.ResultBean.PzshBean.CommodityListBeanX> commodityList = new ArrayList<>();
public PzshAdapter(Context context, List<HomeListBean.ResultBean.PzshBean.CommodityListBeanX> commodityList) {
this.context = context;
this.commodityList = commodityList;
}
@NonNull
@Override
public RxxpViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.adapter_pzss, null);
RxxpViewHolder rxxpViewHolder = new RxxpViewHolder(view);
return rxxpViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RxxpViewHolder rxxpViewHolder, int i) {
rxxpViewHolder.mTitle.setText(commodityList.get(i).getCommodityName());
rxxpViewHolder.mDesc.setText(commodityList.get(i).getPrice() + "");
Glide.with(context).load(commodityList.get(i).getMasterPic()).into(rxxpViewHolder.mImage);
}
@Override
public int getItemCount() {
return commodityList.size();
}
public class RxxpViewHolder extends RecyclerView.ViewHolder {
private ImageView mImage;
private TextView mTitle, mDesc;
public RxxpViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (ImageView) itemView.findViewById(R.id.image_view);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
}
}
}
package com.bawei.mvp.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bawei.R;
import com.bawei.mvp.bean.HomeListBean;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:54
*/
public class RxxpAdapter extends RecyclerView.Adapter<RxxpAdapter.RxxpViewHolder> {
private Context context;
private List<HomeListBean.ResultBean.RxxpBean.CommodityListBean> commodityList = new ArrayList<>();
public RxxpAdapter(Context context, List<HomeListBean.ResultBean.RxxpBean.CommodityListBean> commodityList) {
this.context = context;
this.commodityList = commodityList;
}
@NonNull
@Override
public RxxpViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.adapter_rxxp, null);
RxxpViewHolder rxxpViewHolder = new RxxpViewHolder(view);
return rxxpViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RxxpViewHolder rxxpViewHolder, int i) {
rxxpViewHolder.mTitle.setText(commodityList.get(i).getCommodityName());
rxxpViewHolder.mDesc.setText(commodityList.get(i).getPrice() + "");
Glide.with(context).load(commodityList.get(i).getMasterPic()).into(rxxpViewHolder.mImage);
}
@Override
public int getItemCount() {
return commodityList.size();
}
public class RxxpViewHolder extends RecyclerView.ViewHolder {
private ImageView mImage;
private TextView mTitle, mDesc;
public RxxpViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (ImageView) itemView.findViewById(R.id.image_view);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
}
}
}
MainActivity
package com.bawei;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.bawei.mvp.adapter.MainAdapter;
import com.bawei.mvp.bean.BannerBean;
import com.bawei.mvp.bean.BaseBean;
import com.bawei.mvp.bean.HomeListBean;
import com.bawei.mvp.model.MainModelIml;
import com.bawei.mvp.presenter.MainPresenterIml;
import com.bawei.mvp.view.MainView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/1 18:54
*/
public class MainActivity extends AppCompatActivity implements MainView {
private List<BaseBean> baseBeanList = new ArrayList<>();
private RecyclerView recyclerview;
private MainAdapter mainAdapter;
private MainPresenterIml mainPresenterIml;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview=(RecyclerView)findViewById(R.id.recycler);
mainAdapter=new MainAdapter(this);
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setAdapter( mainAdapter);
mainPresenterIml=new MainPresenterIml(new MainModelIml(),this);
mainPresenterIml.doBaner(0,"http://172.17.8.100/small/commodity/v1/bannerShow");
mainPresenterIml.doData(1,"http://172.17.8.100/small/commodity/v1/commodityList");
}
@Override
public void sueccss(int type, String data) {
if (type==0){
BannerBean bean = new Gson().fromJson(data, BannerBean.class);
baseBeanList.add(bean);
}else {
HomeListBean homeListBean = new Gson().fromJson(data, HomeListBean.class);
HomeListBean.ResultBean.RxxpBean rxxpBean = homeListBean.getResult().getRxxp();
HomeListBean.ResultBean.MlssBean mlssBean = homeListBean.getResult().getMlss();
HomeListBean.ResultBean.PzshBean pzshBean = homeListBean.getResult().getPzsh();
baseBeanList.add(rxxpBean);
baseBeanList.add(mlssBean);
baseBeanList.add(pzshBean);
mainAdapter.setList(baseBeanList);
}
}
@Override
public void fail(int type, String error) {
mainPresenterIml.fail(type,error);
}
@Override
protected void onDestroy() {
super.onDestroy();
mainPresenterIml.destroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
adapter_banenr.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<com.stx.xhb.xbanner.XBanner
android:id="@+id/xbanner"
android:layout_width="match_parent"
android:layout_height="170dp" />
</RelativeLayout>
adapter_mIss.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="120dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image_view"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:lines="1"/>
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:textColor="#d43c3c" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
adapter_pzss.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:id="@+id/image_view"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:id="@+id/tv_title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_desc"
android:textColor="#d43c3c"
/>
</LinearLayout>
</RelativeLayout>
adapter_rxxp.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="80dp" />
<TextView
android:id="@+id/tv_title"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/image_view"
android:ellipsize="end"
android:lines="1"
android:text="111" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_title"
android:textColor="#d43c3c" />
</RelativeLayout>
</RelativeLayout>
adapter_rxxp_mIss_pzss.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<RelativeLayout
android:id="@+id/layout_title"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#d43c3c"
android:layout_centerInParent="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/adapter_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_title" />
</RelativeLayout>