电商小项目

首页fragment.xml






首页json解析 package com.example.gwc.entity;

import java.util.List;

public class Home {
public String code;
public String msg;
public DataBase data;

public static class DataBase{

    public Listbanner;
    public Listfenlei;
    public Miaosha miaosha;
    public Tuijian tuijian;

    public class BannerBase{
        public String icon;
    }
    public class FenleiBase{
        public String icon;
        public String name;
    }
    public class Miaosha{
        public Listlist;
        public String name;
        public class ListBase{
            public String images;
            public double price;
        }
    }
    public class Tuijian{
        public Listlist;
        public String name;
        public class ListBase{
            public String title;
            public double price;
            public String images;
        }
    }

}

}
首页adapter
package com.example.gwc.adapter;

import android.content.Context;
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.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.gwc.R;
import com.example.gwc.entity.Home;
import com.recker.flybanner.FlyBanner;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class HomeAdapter extends RecyclerView.Adapter{
private Context context;
private Home.DataBase dataBase;
private static final int BANNER=0;
public static final int FENLEI=1;
public static final int MIAOSHA=2;
public static final int TUIJIAN=3;

public HomeAdapter(Context context) {
    this.context = context;
    this.dataBase = new Home.DataBase();
}

public void setDataBase(Home.DataBase dataBase) {
    if (dataBase!=null){
        this.dataBase = dataBase;
    }
    notifyDataSetChanged();
}

@Override
public int getItemViewType(int position) {
    if (position==0){
        return BANNER;
    }else if (position==1){
        return FENLEI;
    }else if (position==2){
        return MIAOSHA;
    }else{
        return TUIJIAN;
    }
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    if (getItemViewType(i)==BANNER){
        View view=LayoutInflater.from(context).inflate(R.layout.home_banner,viewGroup,false);
        ViewHolder1 viewHolder1=new ViewHolder1(view);
        return viewHolder1;
    }else if (getItemViewType(i)==FENLEI){
        View view=LayoutInflater.from(context).inflate(R.layout.home_gongnzhu,viewGroup,false);
        ViewHolder2 viewHolder2=new ViewHolder2(view);
        return viewHolder2;
    }else if (getItemViewType(i)==MIAOSHA){
        View view=LayoutInflater.from(context).inflate(R.layout.home_haohuozhu,viewGroup,false);
        ViewHolder3 viewHolder3=new ViewHolder3(view);
        return viewHolder3;
    }else {
        View view=LayoutInflater.from(context).inflate(R.layout.home_tuijianzhu,viewGroup,false);
        ViewHolder4 viewHolder4=new ViewHolder4(view);
        return viewHolder4;
    }
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    if (dataBase.banner!=null&&dataBase.fenlei!=null&&dataBase.miaosha!=null&&dataBase.tuijian!=null){
        if (viewHolder instanceof ViewHolder1){
            ArrayList list = new ArrayList<>();
            for (int j=0;j

}
Basefragment
package com.example.zhoermoni.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(getLayoutId(),container,false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initView(view);
}

protected abstract int getLayoutId();

protected abstract void initView(View view);

protected abstract void initData();

}

分类(尽量调用一个新的接口)xml
分类RequestCallback
package com.example.zhoermoni.net;

import com.example.zhoermoni.entily.Product;
import com.example.zhoermoni.entily.User;

public interface RequestCallback {
void onFailUre(String msg);
void onSuccessUser(User user);
void onSuccessProduct(Product product);
void onSuccessMsg(String msg);
}
分类OkhttpCallback一样
分类契约类
package com.example.zhoermoni.contract;

import com.example.zhoermoni.entily.Product;
import com.example.zhoermoni.entily.User;
import com.example.zhoermoni.net.RequestCallback;

import java.util.HashMap;

public interface IShowCallback {
public abstract class IShowPresenter{
public abstract void user();
public abstract void product(HashMapparams);
}
interface IShowModel{
void user(RequestCallback callback);
void product(HashMapparams,RequestCallback callback);
}
interface IShowView{
void onFailUre(String msg);
void onSuccessUser(User user);
void onSuccessProduct(Product product);
void onSuccessMsg(String msg);
}
}
分类在model层解析
package com.example.zhoermoni.model;

import android.os.Handler;
import android.text.TextUtils;

import com.example.zhoermoni.api.UserApi;
import com.example.zhoermoni.contract.IShowCallback;
import com.example.zhoermoni.entily.Product;
import com.example.zhoermoni.entily.User;
import com.example.zhoermoni.net.OkhttpCallback;
import com.example.zhoermoni.net.RequestCallback;
import com.example.zhoermoni.utiles.OkHttpUtiley;
import com.google.gson.Gson;

import java.util.HashMap;

public class UserModel implements IShowCallback.IShowModel {
private Handler handler=new Handler();
@Override
public void user(final RequestCallback callback) {
OkHttpUtiley.getmInstance().doPost(UserApi.PRODUCT_LEFT, null, new OkhttpCallback() {
@Override
public void onFailUre(String msg) {
if (callback!=null){
handler.post(new Runnable() {
@Override
public void run() {
callback.onFailUre(“网络有误,请稍后再试”);
}
});
}
}

        @Override
        public void onSuccess(String result) {
            final User user = new Gson().fromJson(result, User.class);
            if (!TextUtils.isEmpty(result)){
                if (callback!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onSuccessUser(user);
                        }
                    });
                }
            }
        }
    });
}

@Override
public void product(HashMap params, final RequestCallback callback) {
    OkHttpUtiley.getmInstance().doPost(UserApi.PRODUCE_RIGHT, params, new OkhttpCallback() {
        @Override
        public void onFailUre(String msg) {
            if (callback!=null){
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onFailUre("网络异常");
                    }
                });
            }
        }

        @Override
        public void onSuccess(String result) {
            final Product product = new Gson().fromJson(result, Product.class);
            if (!TextUtils.isEmpty(result)){
                if (callback!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onSuccessProduct(product);
                        }
                    });
                }
            }
        }
    });
}

}
分类json解析
侧面
package com.example.zhoermoni.entily;

