一:导入相关依赖
二:根据相关接口写入bean类
三:根据接口写接口请求类 Api 如:uid = 85
public interface Api {
//购物车
@GET("product/getCarts")
Observable<ShopBean> gouwuche(@Query("uid") String uid);
}
四:网络请求类
public class HttpUtils {
private static final String BASE_URL = "http://www.zhaoapi.cn/";
private Retrofit retrofit;
private static final class SINGLE_INSTANCE{
private static final HttpUtils _INSTANCE = new HttpUtils();
}
public static HttpUtils getInstance(){
return SINGLE_INSTANCE._INSTANCE;
}
private HttpUtils(){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(buildOkHttpClient())
.build();
}
private OkHttpClient buildOkHttpClient() {
return new OkHttpClient.Builder()
.readTimeout(5000,TimeUnit.MILLISECONDS)
.writeTimeout(5000,TimeUnit.MILLISECONDS)
.build();
}
public <T> T create(Class<T> clazz){
return retrofit.create(clazz);
}
}
五:view层
public interface ShopView {
void onSuccess(ShopBean shopBean);
void onFailed(Throwable t);
}
六:model层
public class ShopModel {
public Observable<ShopBean> shopData(String uid){
Api api = HttpUtils.getInstance().create(Api.class);
Observable<ShopBean> shop = api.gouwuche(uid);
return shop;
}
}
七:presenter层
public class ShopPresenter {
public ShopView shopView;
private final ShopModel shopModel;
public ShopPresenter(ShopView shopView) {
this.shopView = shopView;
shopModel = new ShopModel();
}
public void gouwuche(String uid){
shopModel.shopData(uid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ShopBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ShopBean shopBean) {
//Log.e("aaaaa",shopBean.getData().get(1).getSellerName());
shopView.onSuccess(shopBean);
}
@Override
public void onError(Throwable e) {
// Log.e("sss",e+"");
}
@Override
public void onComplete() {
}
});
}
public void attach(ShopView iv) {
this.shopView = iv;
}
//解决内存泄漏
public void dettach(){
shopView = null;
}
}
八:adapter层 ShopAdapter
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {
private Context context;
private List<ShopBean.DataBean> list;
private ShopZiAdapter shopZiAdapter;
public ShopAdapter(Context context, List<ShopBean.DataBean> list) {
this.context = context;
this.list = list;
}
public interface OnShoppingClickListener{
void OnShoppingClick(int postion,boolean ischeked);
}
private OnShoppingClickListener shoppingClickListener;
public void setOnShoppingClickListener(OnShoppingClickListener listener){
this.shoppingClickListener=listener;
}
private ShopZiAdapter.OnAddDecreaseProductListener productListener;
public void setOnAddDecreaseProductListener(ShopZiAdapter.OnAddDecreaseProductListener listener){
this.productListener=listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.shop_adapter, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
final ShopBean.DataBean dataBean = list.get(position);
holder.txtshoppername.setText(list.get(position).getSellerName());
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(context);
holder.rvproduct.setLayoutManager(layoutManager);
final ShopZiAdapter adapter = new ShopZiAdapter(context,dataBean.getList());
if(productListener!=null){
adapter.setOnAddDecreaseProductListener(productListener);
}
adapter.setOnProductClickListener(new ShopZiAdapter.OnProductClickListener() {
@Override
public void OnProductClickListener(int postion, boolean ischecked) {
if(!ischecked){
dataBean.setChecked(false);
shoppingClickListener.OnShoppingClick(postion,false);
}else{
boolean isAllProductSelected=true;
for (ShopBean.DataBean.ListBean listBean : dataBean.getList()) {
if(!listBean.isChecked()){
isAllProductSelected=false;
break;
}
}
dataBean.setChecked(isAllProductSelected);
shoppingClickListener.OnShoppingClick(postion,true);
}
notifyDataSetChanged();
productListener.onChange(0,0);
}
});
holder.rvproduct.setAdapter(adapter);
holder.cbshopper.setOnCheckedChangeListener(null);
holder.cbshopper.setChecked(dataBean.isChecked());
holder.