retrofit+多条目

布局:

activity_main:




    

;


item_banner:



    
        
        
        

    

;





    

    
    


;


item_mlss:



    

    

        

        

    




;


item-pzsh:



    

    

    







item-rxxp:



    

    

    



工具类:

public class RetrofitUtil {
    // TODO: 2019/11/10  私有一个静态apiservice接口方法
    private static ApiService apiService;
    // TODO: 2019/11/10  暴露一个静态apiservice方法
    public static ApiService getApiService(){
        if (apiService == null){
            // TODO: 2019/11/10  同步
            synchronized (RetrofitUtil.class){
                if (apiService == null){
                    apiService = new RetrofitUtil().getRetrofit();
                }
            }
        }
        return apiService;
    }

    private ApiService getRetrofit(){
        apiService = initRetrofit(initOkHttp()).create(ApiService.class);
        return apiService;
    }

    private Retrofit initRetrofit(OkHttpClient client) {
        Retrofit retrofit = new Retrofit.Builder()
                // TODO: 2019/11/10  baseUrl必须以/结尾
                .baseUrl(Contans.Base_Url)
                // TODO: 2019/11/10  添加client
                .client(client)
                // TODO: 2019/11/10  添加call adapter
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                // TODO: 2019/11/10  添加gson解析器
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit;
    }

    private OkHttpClient initOkHttp() {
        // TODO: 2019/11/10  日志拦截器
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        // TODO: 2019/11/10  打印日志为body的级别
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .build();
        return client;
    }
}

契约类:

public interface Contract {
    // TODO: 2019/11/10  view层
    interface IView{
        void onBannerSuccess(BannerBean bannerBean);
        void onHomeGoodsSuccess(HomeGoodsBean homeGoodsBean);
    }
    // TODO: 2019/11/10  model层
    interface IModel{
        void getBannerModel(BannerCallBackModel bannerCallBackModel);

        void getModelHomeGoods(HomeGoodsCallBack homeGoodsCallBack);

        interface BannerCallBackModel{
            void onModelBannerSuccess(BannerBean bannerBean);
        }

        interface HomeGoodsCallBack{
            void onHomeGoodsSuccess(HomeGoodsBean homeGoodsBean);
        }


    }
    // TODO: 2019/11/10  presenter层
    interface IPresenter{
        void attachView(Contract.IView iView);
        void detachView();
        void setBannerPresenter(String path);
        void setHomeGoodsPresenter(String path);
    }
}



presenter层实现方法:
public class HomePresenter implements Contract.IPresenter {
    private HomeModel model;
    private WeakReference iViewWeakReference;

    @Override
    public void attachView(Contract.IView iView) {
        model = new HomeModel();
        iViewWeakReference = new WeakReference<>(iView);
    }
    public Contract.IView getView(){
        return iViewWeakReference.get();
    }

    @Override
    public void detachView() {
        if (iViewWeakReference!=null){
            iViewWeakReference.clear();
            iViewWeakReference=null;
        }
    }

    @Override
    public void setBannerPresenter(String path) {

        model.getBannerModel(new Contract.IModel.BannerCallBackModel() {
            @Override
            public void onModelBannerSuccess(BannerBean bannerBean) {
                getView().onBannerSuccess(bannerBean);
            }
        });

    }

    @Override
    public void setHomeGoodsPresenter(String path) {

        model.getModelHomeGoods(new Contract.IModel.HomeGoodsCallBack() {
            @Override
            public void onHomeGoodsSuccess(HomeGoodsBean homeGoodsBean) {
                getView().onHomeGoodsSuccess(homeGoodsBean);
            }
        });
    }
}



model层实现方法:
public class HomeModel implements Contract.IModel {

    @Override
    public void getBannerModel(final BannerCallBackModel bannerCallBackModel) {
        Observable bannerBeanObservable = RetrofitUtil.getApiService().getBanner();
        Observer bannerBeanObserver = new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BannerBean bannerBean) {
                bannerCallBackModel.onModelBannerSuccess(bannerBean);

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };
        bannerBeanObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(bannerBeanObserver);
    }

    @Override
    public void getModelHomeGoods(final HomeGoodsCallBack homeGoodsCallBack) {
        Observable homeGoodsBeanObservable = RetrofitUtil.getApiService().getHomeGoods();
        Observer homeGoodsBeanObserver = new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                // TODO: 2019/11/10  请求前
            }

            @Override
            public void onNext(HomeGoodsBean homeGoodsBean) {
                // TODO: 2019/11/10  请求结果
                homeGoodsCallBack.onHomeGoodsSuccess(homeGoodsBean);

            }

            @Override
            public void onError(Throwable e) {
                // TODO: 2019/11/10  请求异常

            }

            @Override
            public void onComplete() {
                // TODO: 2019/11/10  请求结束

            }
        };
        homeGoodsBeanObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(homeGoodsBeanObserver);

    }
}