import java.util.List;

public class User {
private String msg;
private String code;
private List data;

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public List getData() {
    return data;
}

public void setData(List data) {
    this.data = data;
}

public static class DataBean {

    private int cid;
    private String name;

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

}
分类主页面
package com.example.zhoermoni.entily;

import java.util.List;

public class Product {
private String msg;
private String code;
private List data;

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public List getData() {
    return data;
}

public void setData(List data) {
    this.data = data;
}

public class DataBean {
    private String name;
    private String pcid;
    private List list;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPcid() {
        return pcid;
    }

    public void setPcid(String pcid) {
        this.pcid = pcid;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public class ListBean {

        private String icon;
        private String name;

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

}
分类适配器
侧面
package com.example.dierzzk190107jjt.adapter;

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.TextView;

import com.example.dierzzk190107jjt.R;
import com.example.dierzzk190107jjt.entity.User;

import java.util.List;

public class UserAdapter extends RecyclerView.Adapter {
private Context context;
private Listlist;

public UserAdapter(Context context, List list) {
    this.context = context;
    this.list = list;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=LayoutInflater.from(context).inflate(R.layout.cemian,viewGroup,false);
    ViewHolder viewHolder=new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    final User.Data data = list.get(i);
    viewHolder.title.setText(data.getName());
    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (itemClickListener!=null){
                itemClickListener.click(data.getCid()+"");
            }
        }
    });
}


@Override
public int getItemCount() {
    return list==null?0:list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView title;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
            title=itemView.findViewById(R.id.title);
    }
}
private ItemClickListener itemClickListener;
public void setItemClickListenert(ItemClickListener itemClickListenert){
    this.itemClickListener=itemClickListenert;
}
public interface ItemClickListener{
    void click(String cid);
}

}
分类主页面主适配器
package com.example.dierzzk190107jjt.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.dierzzk190107jjt.R;
import com.example.dierzzk190107jjt.entity.Prodocf;

