创建一个类里面放接口
public class GoodsApi {
public static final String XBanner_Api=“http://172.17.8.100/small/commodity/v1/bannerShow”;
public static final String Home_Api=“http://172.17.8.100/small/commodity/v1/commodityList”;
}
自定义一个接口里面存放成功失败方法
public interface OkHttpUtilsCallBack {
void onSuccess(String result);
void onFilure(String msg);
}
自定义一个接口里面存放M,V,P的接口
public interface GoodsContract {
//M层
interface IModle{
void BannerModle(HashMap<String,String> map, final OkHttpUtilsCallBack okHttpUtilsCallBack);
void ProductModle(HashMap<String,String> map, final OkHttpUtilsCallBack okHttpUtilsCallBack);
}
//v层
interface IView{
void bannerSuccess(String result);
void bnnerFilure(String msg);
void productSuccess(String result);
void productFilure(String msg);
}
//p层
abstract class IPresenter{
public abstract void bnnerPresenter(HashMap<String,String> map);
public abstract void productPresenter(HashMap<String,String> map);
}
}
然后写OkHttp
public class OkHttpUtils {
private final OkHttpClient okHttpClient;
private Handler handler=new Handler();
private static OkHttpUtils okHttpUtils;
private OkHttpUtils(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.readTimeout(5,TimeUnit.SECONDS)
.writeTimeout(5,TimeUnit.SECONDS)
.connectTimeout(5,TimeUnit.SECONDS)
.build();
}
public static OkHttpUtils getInstance(){
if (okHttpUtils==null){
synchronized (OkHttpUtils.class){
if (okHttpUtils==null){
okHttpUtils=new OkHttpUtils();
}
}
}
return okHttpUtils;
}
public void doGet(String url, final OkHttpUtilsCallBack okHttpUtilsCallBack){
final Request request=new Request.Builder()
.url(url)
.get()
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
okHttpUtilsCallBack.onFilure("网络异常");
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String result = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
okHttpUtilsCallBack.onSuccess(result);
}
});
}
});
}
}
接下来就是M层
public class GoodsModle implements GoodsContract.IModle {
@Override
public void BannerModle(HashMap<String, String> map, final OkHttpUtilsCallBack okHttpUtilsCallBack) {
OkHttpUtils.getInstance().doGet(GoodsApi.XBanner_Api, new OkHttpUtilsCallBack() {
@Override
public void onSuccess(String result) {
okHttpUtilsCallBack.onSuccess(result);
}
@Override
public void onFilure(String msg) {
okHttpUtilsCallBack.onSuccess(msg);
}
});
}
@Override
public void ProductModle(HashMap<String, String> map, final OkHttpUtilsCallBack okHttpUtilsCallBack) {
OkHttpUtils.getInstance().doGet(GoodsApi.Home_Api, new OkHttpUtilsCallBack() {
@Override
public void onSuccess(String result) {
okHttpUtilsCallBack.onSuccess(result);
}
@Override
public void onFilure(String msg) {
okHttpUtilsCallBack.onFilure(msg);
}
});
}
}
然后写P层
public class GoodsPresenter extends GoodsContract.IPresenter {
private GoodsModle goodsModle;
private GoodsContract.IView iView;
public GoodsPresenter(GoodsContract.IView iView){
this.iView=iView;
this.goodsModle=new GoodsModle();
}
@Override
public void bnnerPresenter(HashMap<String, String> map) {
goodsModle.BannerModle(map, new OkHttpUtilsCallBack() {
@Override
public void onSuccess(String result) {
if (iView!=null){
iView.bannerSuccess(result);
}
}
@Override
public void onFilure(String msg) {
if (iView!=null){
iView.bnnerFilure(msg);
}
}
});
}
@Override
public void productPresenter(HashMap<String, String> map) {
goodsModle.ProductModle(map, new OkHttpUtilsCallBack() {
@Override
public void onSuccess(String result) {
if (iView!=null){
iView.productSuccess(result);
}
}
@Override
public void onFilure(String msg) {
if (iView!=null){
iView.bnnerFilure(msg);
}
}
});
}
}
接下来写适配器因为这是三级列表就写了4个适配器
主的适配器
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<BannerBean.Data> resultBean;
private ProductBean productBean;
public MyAdapter(Context context){
this.context=context;
this.resultBean=new ArrayList<>();
}
public void setLiset(ProductBean productBean){
this.productBean=productBean;
notifyDataSetChanged();
}
private final int BANNER=0;
private List<String> list;
public void setBnner(List<BannerBean.Data> resultBean){
list=new ArrayList<>();
for (BannerBean.Data k:resultBean){
list.add(k.imageUrl);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//轮播图
if (getItemViewType(viewType)==BANNER){
View view = LayoutInflater.from(context).inflate(R.layout.activity_banner, parent, false);
return new BannerHolder(view);
}
//热销产品
else if (getItemViewType(viewType)==1){
View view = LayoutInflater.from(context).inflate(R.layout.activity_rxsp, parent, false);
return new RxspHoder(view);
}
//品质生活
else if (getItemViewType(viewType)==2){
View view = LayoutInflater.from(context).inflate(R.layout.activity_pzsh, parent, false);
return new PzshHoder(view);
}
//魔丽时尚
else {
View view = LayoutInflater.from(context).inflate(R.layout.activity_mlss, parent, false);
return new MlssHoder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position)==BANNER){
if (list!=null){
((BannerHolder) holder).flaBanner.setImagesUrl(list);
}
}
//热销商品
else if (getItemViewType(position)==1){
RespAdapter respAdapter = new RespAdapter(context, productBean.getResult().getRxxp().getCommodityList());
((RxspHoder)holder).rexXr.setLayoutManager(new GridLayoutManager(context,3));
((RxspHoder)holder).rexXr.setAdapter(respAdapter);
}
//品质生活
else if (getItemViewType(position)==2){
PzshAdapter pzshAdapter = new PzshAdapter(context, productBean.getResult().getRxxp().getCommodityList());
((PzshHoder)holder).pzahXr.setLayoutManager(new LinearLayoutManager(context));
((PzshHoder)holder).pzahXr.setAdapter(pzshAdapter);
}
//魔力时尚
else {
MLssAdapter mLssAdapter = new MLssAdapter(context, productBean.getResult().getRxxp().getCommodityList());
((MlssHoder)holder).mlssXr.setLayoutManager(new LinearLayoutManager(context));
((MlssHoder)holder).mlssXr.setAdapter(mLssAdapter);
}
}
@Override
public int getItemCount() {
return 4;
}
@Override
public int getItemViewType(int position) {
if (position==BANNER){
return BANNER;
}else if (position==1){
return 1;
}else if (position==2){
return 2;
}else {
return 3;
}
}
//轮播图
private class BannerHolder extends RecyclerView.ViewHolder {
private final FlyBanner flaBanner;
public BannerHolder(View view) {
super(view);
flaBanner = view.findViewById(R.id.flybnner);
}
}
//热销产品
private class RxspHoder extends RecyclerView.ViewHolder {
private final RecyclerView rexXr;
public RxspHoder(View view) {
super(view);
rexXr=view.findViewById(R.id.rx_xr);
}
}
//品质生活
private class PzshHoder extends RecyclerView.ViewHolder {
private final RecyclerView pzahXr;
public PzshHoder(View view) {
super(view);
pzahXr=view.findViewById(R.id.pz_xr);
}
}
//魔力时尚
private class MlssHoder extends RecyclerView.ViewHolder {
private final RecyclerView mlssXr;
public MlssHoder(View view) {
super(view);
mlssXr=view.findViewById(R.id.ml_xr);
}
}
}
轮播图的布局文件
<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<com.recker.flybanner.FlyBanner
android:id="@+id/flybnner"
android:layout_width=“0dp”
android:layout_height=“180dp”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
></com.recker.flybanner.FlyBanner>
</android.support.constraint.ConstraintLayout>
热销产品的布局文件
<android.support.constraint.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<android.support.v7.widget.RecyclerView
android:id="@+id/rx_xr"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
app:layout_constraintTop_toBottomOf="@+id/name"
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
品质生活的布局文件
<android.support.constraint.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<android.support.v7.widget.RecyclerView
android:id="@+id/pz_xr"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
app:layout_constraintTop_toBottomOf="@+id/name"
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
魔丽时尚的布局文件
<android.support.constraint.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<android.support.v7.widget.RecyclerView
android:id="@+id/ml_xr"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
app:layout_constraintTop_toBottomOf="@+id/name"
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
/热销商品的适配器
public class RespAdapter extends RecyclerView.Adapter<RespAdapter.RespHolder> {
private Context context;
private List<ProductBean.ResultBean.RxxpBean.CommodityListBean> list;
public RespAdapter(Context context, List<ProductBean.ResultBean.RxxpBean.CommodityListBean> list){
this.context=context;
this.list=list;
}
//加载布局
@NonNull
@Override
public RespHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.activity_resp_item, parent, false);
RespHolder respHolder = new RespHolder(view);
return respHolder;
}
//设置属性
@Override
public void onBindViewHolder(@NonNull RespHolder holder, int position) {
ProductBean.ResultBean.RxxpBean.CommodityListBean commodityListBean=list.get(position);
holder.name.setText(commodityListBean.getCommodityName());
holder.price.setText(commodityListBean.getPrice()+"");
Glide.with(context).load(commodityListBean.getMasterPic()).into(holder.img);
}
//长度
@Override
public int getItemCount() {
return list.size();
}
//viewholder
public class RespHolder extends RecyclerView.ViewHolder{
private final TextView name;
private final ImageView img;
private final TextView price;
public RespHolder(View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
img=itemView.findViewById(R.id.img);
price=itemView.findViewById(R.id.price);
}
}
}
对应的布局文件
<android.support.constraint.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:padding=“6dp”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<android.support.constraint.ConstraintLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
>
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="108dp"
android:src="@mipmap/ic_launcher"
android:paddingLeft="3dp"
android:paddingRight="3dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/img"
android:textSize="12sp"
android:text="名字"
android:layout_marginLeft="10dp"
app:layout_constraintLeft_toLeftOf="parent"
android:maxLines="1"
/>
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/name"
android:textSize="14sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintLeft_toLeftOf="parent"
android:textColor="#f00"
android:text="价格"
app:layout_constraintRight_toRightOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
/品质生活的适配器
public class PzshAdapter extends RecyclerView.Adapter<PzshAdapter.PzshHolder> {
private List<ProductBean.ResultBean.RxxpBean.CommodityListBean> list;
private Context context;
PzshAdapter(Context context, List<ProductBean.ResultBean.RxxpBean.CommodityListBean> list){
this.context=context;
this.list=list;
}
@NonNull
@Override
public PzshHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.activity_pzsh_item, parent, false);
PzshHolder pzshHolder = new PzshHolder(view);
return pzshHolder;
}
@Override
public void onBindViewHolder(@NonNull PzshHolder holder, int position) {
ProductBean.ResultBean.RxxpBean.CommodityListBean commodityListBean=list.get(position);
holder.nameVH.setText(commodityListBean.getCommodityName());
holder.priceVh.setText(commodityListBean.getMasterPic()+"");
Glide.with(context).load(commodityListBean.getMasterPic()).into(holder.img);
}
@Override
public int getItemCount() {
return list.size();
}
public class PzshHolder extends RecyclerView.ViewHolder{
private final ImageView img;
private final TextView nameVH,priceVh;
public PzshHolder(View itemView) {
super(itemView);
img=itemView.findViewById(R.id.img);
nameVH=itemView.findViewById(R.id.name);
priceVh=itemView.findViewById(R.id.price);
}
}
}
对应的布局文件
<android.support.constraint.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:padding=“9dp”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<android.support.constraint.ConstraintLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
>
<ImageView
android:id="@+id/img"
android:layout_width="131dp"
android:layout_height="102dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="3dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/img"
android:textSize="12sp"
android:text="名字"
android:layout_marginLeft="10dp"
app:layout_constraintLeft_toLeftOf="parent"
android:maxLines="1"
/>
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/name"
android:textSize="14sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintLeft_toLeftOf="parent"
android:textColor="#f00"
android:text="价格"
app:layout_constraintRight_toRightOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
//魔力时尚的适配器
public class MLssAdapter extends RecyclerView.Adapter<MLssAdapter.MlssHolder> {
private Context context;
private List<ProductBean.ResultBean.RxxpBean.CommodityListBean> list;
MLssAdapter(Context context, List<ProductBean.ResultBean.RxxpBean.CommodityListBean> list){
this.context=context;
this.list=list;
}
@NonNull
@Override
public MlssHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.activity_mlss_item, parent, false);
MlssHolder mlssHolder = new MlssHolder(view);
return mlssHolder;
}
@Override
public void onBindViewHolder(@NonNull MlssHolder holder, int position) {
ProductBean.ResultBean.RxxpBean.CommodityListBean commodityListBean=list.get(position);
holder.nameTV.setText(commodityListBean.getCommodityName());
holder.priceTV.setText(commodityListBean.getMasterPic());
Glide.with(context).load(commodityListBean.getMasterPic()).into(holder.imgTV);
}
@Override
public int getItemCount() {
return list.size();
}
public class MlssHolder extends RecyclerView.ViewHolder{
private final TextView nameTV,priceTV;
private final ImageView imgTV;
public MlssHolder(View itemView) {
super(itemView);
nameTV=itemView.findViewById(R.id.name);
priceTV=itemView.findViewById(R.id.price);
imgTV=itemView.findViewById(R.id.img);
}
}
}
对应的布局文件
<android.support.constraint.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:paddingBottom=“9dp”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<android.support.constraint.ConstraintLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
展示的Activity
public class HomeFragment extends Fragment implements GoodsContract.IView {
private RecyclerView recy;
private MyAdapter myAdapter;
private GoodsPresenter goodsPresenter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//获取资源ID
recy=view.findViewById(R.id.xr);
recy.setLayoutManager(new LinearLayoutManager(getActivity()));
//添加适配器
myAdapter = new MyAdapter(getActivity());
//实例化P层
goodsPresenter = new GoodsPresenter(this);
goodsPresenter.bnnerPresenter(new HashMap<String, String>());
goodsPresenter.productPresenter(new HashMap<String, String>());
}
//banner轮播图的方法
@Override
public void bannerSuccess(String result) {
BannerBean bannerBean = new Gson().fromJson(result, BannerBean.class);
myAdapter.setBnner(bannerBean.result);
recy.setAdapter(myAdapter);
}
@Override
public void bnnerFilure(String msg) {
}
//展示列表的成功失败方法
@Override
public void productSuccess(String result) {
ProductBean productBean = new Gson().fromJson(result, ProductBean.class);
myAdapter.setLiset(productBean);
recy.setAdapter(myAdapter);
}
@Override
public void productFilure(String msg) {
}
}
对应的布局
<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<android.support.v7.widget.RecyclerView
android:id="@+id/xr"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>