适配器:

HomeAdapter:
public class HomeAdapter extends RecyclerView.Adapter {
    private Context context;
    private List bannerData = new ArrayList<>();
    private HomeGoodsBean.ResultBean homeBean;
    private BannerBean.ResultBean bannerBean;
    private final int type_banner = 1;
    private final int type_rxxp = 2;
    private final int type_mlss = 3;
    private final int type_pzsh = 4;
    private XBanner.XBannerAdapter xBannerAdapter;

    public HomeAdapter(Context context) {
        this.context = context;
    }

    public void setBannerData(List bannerData) {
        this.bannerData = bannerData;
    }

    public void setHomeBean(HomeGoodsBean.ResultBean homeBean) {
        this.homeBean = homeBean;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        if (viewType == type_banner ){
            view = LayoutInflater.from(context).inflate(R.layout.item_banner, parent, false);
            holder = new BannerViewHolder(view);
        }

        if (viewType==type_rxxp){
            view = LayoutInflater.from(context).inflate(R.layout.item_goods, parent, false);
            holder=new RxxpViewHolder(view);
        }

        if (viewType==type_mlss) {
            view = LayoutInflater.from(context).inflate(R.layout.item_goods, parent, false);
            holder=new MlssViewHolder(view);
        }

        if (viewType==type_pzsh) {
            view = LayoutInflater.from(context).inflate(R.layout.item_goods, parent, false);
            holder=new PzshViewHolder(view);
        }

        return holder;
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

        //根据position获取到对应位置的type类型
        int type = getItemViewType(position);

        //根据type强转为对应的viewholder
        if (type==type_banner) {

            if (xBannerAdapter==null) {

                BannerViewHolder bannerViewHolder = (BannerViewHolder) viewHolder;

                //轮播图
                bannerViewHolder.xBanner.setData(bannerData,null);

                xBannerAdapter = new XBanner.XBannerAdapter() {
                    @Override
                    public void loadBanner(XBanner banner, View view, int position) {
                        bannerBean = bannerData.get(position);
                        Glide.with(context).load(bannerBean.imageUrl).into((ImageView) view);
                    }
                };

                bannerViewHolder.xBanner.setmAdapter(xBannerAdapter);
                bannerViewHolder.xBanner.startAutoPlay();

            }


        }

        if (type==type_rxxp)  {
            RxxpViewHolder rxxpViewHolder = (RxxpViewHolder) viewHolder;
            rxxpViewHolder.tv_CategroyName.setText("热销新品");
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rxxpViewHolder.recycler_GoodsList.setLayoutManager(linearLayoutManager);
            RxxpAdapter rxxpAdapter = new RxxpAdapter(homeBean.rxxp.commodityList,context);
            rxxpViewHolder.recycler_GoodsList.setAdapter(rxxpAdapter);

        }

        if (type==type_mlss) {
            MlssViewHolder mlssViewHolder = (MlssViewHolder) viewHolder;
            mlssViewHolder.tv_CategroyName.setText("魔丽时尚");
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mlssViewHolder.recycler_GoodsList.setLayoutManager(linearLayoutManager);
            MlssAdapter mlssAdapter = new MlssAdapter(homeBean.mlss.commodityList,context);
            mlssViewHolder.recycler_GoodsList.setAdapter(mlssAdapter);

        }

        if (type==type_pzsh) {
            PzshViewHolder pzshViewHolder = (PzshViewHolder) viewHolder;
            pzshViewHolder.tv_CategroyName.setText("品质生活");
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,2);
            gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
            pzshViewHolder.recycler_GoodsList.setLayoutManager(gridLayoutManager);
            PzshAdapter pzshAdapter = new PzshAdapter(homeBean.pzsh.commodityList,context);
            pzshViewHolder.recycler_GoodsList.setAdapter(pzshAdapter);

        }


    }

    @Override
    public int getItemCount() {
        int total = 0;

        if (bannerData.size()>0){
            total++;
        }


        if (homeBean!=null){

            if (homeBean.rxxp.commodityList.size()>0) {
                total++;
            }

            if (homeBean.mlss.commodityList.size()>0) {
                total++;
            }

            if (homeBean.pzsh.commodityList.size()>0) {
                total++;
            }

        }
        return total;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return type_banner;
        }

        if (position==1){
            return type_rxxp;
        }

        if (position==2){
            return type_mlss;
        }

        if (position==3){
            return type_pzsh;
        }

        return super.getItemViewType(position);
    }

    private class BannerViewHolder extends RecyclerView.ViewHolder {
        public XBanner xBanner;

        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
            xBanner = itemView.findViewById(R.id.xBanner);
        }
    }

    private class RxxpViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_CategroyName;
        public RecyclerView recycler_GoodsList;

        public RxxpViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_CategroyName = itemView.findViewById(R.id.tv_CategroyName);
            recycler_GoodsList = itemView.findViewById(R.id.recycler_GoodsList);
        }
    }

    private class MlssViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_CategroyName;
        public RecyclerView recycler_GoodsList;

        public MlssViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_CategroyName = itemView.findViewById(R.id.tv_CategroyName);
            recycler_GoodsList = itemView.findViewById(R.id.recycler_GoodsList);
        }
    }

    private class PzshViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_CategroyName;
        public RecyclerView recycler_GoodsList;

        public PzshViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_CategroyName = itemView.findViewById(R.id.tv_CategroyName);
            recycler_GoodsList = itemView.findViewById(R.id.recycler_GoodsList);
        }
    }
}