import java.util.List;

public class ProdocfAdapter extends RecyclerView.Adapter{
private Context context;
private List list;

public ProdocfAdapter(Context context, List list) {
    this.context = context;
    this.list = list;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=LayoutInflater.from(context).inflate(R.layout.zhu,viewGroup,false);
    ViewHolder viewHolder=new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    Prodocf.Data data = list.get(i);
    viewHolder.title.setText(data.getName());
    List list = data.getList();
    viewHolder.rec.setLayoutManager(new GridLayoutManager(context,3));
    viewHolder.rec.setAdapter(new ZuiAdapter(context, list));
}

@Override
public int getItemCount() {
    return list==null?0:list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView title;
    private RecyclerView rec;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        title=itemView.findViewById(R.id.title);
        rec=itemView.findViewById(R.id.rec);
    }
}

}
子适配器
package com.example.dierzzk190107jjt.adapter;

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.example.dierzzk190107jjt.R;
import com.example.dierzzk190107jjt.entity.Prodocf;

import java.util.List;

public class ZuiAdapter extends RecyclerView.Adapter {
private Context context;
private Listlist;
public ZuiAdapter(Context context, List list) {
this.context=context;
this.list=list;
}

@NonNull
@Override
public VIewHolder1 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=LayoutInflater.from(context).inflate(R.layout.zuiho,viewGroup,false);
    VIewHolder1 vIewHolder1=new VIewHolder1(view);
    return vIewHolder1;
}

@Override
public void onBindViewHolder(@NonNull VIewHolder1 vIewHolder, int i) {
    Prodocf.Data.ListBean listBean = list.get(i);
    Glide.with(context).load(listBean.getIcon()).into(vIewHolder.img);
    vIewHolder.title.setText(listBean.getName());
}

@Override
public int getItemCount() {
    return list.size();
}
public class VIewHolder1 extends RecyclerView.ViewHolder{
    private ImageView img;
    private TextView title;
    public VIewHolder1(@NonNull View itemView) {
        super(itemView);
        title=itemView.findViewById(R.id.title);
        img=itemView.findViewById(R.id.img);
    }
}

}
分页Activity
package com.example.dierzzk190107jjt.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.dierzzk190107jjt.R;
import com.example.dierzzk190107jjt.adapter.ProdocfAdapter;
import com.example.dierzzk190107jjt.adapter.UserAdapter;
import com.example.dierzzk190107jjt.contract.IShowCallback;
import com.example.dierzzk190107jjt.entity.Prodocf;
import com.example.dierzzk190107jjt.entity.User;
import com.example.dierzzk190107jjt.net.OkHttpUtily;
import com.example.dierzzk190107jjt.presenter.UserPresenter;

import java.util.HashMap;
import java.util.List;

public class FragmentTwo extends BaseFragment implements IShowCallback.IShowView {

private RecyclerView cemian;
private UserAdapter userAdapter;
private UserPresenter presenter;
private String cid="1";
private RecyclerView zhu;
private ProdocfAdapter prodocfAdapter;

@Override
protected int getLayout() {
    return R.layout.fragmenttwo;
}
@Override
protected void initView(View view) {
    cemian = view.findViewById(R.id.cemian);
    zhu = view.findViewById(R.id.zhu);
    presenter = new UserPresenter(this);
    presenter.user();
    HashMap params=new HashMap<>();

    params.put("cid",cid);
    presenter.prodocf(params);
    cemian.setLayoutManager(new LinearLayoutManager(getActivity()));
    zhu.setLayoutManager(new LinearLayoutManager(getActivity()));

}

@Override
protected void initData() {

}


@Override
public void onFailUer(String msg) {

}

@Override
public void onSuccessUser(User user) {
    List data = user.getData();
    userAdapter=new UserAdapter(getActivity(),data);
    cemian.setAdapter(userAdapter);
    userAdapter.setItemClickListenert(new UserAdapter.ItemClickListener() {
        @Override
        public void click(String cid) {
            HashMap params=new HashMap<>();
            params.put("cid",cid);
            presenter.prodocf(params);
        }
    });
}

@Override
public void onSuccessProdocf(Prodocf prodocf) {
    List data1 = prodocf.getData();
    prodocfAdapter = new ProdocfAdapter(getActivity(), data1);
    zhu.setAdapter(prodocfAdapter);
}

@Override
public void onSuccessMsg(String msg) {

}

@Override
public void onDestroy() {
    super.onDestroy();
    OkHttpUtily.getmInstaser().okhttpcsas();
    presenter.dester();
}

}
购物车搜索xml






android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:id="@+id/xr">

搜索的OKHTTP
package com.example.gwc.net;

import android.os.Handler;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpUtiles {
private Handler handler=new Handler();
private OkHttpClient okHttpClient;
private static OkHttpUtiles mIncvcnmxz;
public OkHttpUtiles(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient=new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.readTimeout(5,TimeUnit.SECONDS)
.writeTimeout(5,TimeUnit.SECONDS)
.connectTimeout(5,TimeUnit.SECONDS)
.build();
}
public void doPost(String auiUre, HashMapparams, final OkHttpCallback okHttpCallback){
FormBody.Builder formBody = new FormBody.Builder();
if (params!=null){
for (Map.Entryp:params.entrySet()){
formBody.add(p.getKey(),p.getValue());
}
}
final Request request = new Request.Builder().url(auiUre).post(formBody.build()).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (okHttpCallback!=null){
handler.post(new Runnable() {
@Override
public void run() {
okHttpCallback.onFaliUre(“网络异常”);
}
});
}
}

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final String result = response.body().string();
            int code = response.code();
            if (200==code){
                if (okHttpCallback!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            okHttpCallback.onSuccess(result);
                        }
                    });
                }
            }
        }
    });
}
public static OkHttpUtiles getmIncvcnmxz(){
    if (mIncvcnmxz==null){
        synchronized (OkHttpUtiles.class){
            if (mIncvcnmxz==null){
                mIncvcnmxz=new OkHttpUtiles();
            }
        }
    }
    return mIncvcnmxz;
}
public void gczvbxczx(){
    if (okHttpClient!=null){
        okHttpClient.dispatcher().cancelAll();
    }
}

}
搜索的Model
package com.example.gwc.model;

import android.text.TextUtils;

import com.example.gwc.api.UserApi;
import com.example.gwc.contract.IShowContract;
import com.example.gwc.net.OkHttpCallback;
import com.example.gwc.net.OkHttpUtiles;
import com.example.gwc.net.RequestCallback;

import java.util.HashMap;

public class IShowModel implements IShowContract.IShowModel {
@Override
public void ShowModel(HashMap params, final RequestCallback callback) {
OkHttpUtiles.getmIncvcnmxz().doPost(UserApi.USER_SHOW, params, new OkHttpCallback() {
@Override
public void onFaliUre(String msg) {
if(callback!=null){
callback.onFaliUre(msg);
}
}

        @Override
        public void onSuccess(String result) {
           if (!TextUtils.isEmpty(result)){
               if (callback!=null){
                    callback.onSuccess(result);
               }
           }
        }
    });
}

@Override
public void SosuoModel(HashMap params, final RequestCallback callback) {
    OkHttpUtiles.getmIncvcnmxz().doPost(UserApi.USER_USER, params, new OkHttpCallback() {
        @Override
        public void onFaliUre(String msg) {
            if(callback!=null){
                callback.onFaliUre(msg);
            }
        }

        @Override
        public void onSuccess(String result) {
            if (!TextUtils.isEmpty(result)){
                if (callback!=null){
                    callback.onSuccess(result);
                }
            }
        }
    });
}

@Override
public void AddModel(HashMap params, final RequestCallback callback) {
    OkHttpUtiles.getmIncvcnmxz().doPost(UserApi.USER_ADDSHOP, params, new OkHttpCallback() {
        @Override
        public void onFaliUre(String msg) {
            if(callback!=null){
                callback.onFaliUre(msg);
            }
        }

        @Override
        public void onSuccess(String result) {
            if (!TextUtils.isEmpty(result)){
                if (callback!=null){
                    callback.onSuccess(result);
                }
            }
        }
    });
}

}
搜索preserve
package com.example.gwc.preserve;