魔丽时尚适配器:

public class MlssAdapter extends RecyclerView.Adapter {
    private List listBean = new ArrayList<>();
    private Context context;

    public MlssAdapter(List listBean, Context context) {
        this.listBean.addAll(listBean);
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mlss, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder holder1 = (ViewHolder) holder;
        holder1.tv_GoodsName.setText(listBean.get(position).commodityName);
        holder1.tv_CategroyPrice.setText(listBean.get(position).price+"");
        Glide.with(context).load(listBean.get(position).masterPic).into(holder1.iv_GoodsPic);

    }

    @Override
    public int getItemCount() {
        return listBean.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView iv_GoodsPic;
        private TextView tv_GoodsName;
        private TextView tv_CategroyPrice;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_GoodsPic = itemView.findViewById(R.id.iv_GoodsPic);
            tv_GoodsName = itemView.findViewById(R.id.tv_GoodsName);
            tv_CategroyPrice = itemView.findViewById(R.id.tv_CategroyPrice);
        }
    }
}
 

多条目展示:// TODO: 2019/11/10  多条目展示
    @GET("commodity/v1/commodityList")
    Observable getHomeGoods();

 

public class Contans {
    // TODO: 2019/11/10  定义一个总接口
    public static String Base_Url = "http://172.17.8.100/small/";

主方法:

package com.bawei.weektwo2;

import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.bawei.weektwo2.adapter.HomeAdapter;
import com.bawei.weektwo2.api.Contans;
import com.bawei.weektwo2.base.BaseActivity;
import com.bawei.weektwo2.bean.BannerBean;
import com.bawei.weektwo2.bean.HomeGoodsBean;
import com.bawei.weektwo2.contract.Contract;
import com.bawei.weektwo2.presenter.HomePresenter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements Contract.IView {

    @BindView(R.id.xRecyclerView_main)
    XRecyclerView xRecyclerMain;
    private HomePresenter presenter;
    private HomeAdapter homeAdapter;

    @Override
    protected void initData() {
        presenter = new HomePresenter();
        presenter.attachView(this);
        presenter.setBannerPresenter(Contans.Base_Url);


    }

    @SuppressLint("WrongConstant")
    @Override
    protected void initView() {
        ButterKnife.bind(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerMain.setLayoutManager(linearLayoutManager);

        xRecyclerMain.setPullRefreshEnabled(true);
        xRecyclerMain.setLoadingMoreEnabled(true);

        xRecyclerMain.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                presenter.setBannerPresenter(Contans.Base_Url);
                xRecyclerMain.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                presenter.setHomeGoodsPresenter(Contans.Base_Url);
                xRecyclerMain.loadMoreComplete();

            }
        });

    }

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void onBannerSuccess(BannerBean bannerBean) {
        homeAdapter = new HomeAdapter(this);
        homeAdapter.setBannerData(bannerBean.result);
        xRecyclerMain.setAdapter(homeAdapter);
        presenter.setHomeGoodsPresenter(Contans.Base_Url);


    }

    @Override
    public void onHomeGoodsSuccess(HomeGoodsBean homeGoodsBean) {
//        HomeGoodsBean.ResultBean result = homeGoodsBean.result;
//        homeAdapter.setHomeData((List) homeGoodsBean.result);
//        homeAdapter.notifyDataSetChanged();
        HomeGoodsBean.ResultBean resultBean = homeGoodsBean.result;
        homeAdapter.setHomeBean(resultBean);
        homeAdapter.notifyDataSetChanged();

    }
}

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(retrofit+多条目)