import com.example.gwc.contract.IShowContract;
import com.example.gwc.model.IShowModel;
import com.example.gwc.net.RequestCallback;

import java.util.HashMap;

public class IShowPreserve extends IShowContract.IShowPreserve {
private IShowModel iShowModel;
private IShowContract.IShowView iShowView;

public IShowPreserve( IShowContract.IShowView iShowView) {
    this.iShowModel = new IShowModel();
    this.iShowView = iShowView;
}

@Override
public void ShowPreserve(HashMap params) {
    if (iShowModel!=null){
        iShowModel.ShowModel(params, new RequestCallback() {
            @Override
            public void onFaliUre(String msg) {
                if (iShowView!=null){
                    iShowView.onFaliUre(msg);
                }
            }

            @Override
            public void onSuccess(String result) {
                if (iShowView!=null){
                    iShowView.onSuccess(result);
                }
            }
        });
    }
}

@Override
public void SosuoPreserve(HashMap params) {
    if (iShowModel!=null){
        iShowModel.SosuoModel(params, new RequestCallback() {
            @Override
            public void onFaliUre(String msg) {
                if (iShowView!=null){
                    iShowView.onFaliUre(msg);
                }
            }

            @Override
            public void onSuccess(String result) {
                if (iShowView!=null){
                    iShowView.onSuccess(result);
                }
            }
        });
    }
}

@Override
public void AddPreserve(HashMap params) {
    if (iShowModel!=null){
        iShowModel.AddModel(params, new RequestCallback() {
            @Override
            public void onFaliUre(String msg) {
                if (iShowView!=null){
                    iShowView.onFaliUre(msg);
                }
            }

            @Override
            public void onSuccess(String result) {
                if (iShowView!=null){
                    iShowView.onSuccess(result);
                }
            }
        });
    }
}
private void okhttcallback(){
    if (iShowView!=null){
        iShowView=null;
    }
}

}
购物车展示json
package com.example.gwc.entity;

import java.util.List;

public class Sosuo {
public String code;
public String msg;
public List data;
public class DataBase{
public String sellerName;
public String sellerid;
public boolean ischeckBox;
public List list;
public class Product{
public String title;
public String images;
public double price;
public boolean isproductcheckBox;
public String pid;
public int pos;
public int productNum;
}
}
}
搜索的适配器
package com.example.gwc.adapter;

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.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.gwc.R;
import com.example.gwc.entity.Show;

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

public class ShowAdapter extends RecyclerView.Adapter{
private Context context;
private List show;

public ShowAdapter(Context context) {
    this.context = context;
    this.show = new ArrayList<>();
}

public void setShow(List show) {
    if (show!=null){
        this.show = show;
    }
    notifyDataSetChanged();
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=LayoutInflater.from(context).inflate(R.layout.sosuo_shuju,viewGroup,false);
    ViewHolder viewHolder=new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
    Show.DataBean dataBean = show.get(i);
    String[] split = dataBean.getImages().split("!");
    Glide.with(context).load(split[0]).into(viewHolder.img);
    viewHolder.title.setText(dataBean.getTitle());
    viewHolder.price.setText("¥:"+dataBean.getPrice());
    viewHolder.gwc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getpidllback!=null){
                getpidllback.getpid(show.get(i).getPid());
            }
        }
    });
}

@Override
public int getItemCount() {
    return show==null?0:show.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    private ImageView img;
    private TextView title,price;
    private Button gwc;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        this.img=itemView.findViewById(R.id.img);
        this.title=itemView.findViewById(R.id.title);
        this.price=itemView.findViewById(R.id.price);
        this.gwc=itemView.findViewById(R.id.gwc);
    }
}
private getpidCllback getpidllback;

public void setGetpidllback(getpidCllback getpidllback) {
    this.getpidllback = getpidllback;
}

public interface getpidCllback{
    void getpid(int pid);
}

}
搜索的fragment
package com.example.gwc.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.gwc.R;
import com.example.gwc.adapter.ShowAdapter;
import com.example.gwc.contract.IShowContract;
import com.example.gwc.entity.AddShop;
import com.example.gwc.entity.Show;
import com.example.gwc.preserve.IShowPreserve;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;

public class FragmentSosuo extends BaseFragment implements IShowContract.IShowView {

private EditText name;
private Button sos;
private IShowPreserve iShowPreserve;
private ShowAdapter showAdapter;
private int page=1;
private XRecyclerView xr;

@Override
protected int getfragment() {
    return R.layout.fragmentsosuo;
}

@Override
protected void initView(View view) {
    name = view.findViewById(R.id.name);
    sos = view.findViewById(R.id.sos);
    xr = view.findViewById(R.id.xr);
    xr.setLayoutManager(new LinearLayoutManager(getActivity()));
    iShowPreserve = new IShowPreserve(this);
    showAdapter = new ShowAdapter(getActivity());
    sos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String title = name.getText().toString();
            HashMapparams=new HashMap<>();
            params.put("keywords",title);
            params.put("page",page+"");
            iShowPreserve.ShowPreserve(params);
        }
    });
    showAdapter.setGetpidllback(new ShowAdapter.getpidCllback() {
        @Override
        public void getpid(int pid) {
            HashMapparams=new HashMap<>();
            params.put("uid","601");
            params.put("pid",pid+"");
            iShowPreserve.AddPreserve(params);
        }
    });
    xr.setAdapter(showAdapter);
}

@Override
protected void initData() {

}

@Override
public void onFaliUre(String msg) {
    Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
}

@Override
public void onSuccess(String result) {
    Show show = new Gson().fromJson(result, Show.class);
    Toast.makeText(getActivity(),show.getMsg(),Toast.LENGTH_SHORT).show();
    showAdapter.setShow(show.getData());
}

@Override
public void onSuccessAddshow(String result) {
    AddShop addShop = new Gson().fromJson(result, AddShop.class);
    Toast.makeText(getActivity(),addShop.getMsg(),Toast.LENGTH_SHORT).show();
}

}
购物车第一级适配器
package com.example.gwc.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.gwc.R;
import com.example.gwc.entity.Sosuo;
import com.example.gwc.fragmentcallback.BufenCallback;
import com.example.gwc.fragmentcallback.CartUlCallback;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

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

public class GwcAdapter_Yi extends RecyclerView.Adapterimplements BufenCallback {
private Context context;
private List show;
private GwcAdapter_Er gwcAdapter_er;
private CartUlCallback cartUlCallback;

public GwcAdapter_Yi(Context context,CartUlCallback cartUlCallback) {
    this.context = context;
    this.show = new ArrayList<>();
    this.cartUlCallback=cartUlCallback;
}

public void setShow(List show) {
    if (show!=null){
        this.show = show;
    }
    notifyDataSetChanged();
}
public void addShow(List show) {
    if (show!=null){
       show.addAll(show);
    }
    notifyDataSetChanged();

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=LayoutInflater.from(context).inflate(R.layout.gwc_zhuyi,viewGroup,false);
    ViewHolder viewHolder=new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
    double totprice=0;
    for (Sosuo.DataBase.Product product : show.get(i).list) {
        totprice+=product.price*product.productNum;
    }
    for (Sosuo.DataBase.Product product : show.get(i).list) {
        product.pos=i;
    }
    viewHolder.xiao.setText("¥:"+totprice);
    Sosuo.DataBase dataBase = show.get(i);
    viewHolder.name.setText(dataBase.sellerName);
    viewHolder.checkBox.setChecked(show.get(i).ischeckBox);
    if (dataBase.ischeckBox){
        viewHolder.checkBox.setChecked(true);
    }else {
        viewHolder.checkBox.setChecked(false);
    }
    viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            show.get(i).ischeckBox=viewHolder.checkBox.isChecked();
            for (Sosuo.DataBase.Product product : show.get(i).list) {
                product.isproductcheckBox=show.get(i).ischeckBox;
            }
            notifyDataSetChanged();
            if (cartUlCallback!=null){
                cartUlCallback.notifyCall();
            }
        }
    });
    viewHolder.xr.setLayoutManager(new LinearLayoutManager(context));
    gwcAdapter_er = new GwcAdapter_Er(context, show.get(i).list);
    gwcAdapter_er.setBufenCallback(this);
    viewHolder.xr.setAdapter(gwcAdapter_er);
}

@Override
public int getItemCount() {
    return show.size();
}

@Override
public void notifyCartItem(boolean isChecked, int postion) {
    show.get(postion).ischeckBox=isChecked;
    notifyDataSetChanged();
    if (cartUlCallback!=null){
        cartUlCallback.notifyCall();
    }
}

@Override
public void notifyNum() {
    if (cartUlCallback!=null){
        cartUlCallback.notifyCall();
    }
}

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView name,xiao;
    private CheckBox checkBox;
    private XRecyclerView xr;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        this.checkBox=itemView.findViewById(R.id.checkBox);
        this.name=itemView.findViewById(R.id.name);
        this.xr=itemView.findViewById(R.id.xr);
        this.xiao=itemView.findViewById(R.id.xiao);
    }
}

}

购物车第二级适配器
package com.example.gwc.adapter;

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.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.gwc.R;
import com.example.gwc.entity.Sosuo;
import com.example.gwc.fragmentcallback.BufenCallback;
import com.example.gwc.view.MyView;

import java.util.List;

public class GwcAdapter_Er extends RecyclerView.Adapter{
private Context context;
private List list;
private BufenCallback bufenCallback;

public void setBufenCallback(BufenCallback bufenCallback) {
    this.bufenCallback = bufenCallback;
}

public GwcAdapter_Er(Context context, List list) {
    this.context = context;
    this.list = list;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=LayoutInflater.from(context).inflate(R.layout.gwc_er,viewGroup,false);
    ViewHolder viewHolder=new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
    final Sosuo.DataBase.Product product = list.get(i);
    String[] split = product.images.split("!");
    Glide.with(context).load(split[0]).into(viewHolder.img);
    viewHolder.title.setText(product.title);
    viewHolder.myview.setNum(list.get(i).productNum);
    viewHolder.checkBox.setChecked(list.get(i).isproductcheckBox);
    viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            list.get(i).isproductcheckBox=viewHolder.checkBox.isChecked();
            for (Sosuo.DataBase.Product product1 : list) {
                if (!product1.isproductcheckBox){
                    if (bufenCallback!=null){
                        bufenCallback.notifyCartItem(false,product1.pos);
                    }
                    return;
                }
                if (bufenCallback!=null){
                    bufenCallback.notifyCartItem(true,product1.pos);
                }
            }
        }
    });
    viewHolder.price.setText("¥:"+product.price);
    viewHolder.myview.setGetnumcallback(new MyView.getNumcallback() {
        @Override
        public void getnum(int nums) {
            list.get(i).productNum=nums;
            if (bufenCallback!=null){
                bufenCallback.notifyNum();
            }
        }
    });
}

@Override
public int getItemCount() {
    return list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    private ImageView img;
    private CheckBox checkBox;
    private TextView title,price;
    private MyView myview;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        this.checkBox=itemView.findViewById(R.id.checkBox);
        this.img=itemView.findViewById(R.id.img);
        this.title=itemView.findViewById(R.id.title);
        this.price=itemView.findViewById(R.id.price);
        this.myview=itemView.findViewById(R.id.myviwe);
    }
}

}
购物车activity
package com.example.gwc.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gwc.R;
import com.example.gwc.adapter.GwcAdapter_Yi;
import com.example.gwc.contract.IShowContract;
import com.example.gwc.entity.Sosuo;
import com.example.gwc.fragmentcallback.CartUlCallback;
import com.example.gwc.preserve.IShowPreserve;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;

public class FragmentGwc extends BaseFragment implements IShowContract.IShowView,CartUlCallback {

private XRecyclerView xr;
private CheckBox checkBox;
private TextView zong;
private IShowPreserve iShowPreserve;
private GwcAdapter_Yi gwcAdapter_yi;
private List soso;

@Override
protected int getfragment() {
    return R.layout.fragmentgwc;
}

@Override
protected void initView(View view) {
    xr = view.findViewById(R.id.xr);
    xr.setLayoutManager(new LinearLayoutManager(getActivity()));
    xr.setPullRefreshEnabled(true);
    xr.setLoadingMoreEnabled(true);
    xr.setLoadingListener(new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {

            HashMapparams=new HashMap<>();
            params.put("uid","601");
            iShowPreserve.SosuoPreserve(params);
            xr.refreshComplete();
        }

        @Override
        public void onLoadMore() {

        }
    });
    checkBox = view.findViewById(R.id.checkBox);

    zong = view.findViewById(R.id.zong);
    iShowPreserve = new IShowPreserve(this);
    gwcAdapter_yi = new GwcAdapter_Yi(getActivity(),this);
    HashMapparams=new HashMap<>();
    params.put("uid","601");
    iShowPreserve.SosuoPreserve(params);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                for (Sosuo.DataBase dataBase : soso) {
                    dataBase.ischeckBox=true;
                    for (Sosuo.DataBase.Product product : dataBase.list) {
                        product.isproductcheckBox=true;
                    }
                }
            }else {
                for (Sosuo.DataBase dataBase : soso) {
                    dataBase.ischeckBox=false;
                    for (Sosuo.DataBase.Product product : dataBase.list) {
                        product.isproductcheckBox=false;
                    }
                }
            }
            gwcAdapter_yi.notifyDataSetChanged();
            getTotalPrice();
        }
    });
    xr.setAdapter(gwcAdapter_yi);
}

@Override
protected void initData() {

}

@Override
public void onFaliUre(String msg) {

}

@Override
public void onSuccess(String result) {
    Sosuo sosuo = new Gson().fromJson(result, Sosuo.class);
    soso=sosuo.data;
    for (Sosuo.DataBase dataBase : soso) {
        for (Sosuo.DataBase.Product product : dataBase.list) {
            product.productNum=1;
        }
    }
    Toast.makeText(getActivity(),sosuo.msg,Toast.LENGTH_SHORT).show();
    gwcAdapter_yi.setShow(sosuo.data);
}

@Override
public void onSuccessAddshow(String result) {

}
public void getTotalPrice(){
    double totalprice=0;
    for (Sosuo.DataBase dataBase : soso) {
        for (Sosuo.DataBase.Product product : dataBase.list) {
            if (product.isproductcheckBox){
                totalprice+=product.price*product.productNum;
            }
        }
    }
    zong.setText("¥:"+totalprice);
}

@Override
public void notifyCall() {
    getTotalPrice();
}

}
购物车接口
package com.example.gwc.fragmentcallback;

public interface BufenCallback {
选择状态
void notifyCartItem(boolean isChecked,int postion);
一级二级 联动
void notifyNum();
}
package com.example.gwc.fragmentcallback;

public interface CartUlCallback {
总价
void notifyCall();
}

你可能感兴趣的:(展示,框架)