一,分类,购物车yi

一,okHttpUtil

public class OkHttpUtil {

    //1.单例模式必须有私有构造器
    private static OkHttpUtil okHttpUtil;
    private final OkHttpClient okHttpClient;
    private Handler myHandler;

    //2.类的单例
    private OkHttpUtil(){

        //7.声明日志拦截器
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        myHandler=new Handler(Looper.getMainLooper());
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5000, TimeUnit.MILLISECONDS)//设置连接超时
                .readTimeout(5000,TimeUnit.MILLISECONDS)//设置读取超时
                .writeTimeout(5000,TimeUnit.MILLISECONDS)//设置写入超时
                .addInterceptor(httpLoggingInterceptor)//设置日志拦截器
                .build();
    }

    //3.有一个公共的方法
    public static OkHttpUtil getInstence(){
        if(okHttpUtil==null){
            synchronized (OkHttpUtil.class){
                if(okHttpUtil==null){
                    return  okHttpUtil=new OkHttpUtil();
                }
            }
        }
        return  okHttpUtil;
    }



    //5.设置DOGet 方法
    public void doGet(String url, final OkCallback okCallback){
        //联网请求
        Request request=new Request.Builder()
                .get()
                .url(url)
                .build();

        //用request对象去声明一个call 对象.call去执行
        Call call=okHttpClient.newCall(request);
          call.enqueue(new Callback() {
              @Override
              public void onFailure(Call call, IOException e) {
                  //失败
                  if(okCallback!=null){
                      okCallback.getError(e);
                  }
              }

              @Override
              public void onResponse(Call call, Response response) throws IOException {
                  //成功
                  if(response!=null&& response.isSuccessful()){
                       if(okCallback!=null){
                           final String json = response.body().string();

                           //切换线程
                           myHandler.post(new Runnable() {
                               @Override
                               public void run() {
                                   okCallback.getSuccess(json);
                               }
                           });
                       }
                  }
              }
          });
    }



    //6.设置DOpost
    public void doPost(String url, Map<String,String> map, final OkCallback okCallback){
        FormBody.Builder formBody = new FormBody.Builder();

        for(String key:map.keySet()){
            formBody.add(key,map.get(key));
        }

        //生成formBody
        FormBody FBody=formBody.build();

        Request request=new Request.Builder()
                .post(FBody)
                .url(url)
                .build();

        Call call=okHttpClient.newCall(request);

          call.enqueue(new Callback() {
              @Override
              public void onFailure(Call call, IOException e) {
                  //失败
                  if(okCallback!=null){
                      okCallback.getError(e);
                  }
              }

              @Override
              public void onResponse(Call call, Response response) throws IOException {
                //成功
                  if(response!=null && response.isSuccessful()){
                      if(okCallback!=null){
                          final String json = response.body().string();

                          //切换线程
                          myHandler.post(new Runnable() {
                              @Override
                              public void run() {
                                  okCallback.getSuccess(json);
                              }
                          });
                      }
                  }
              }
          });

    }




    //4.定义一个接口
    public interface OkCallback{
        void getSuccess(String json);
        void getError(Exception e);
    }
}

二,Base 抽基类   IView,BasePresenter,BaseActivity

(2.)

public abstract  class BasePresenter<V extends IView> {


    protected V view;

    public BasePresenter(V view) {
        this.view = view;
        initModel();
    }

    protected abstract void initModel();


    //内存泄漏
    void onDestory(){
        view=null;
    }
}

(3.)

public abstract class BaseActivity <P extends BasePresenter> extends AppCompatActivity {

    protected P presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(setView());
        presenter=presenterById();
        initview();
        initData();
        initListener();
    }

    protected abstract void initListener();

    protected abstract void initData();

    protected abstract void initview();

    protected abstract P presenterById();

    protected abstract int setView();



    //内存泄漏

    @Override
    protected void onDestroy() {
        presenter.onDestory();
        super.onDestroy();
    }
}


三,MVP

(1,HomeActivity),主页,Fragment 切换 

public class HomeActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private RadioGroup viewGroup;

    private List<Fragment> list=new ArrayList<>();
    private MyFragmentPageAdapter myFragmentPageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化视图
        viewPager = findViewById(R.id.viewpager);
        viewGroup = findViewById(R.id.radiogroup);



        list.add(new Fragment_Home());
        list.add(new Fragment_Type());
        list.add(new Fragment_ShopCart());
        list.add(new Fragment_MyCenter());


        //创建适配器
        myFragmentPageAdapter = new MyFragmentPageAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(myFragmentPageAdapter);

        viewGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.button1:
                        viewPager.setCurrentItem(0);


                        break;

                    case R.id.button2:
                        viewPager.setCurrentItem(1);

                        break;

                    case R.id.button3:
                        viewPager.setCurrentItem(2);

                        break;


                    case R.id.button4:
                        viewPager.setCurrentItem(3);

                        break;
                }
            }

        });


    }
}

(2,适配器)

public class MyFragmentPageAdapter extends FragmentPagerAdapter{

    private List<Fragment> list;


    public MyFragmentPageAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.list = list;
    }

    public MyFragmentPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

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

2.(1)分类 TypeModel

public class TypeModel  {

    public void typeModel(String url, final MTypeCallback mTypeCallback){
        OkHttpUtil instence = OkHttpUtil.getInstence();
        instence.doGet(url, new OkHttpUtil.OkCallback() {
            @Override
            public void getSuccess(String json) {
                //成功
                if(mTypeCallback!=null){
                    mTypeCallback.getSuccess(json);
                }

            }

            @Override
            public void getError(Exception e) {
                //失败
                if(mTypeCallback!=null){
                    mTypeCallback.getError(e);
                }
            }
        });



        //求右边


    }

    public void getRightData(String cid, final OnRightDataCallback onRightDataCallback){
        OkHttpUtil instence = OkHttpUtil.getInstence();
        instence.doGet(HttpUrl.right_URL+"?cid="+cid, new OkHttpUtil.OkCallback() {
            @Override
            public void getSuccess(String json) {
                if (onRightDataCallback!=null) {
                    onRightDataCallback.getRightSuccess(json);
                }
            }

            @Override
            public void getError(Exception e) {
                if (onRightDataCallback!=null) {
                    onRightDataCallback.getRightError(e);
                }
            }
        });
    }



    public interface MTypeCallback {

        void getSuccess(String json);
        void getError(Exception e);
    }

    public interface OnRightDataCallback {

        void getRightSuccess(String json);
        void getRightError(Exception e);
    }
}
 

(2)TypePresenter 

public class TypePresenter extends BasePresenter<TypeView> {

    private TypeModel typeModel;

    public TypePresenter(TypeView view) {
        super(view);
    }


    public void typePresenter(String url){
        typeModel.typeModel(url, new TypeModel.MTypeCallback() {
            @Override
            public void getSuccess(String json) {
                if(view!=null){
                    view.getSuccess(json);
                }
            }

            @Override
            public void getError(Exception e) {
              if(view!=null){
                  view.getError(e);
              }
            }
        });
    }

    public void getRightData(String cid){
        typeModel.getRightData(cid, new TypeModel.OnRightDataCallback() {
            @Override
            public void getRightSuccess(String json) {
                if (view!=null) {
                    view.getRightSuccess(json);
                }

            }

            @Override
            public void getRightError(Exception e) {
                if (view!=null) {
                    view.getError(e);
                }
            }
        });
    }

    @Override
    protected void initModel() {
        typeModel = new TypeModel();
    }
}

(3)Type_Fragment/activity

public class Fragment_Type extends Fragment implements TypeView{
    private static final String TAG = "Fragment_Type======";
    private TypePresenter typePresenter;
    private ListView listView;
    private RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_type, container, false);
        listView=view.findViewById(R.id.listview);
        recyclerView = view.findViewById(R.id.recyleview);
        typePresenter = new TypePresenter(this);
       typePresenter.typePresenter(HttpUrl.TYPE_URL);

        typePresenter.getRightData("1");




        return view;
    }

    @Override
    public void getSuccess(String json) {
        //成功
        Log.d(TAG, "分类成功 "+json);
        Gson gson=new Gson();
        TypeBean typeBean = gson.fromJson(json, TypeBean.class);
        final List<TypeBean.DataBean> data = typeBean.getData();


        //创建适配器
        TypeAdapter typeAdapter=new TypeAdapter(getActivity(),data);
        listView.setAdapter(typeAdapter);
        typeAdapter.notifyDataSetChanged();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int cid = data.get(position).getCid();
                typePresenter.getRightData(cid+"");

            }
        });
    }

    @Override
    public void getError(Exception e) {

    }

    @Override
    public void getRightSuccess(String json) {
        RightBean rightBean = new Gson().fromJson(json, RightBean.class);
        if ("0".equals(rightBean.getCode())) {
            List<RightBean.DataBean> data = rightBean.getData();
            //shipeiqi
            MyRightAdapter myRightAdapter = new MyRightAdapter(data,getContext());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(myRightAdapter);

        }
    }

    @Override
    public void getRightError(Exception e) {

    }
}

(4.)适配器3个 左边ListviewAdapter,RightAdapter,GridAdapter

(1)ListView

public class TypeAdapter extends BaseAdapter {
    private Context context;
    private List<TypeBean.DataBean> list;


    public TypeAdapter(Context context, List<TypeBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            holder=new ViewHolder();
            convertView=View.inflate(context, R.layout.type_adapter,null);
            holder.tv_type=convertView.findViewById(R.id.tv_type);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }

        //赋值
        holder.tv_type.setText(list.get(position).getName());
        return convertView;
    }


    class ViewHolder{
        TextView tv_type;
    }
}

(2)RightAdapter

public class MyRightAdapter extends RecyclerView.Adapter<MyRightAdapter.MyRightViewHolder> {
    List<RightBean.DataBean> list;
    private Context context;

    public MyRightAdapter(List<RightBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public MyRightViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.type_rightitem,parent,false);
        return new MyRightViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyRightViewHolder holder, int position) {
        holder.textView.setText(list.get(position).getName());
        List<RightBean.DataBean.ListBean> list = this.list.get(position).getList();
        MyGridAdapter myGridAdapter = new MyGridAdapter(list);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context,3);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        holder.recyclerView.setLayoutManager(gridLayoutManager);
        holder.recyclerView.setAdapter(myGridAdapter);
    }

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

    public class MyRightViewHolder extends RecyclerView.ViewHolder{

        private final TextView textView;
        private final RecyclerView recyclerView;

        public MyRightViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_type_rightitem);
            recyclerView = itemView.findViewById(R.id.type_recyclerView_parent);
        }
    }
}


(3)GridAdapter

public class MyGridAdapter extends RecyclerView.Adapter<MyGridAdapter.MyGridViewHolder>{
    List<RightBean.DataBean.ListBean> list;

    public MyGridAdapter(List<RightBean.DataBean.ListBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyGridViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_layout,parent,false);
        return new MyGridViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyGridViewHolder holder, int position) {
        holder.textView.setText(list.get(position).getName());
        ImageLoader.getInstance().displayImage(list.get(position).getIcon(),holder.imageView, MyApp.getOptions());
    }

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

    public class MyGridViewHolder extends RecyclerView.ViewHolder{

        private final ImageView imageView;
        private final TextView textView;

        public MyGridViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_grid);
            textView = itemView.findViewById(R.id.tv_name_grid);

        }
    }
}


5.TypeView 接口

public interface TypeView extends IView {

    void getSuccess(String json);
    void getError(Exception e);

    void getRightSuccess(String json);
    void getRightError(Exception e);
}


6.TypeBean (1.ListViewBean) (2.RightBean)

(1)

public class TypeBean {


    /**
     * msg :
     * code : 0
     * data : [{"cid":1,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/shop.png","ishome":1,"name":"京东超市"},{"cid":2,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/qqg.png","ishome":1,"name":"全球购"},{"cid":3,"createtime":"2017-10-10T19:45:11","icon":"http://120.27.23.105/images/category/phone.png","ishome":1,"name":"手机数码"},{"cid":5,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/man.png","ishome":1,"name":"男装"},{"cid":6,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girl.png","ishome":1,"name":"女装"},{"cid":7,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/manshoe.png","ishome":1,"name":"男鞋"},{"cid":8,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girlshoe.png","ishome":1,"name":"女鞋"},{"cid":9,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/shirt.png","ishome":1,"name":"内衣配饰"},{"cid":10,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/bag.png","ishome":1,"name":"箱包手袋"},{"cid":11,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/beauty.png","ishome":1,"name":"美妆个护"},{"cid":12,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/jewel.png","ishome":1,"name":"钟表珠宝"},{"cid":13,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/luxury.png","ishome":1,"name":"奢侈品"},{"cid":14,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/computer.png","ishome":1,"name":"电脑办公"},{"cid":15,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"家用电器"},{"cid":16,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"食品生鲜"},{"cid":17,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"酒水饮料"},{"cid":18,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"母婴童装"},{"cid":19,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"玩具乐器"},{"cid":20,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"医药保健"}]
     */

    private String msg;
    private String code;
    private List<DataBean> 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<DataBean> getData() {
        return data;
    }

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

    public static class DataBean {
        /**
         * cid : 1
         * createtime : 2017-10-10T19:41:39
         * icon : http://120.27.23.105/images/category/shop.png
         * ishome : 1
         * name : 京东超市
         */

        private int cid;
        private String createtime;
        private String icon;
        private int ishome;
        private String name;

        public int getCid() {
            return cid;
        }

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

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getIcon() {
            return icon;
        }

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

        public int getIshome() {
            return ishome;
        }

        public void setIshome(int ishome) {
            this.ishome = ishome;
        }

        public String getName() {
            return name;
        }

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

(2)

public class RightBean {

    /**
     * msg : 获取子分类成功
     * code : 0
     * data : [{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"月饼","pcid":1,"pscid":1},{"icon":"http://120.27.23.105/images/icon.png","name":"坚果炒货","pcid":1,"pscid":2},{"icon":"http://120.27.23.105/images/icon.png","name":"糖巧","pcid":1,"pscid":3},{"icon":"http://120.27.23.105/images/icon.png","name":"休闲零食","pcid":1,"pscid":4},{"icon":"http://120.27.23.105/images/icon.png","name":"肉干肉脯","pcid":1,"pscid":5},{"icon":"http://120.27.23.105/images/icon.png","name":"饼干蛋糕","pcid":1,"pscid":6},{"icon":"http://120.27.23.105/images/icon.png","name":"蜜饯果干","pcid":1,"pscid":7},{"icon":"http://120.27.23.105/images/icon.png","name":"无糖食品","pcid":1,"pscid":8}],"name":"休闲零食","pcid":"1"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"新鲜水果","pcid":2,"pscid":9},{"icon":"http://120.27.23.105/images/icon.png","name":"海鲜水产","pcid":2,"pscid":10},{"icon":"http://120.27.23.105/images/icon.png","name":"精选肉类","pcid":2,"pscid":11},{"icon":"http://120.27.23.105/images/icon.png","name":"蛋类","pcid":2,"pscid":12},{"icon":"http://120.27.23.105/images/icon.png","name":"新鲜蔬菜","pcid":2,"pscid":13},{"icon":"http://120.27.23.105/images/icon.png","name":"冷冻食品","pcid":2,"pscid":14},{"icon":"http://120.27.23.105/images/icon.png","name":"饮品甜品","pcid":2,"pscid":15},{"icon":"http://120.27.23.105/images/icon.png","name":"大闸蟹","pcid":2,"pscid":16}],"name":"京东生鲜","pcid":"2"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"大米","pcid":3,"pscid":21},{"icon":"http://120.27.23.105/images/icon.png","name":"面粉","pcid":3,"pscid":22},{"icon":"http://120.27.23.105/images/icon.png","name":"杂粮","pcid":3,"pscid":23},{"icon":"http://120.27.23.105/images/icon.png","name":"食用油","pcid":3,"pscid":24},{"icon":"http://120.27.23.105/images/icon.png","name":"调味品","pcid":3,"pscid":25},{"icon":"http://120.27.23.105/images/icon.png","name":"方便速食","pcid":3,"pscid":26},{"icon":"http://120.27.23.105/images/icon.png","name":"有机食品","pcid":3,"pscid":27}],"name":"粮油调味","pcid":"3"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"饮用水","pcid":4,"pscid":28},{"icon":"http://120.27.23.105/images/icon.png","name":"饮料","pcid":4,"pscid":29},{"icon":"http://120.27.23.105/images/icon.png","name":"牛奶乳品","pcid":4,"pscid":30},{"icon":"http://120.27.23.105/images/icon.png","name":"名茶","pcid":4,"pscid":31},{"icon":"http://120.27.23.105/images/icon.png","name":"蜂蜜","pcid":4,"pscid":32}],"name":"水饮茗茶","pcid":"4"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"白酒","pcid":5,"pscid":33},{"icon":"http://120.27.23.105/images/icon.png","name":"葡萄酒","pcid":5,"pscid":34},{"icon":"http://120.27.23.105/images/icon.png","name":"洋酒","pcid":5,"pscid":35},{"icon":"http://120.27.23.105/images/icon.png","name":"啤酒","pcid":5,"pscid":36},{"icon":"http://120.27.23.105/images/icon.png","name":"黄酒","pcid":5,"pscid":37},{"icon":"http://120.27.23.105/images/icon.png","name":"陈年老酒","pcid":5,"pscid":38}],"name":"中外名酒","pcid":"5"}]
     */

    private String msg;
    private String code;
    private List<DataBean> 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<DataBean> getData() {
        return data;
    }

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

    public static class DataBean {
        /**
         * cid : 1
         * list : [{"icon":"http://120.27.23.105/images/icon.png","name":"月饼","pcid":1,"pscid":1},{"icon":"http://120.27.23.105/images/icon.png","name":"坚果炒货","pcid":1,"pscid":2},{"icon":"http://120.27.23.105/images/icon.png","name":"糖巧","pcid":1,"pscid":3},{"icon":"http://120.27.23.105/images/icon.png","name":"休闲零食","pcid":1,"pscid":4},{"icon":"http://120.27.23.105/images/icon.png","name":"肉干肉脯","pcid":1,"pscid":5},{"icon":"http://120.27.23.105/images/icon.png","name":"饼干蛋糕","pcid":1,"pscid":6},{"icon":"http://120.27.23.105/images/icon.png","name":"蜜饯果干","pcid":1,"pscid":7},{"icon":"http://120.27.23.105/images/icon.png","name":"无糖食品","pcid":1,"pscid":8}]
         * name : 休闲零食
         * pcid : 1
         */

        private String cid;
        private String name;
        private String pcid;
        private List<ListBean> list;

        public String getCid() {
            return cid;
        }

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

        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<ListBean> getList() {
            return list;
        }

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

        public static class ListBean {
            /**
             * icon : http://120.27.23.105/images/icon.png
             * name : 月饼
             * pcid : 1
             * pscid : 1
             */

            private String icon;
            private String name;
            private int pcid;
            private int pscid;

            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;
            }

            public int getPcid() {
                return pcid;
            }

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

            public int getPscid() {
                return pscid;
            }

            public void setPscid(int pscid) {
                this.pscid = pscid;
            }
        }
    }
}

7.购物车

(1)ShopCart_model

public class ShopCartModel {

    public void shopCartModel(String url, final MShopCartCallback mShopCartCallback){
        OkHttpUtil instence = OkHttpUtil.getInstence();
        instence.doGet(url, new OkHttpUtil.OkCallback() {
            @Override
            public void getSuccess(String json) {
                if(mShopCartCallback!=null){
                    mShopCartCallback.getSuccess(json);
                }
            }

            @Override
            public void getError(Exception e) {
                if(mShopCartCallback!=null){
                    mShopCartCallback.getError(e);
                }
            }
        });

}

(2.ShopCartPresenter)

public class ShopCartPresenter extends BasePresenter<ShopView> {

    private ShopCartModel shopCartModel;

    public ShopCartPresenter(ShopView view) {
        super(view);
    }

    public void shopCartPresenter(String url){
        shopCartModel.shopCartModel(url, new ShopCartModel.MShopCartCallback() {
            @Override
            public void getSuccess(String json) {
                //成功
                if(view!=null){
                    view.getSuccess(json);
                }
            }

            @Override
            public void getError(Exception e) {
                 //失败
                if(view!=null){
                    view.getError(e);
                }
            }
        });
    }

    @Override
    protected void initModel() {
        shopCartModel = new ShopCartModel();
    }
}

(3.ShopCartFragment/Activity)

public class Fragment_ShopCart extends Fragment implements ShopView, View.OnClickListener {
    private static final String TAG = "Fragment_ShopCart====";
    private View view;
    private ExpandableListView expandableListView;
    private CheckBox checkBox;
    private TextView tv_prive;
    private Button button;
    private ShopCartPresenter shopCartPresenter;
    private MyShopCartAdapter myShopCartAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_shopcart, container, false);

        //1.初始化视图
        initview();
        return view;
    }

    private void initview() {
        expandableListView = view.findViewById(R.id.expandableListView);
        checkBox = view.findViewById(R.id.checkbox_qx);
        tv_prive = view.findViewById(R.id.tv_hj);
        button = view.findViewById(R.id.but_qjs);

        //给checkbox点击事件
        checkBox.setOnClickListener(this);

        //2.实例化shopCartPresenter 对象
        shopCartPresenter = new ShopCartPresenter(this);
        shopCartPresenter.shopCartPresenter(HttpUrl.SHOPCART_URL);
    }




    @Override
    public void getSuccess(String json) {
        //成功
        Log.d(TAG, "购物车成功: "+json);
        Gson gson=new Gson();
        ShopCartBean shopCartBean = gson.fromJson(json, ShopCartBean.class);
        String code = shopCartBean.getCode();
        //3.判断code=0的时候去获取数据
        if("0".equals(code)){
            //拿到商家的集合
            List<ShopCartBean.DataBean> data = shopCartBean.getData();


            //4.创建适配器
            myShopCartAdapter = new MyShopCartAdapter(data,getContext());
            //5.给适配器一个监听
            myShopCartAdapter.setOnShoppingCarListListener(new MyShopCartAdapter.OnShoppingCarListListener() {
                @Override
                //商家被点击的时候
                public void onSellerCheckedChange(int groupPosition) {
                    //当前商家的商品checkbox是否被选中,判断商品
                    boolean b = myShopCartAdapter.ischangeCurrentSellerAllProductSelected(groupPosition);
                    //当商家的checkbox被点击的时候调用,用来全选商品或者全不选  判断所有
                    myShopCartAdapter.changeCurrentSellerAllProductStatuts(groupPosition,!b);
                    //改变状态后刷新
                    myShopCartAdapter.notifyDataSetChanged();
                    //刷新底部数据
                    refreshSelectedAndTotalPriceAndTotalNumber();
                }



                @Override
                //商品被点击的时候
                public void onProductCheckedChange(int groupPosition, int childPosition) {
                    //6.改变当前商品的状态
                    myShopCartAdapter.changeCurrentProductStatuts(groupPosition,childPosition);
                    myShopCartAdapter.notifyDataSetChanged();
                    //刷新底部数据
                    refreshSelectedAndTotalPriceAndTotalNumber();
                }

                @Override
                //数量被点击的时候
                public void onNumberCheckedChange(int groupPosition, int childPosition, int number) {
                    //7.改变当前商品的数量
                    myShopCartAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);
                    myShopCartAdapter.notifyDataSetChanged();

                    //刷新底部数据
                    refreshSelectedAndTotalPriceAndTotalNumber();

                }
            });
            expandableListView.setAdapter(myShopCartAdapter);



            //8.展开二级列表
            for (int i = 0; i <data.size() ; i++) {
                expandableListView.expandGroup(i);
            }

            //刷新checkbox状态 总价 和总数量
            refreshSelectedAndTotalPriceAndTotalNumber();
        }
    }

    @Override
    public void getError(Exception e) {
      //失败

    }


    //9.刷新checkbox状态 总价 和总数量
    private void refreshSelectedAndTotalPriceAndTotalNumber() {
        //判断是否所有的商品都被选中
        boolean allProductSelected = myShopCartAdapter.isAllProductSelected();
        //设置给全选checkbox
        checkBox.setSelected(allProductSelected);

        //计算总价
        float totalPrice = myShopCartAdapter.calculateTotalPrice();
        //给总价赋值
        tv_prive.setText("总价:"+totalPrice);


        //计算数量
        int numbers = myShopCartAdapter.calculateTotalNum();
        //给数量赋值
        button.setText("去结算:("+numbers+")");
    }

(4.ShopCartAdapter)

public class MyShopCartAdapter extends BaseExpandableListAdapter{
    private List<ShopCartBean.DataBean> list;
    private Context context;


    public MyShopCartAdapter(List<ShopCartBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList()==null ?0:list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        //1.拿到商家的信息
        ShopCartBean.DataBean dataBean = list.get(groupPosition);
        ViewHolderParent viewHolderParent;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.item_shopcart_parent,null);
            viewHolderParent=new ViewHolderParent();
            viewHolderParent.checkBox_parent=convertView.findViewById(R.id.checkbox_parent);
            viewHolderParent.tv_parent_tille=convertView.findViewById(R.id.tv_parent);
            convertView.setTag(viewHolderParent);
        }else {
            viewHolderParent= (ViewHolderParent) convertView.getTag();
        }

        //2.赋值商家名字
        viewHolderParent.tv_parent_tille.setText(dataBean.getSellerName());
        //3.根据商家确定商品的checkbox是否被选中
        boolean currentSellerAllProducts = isCurrentSellerAllProducts(groupPosition);
        viewHolderParent.checkBox_parent.setChecked(currentSellerAllProducts);
         //点击商家的checkbox
        viewHolderParent.checkBox_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onShoppingCarListListener!=null){
                    onShoppingCarListListener.onSellerCheckedChange(groupPosition);
                }
            }
        });

        return convertView;
    }
    //(3)
    private boolean isCurrentSellerAllProducts(int groupPosition) {
         //拿到商家的信息
        ShopCartBean.DataBean dataBean = list.get(groupPosition);
        //拿到商品的信息
        List<ShopCartBean.DataBean.ListBean> bean = dataBean.getList();

         //遍历商品
        for(ShopCartBean.DataBean.ListBean listBean:bean){
            //只要有一个未选中,商家就直接未选中
            if(listBean.getSelected()==0){
                return false;
            }
        }
         return true;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        //4.拿到商品的信息
        ShopCartBean.DataBean dataBean = list.get(groupPosition);
        List<ShopCartBean.DataBean.ListBean> listBean = dataBean.getList();//拿到全部商品
        ShopCartBean.DataBean.ListBean Bean = listBean.get(childPosition);//拿到某个商品

        ViewHolderChild viewHolderChild;
        if(convertView==null){
            convertView=View.inflate(context,R.layout.item_shopcart_child,null);
            viewHolderChild=new ViewHolderChild();
            viewHolderChild.tv_title_child=convertView.findViewById(R.id.tv_child_title);
            viewHolderChild.tv_price_child=convertView.findViewById(R.id.tv_child_price);
            viewHolderChild.imageView=convertView.findViewById(R.id.imageview_child);
            viewHolderChild.myAddSubView=convertView.findViewById(R.id.myaddview);
            viewHolderChild.checkBox_child=convertView.findViewById(R.id.checkbox_child);
            convertView.setTag(viewHolderChild);
        }else{
            viewHolderChild = (ViewHolderChild) convertView.getTag();
        }

        //赋值商品名字
        viewHolderChild.tv_title_child.setText(Bean.getTitle());//商品名字
        viewHolderChild.tv_price_child.setText(Bean.getPrice()+"");//商品价钱
        viewHolderChild.checkBox_child.setChecked(Bean.getSelected()==1);//商品checkbox状态
        //设置图片
        String[] split = Bean.getImages().split("\\|");
        ImageLoader.getInstance().displayImage(split[0],viewHolderChild.imageView, MyApp.getOptions());
        //点击商品checkbox
        viewHolderChild.checkBox_child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onShoppingCarListListener!=null){
                    onShoppingCarListListener.onProductCheckedChange(groupPosition,childPosition);
                }
            }
        });


        //5.自定义view  myAddSubView拿上商品最新数量
        viewHolderChild.myAddSubView.setNumber(Bean.getNum());
        viewHolderChild.myAddSubView.setOnNumberChangeListener(new MyAddSubView.onNumberChangeListener() {
            @Override
            public void getonNumberChange(int num) {
                //拿到商品的最新数量
                if(onShoppingCarListListener!=null){
                    onShoppingCarListListener.onNumberCheckedChange(groupPosition,childPosition,num);
                }
            }
        });


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    //6.当前商家的商品checkbox是否被选中,判断商品
    public boolean ischangeCurrentSellerAllProductSelected(int groupPosition) {
        ShopCartBean.DataBean dataBean = list.get(groupPosition);
        List<ShopCartBean.DataBean.ListBean> Bean = dataBean.getList();

        for(ShopCartBean.DataBean.ListBean listBean:Bean){
            //判断商家下的所有商品是否被选中,只要有一个未选中,商家就直接未选中
            if(listBean.getSelected()==0){
                return  false;
            }
        }
        return true;
    }

    //7.当商家的checkbox被点击的时候调用,用来全选商品或者全不选  判断所有
    public void changeCurrentSellerAllProductStatuts(int groupPosition, boolean isSelected) {
        ShopCartBean.DataBean dataBean = list.get(groupPosition);
        List<ShopCartBean.DataBean.ListBean> listBean = dataBean.getList();

        for (int i = 0; i < listBean.size(); i++) {
            //拿到商品
            ShopCartBean.DataBean.ListBean bean = listBean.get(i);
            //改变状态
            bean.setSelected(isSelected?1:0);
        }
    }

    //8.当商品得checkbox被点击得时候调用,改变当前商品状态
    public void changeCurrentProductStatuts(int groupPosition, int childPosition) {
        ShopCartBean.DataBean dataBean = list.get(groupPosition);
        List<ShopCartBean.DataBean.ListBean> listBean = dataBean.getList();
        ShopCartBean.DataBean.ListBean bean = listBean.get(childPosition);
        //状态 原来状态=0 设置1   原来状态=1 设置0
        bean.setSelected(bean.getSelected()==0 ?1:0);
    }


    //9.当加减器被点击得时候调用,改变当前商品得数量
    public void changeCurrentProductNumber(int groupPosition, int childPosition, int number) {
        ShopCartBean.DataBean dataBean = list.get(groupPosition);
        List<ShopCartBean.DataBean.ListBean> listBean = dataBean.getList();
        ShopCartBean.DataBean.ListBean bean = listBean.get(childPosition);
        bean.setNum(number);

    }

    //10.所有商品是否被选中
    public boolean isAllProductSelected() {
        for (int i = 0; i <list.size() ; i++) {
            ShopCartBean.DataBean dataBean = list.get(i);
            List<ShopCartBean.DataBean.ListBean> listBean = dataBean.getList();


            for (int j = 0; j < listBean.size(); j++) {
                //如果=0就是没选中
                if(listBean.get(j).getSelected()==0){
                    return false;
                }
            }
        }
        return  true;
    }


    //11.计算总价
    public float calculateTotalPrice() {
        float totalPrice=0;
        for (int i = 0; i <list.size() ; i++) {
            ShopCartBean.DataBean dataBean = list.get(i);
            List<ShopCartBean.DataBean.ListBean> listBean = dataBean.getList();

            for (int j = 0; j <listBean.size() ; j++) {
                //判断只要是选中的状态
                if(listBean.get(j).getSelected()==1){
                    //获取单价
                    float price = listBean.get(j).getPrice();
                    //获取数量
                    int num = listBean.get(j).getNum();
                    //总价=单价*数量
                    totalPrice+=price*num;
                }
            }
        }
        return totalPrice;
    }


    //12.计算数量
    public int calculateTotalNum() {
        int totalNumber=0;
        for (int i = 0; i < list.size(); i++) {
            ShopCartBean.DataBean dataBean = list.get(i);
            List<ShopCartBean.DataBean.ListBean> listBean = dataBean.getList();

            for (int j = 0; j < listBean.size(); j++) {
                //判断如果选中数量=1
                if(listBean.get(j).getSelected()==1){
                    //获取数量
                    int num = listBean.get(j).getNum();
                    //总数量=数量+数量
                    totalNumber+=num;
                }
            }
        }
        return totalNumber;
    }


    //13.改变所有商品的状态
    public void changeAllProductsStatus(boolean selected) {
        for (int i = 0; i <list.size() ; i++) {
            ShopCartBean.DataBean dataBean = list.get(i);
            List<ShopCartBean.DataBean.ListBean> listBean = dataBean.getList();

            for (int j = 0; j <listBean.size() ; j++) {
                listBean.get(j).setSelected(selected?1:0);
            }
        }
    }


    class ViewHolderParent{
        CheckBox checkBox_parent;
        TextView tv_parent_tille;
    }

    class ViewHolderChild{
        CheckBox checkBox_child;
        ImageView imageView;
        TextView tv_title_child,tv_price_child;
        MyAddSubView myAddSubView;
    }



    OnShoppingCarListListener onShoppingCarListListener;

    public void setOnShoppingCarListListener(OnShoppingCarListListener onShoppingCarListListener) {
        this.onShoppingCarListListener = onShoppingCarListListener;
    }

    //定义接口购物车
    public interface OnShoppingCarListListener{

        void onSellerCheckedChange(int groupPosition);//商家状态改变
        void onProductCheckedChange(int groupPosition,int childPosition);//商品状态改变
        void onNumberCheckedChange(int groupPosition,int childPosition,int number);//加减器状态改变

    }
}

8.自定义标题栏

public class MyTitleView extends LinearLayout {

    private View inflate;
    private TextView texttitle;

    public MyTitleView(Context context) {
        super(context,null);
    }

    public MyTitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        //1.加载布局
        inflate = inflate(context, R.layout.title_view, this);

        //2.拿到子view
        texttitle = inflate.findViewById(R.id.tv_title);

        //3.去获取属性
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTitleView, 0, 0);

        //4.用属性去获取
        String title = typedArray.getString(R.styleable.MyTitleView_title_text);
        int color = typedArray.getColor(R.styleable.MyTitleView_titlt_color, Color.RED);

        //5.赋值
        texttitle.setText(title);
        texttitle.setTextColor(color);
    }


}

<resources>
    <declare-styleable name="MyTitleView">
       <attr name="title_text" format="string"/>
        <attr name="titlt_color" format="color"/>
    </declare-styleable>
</resources>

9.布局

(1.)主页面

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".mvp.home.view.activity.HomeActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        >
    </android.support.v4.view.ViewPager>

   <TextView
       android:layout_width="match_parent"
       android:layout_height="2dp"
       android:background="#000"
       />

   <RadioGroup
       android:orientation="horizontal"
       android:id="@+id/radiogroup"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       >
     <RadioButton
         android:id="@+id/button1"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_weight="1"
         android:gravity="center"
         android:button="@null"
         android:textColor="#f00"
         android:textSize="20dp"
         android:background="@drawable/sy_selector"
         />

       <TextView
           android:layout_width="2dp"
           android:layout_height="match_parent"
           android:background="#000"
           />

       <RadioButton
           android:id="@+id/button2"
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:gravity="center"
           android:button="@null"
           android:textColor="#f00"
           android:textSize="20dp"
           android:background="@drawable/fl_selector"
           />
       <TextView
           android:layout_width="2dp"
           android:layout_height="match_parent"
           android:background="#000"
           />
       <RadioButton
           android:id="@+id/button3"
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:gravity="center"
           android:button="@null"
           android:background="@drawable/shop_selector"
           />

       <TextView
           android:layout_width="2dp"
           android:layout_height="match_parent"
           android:background="#000"
           />
       <RadioButton
           android:id="@+id/button4"
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:gravity="center"
           android:button="@null"
           android:textColor="#f00"
           android:textSize="20dp"
           android:background="@drawable/wd_selector"
           />

   </RadioGroup>


</LinearLayout>

(2)分类

(1.)主页面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ListView
        android:id="@+id/listview"
        android:layout_width="100dp"
        android:layout_weight="1"
        android:layout_height="match_parent">
    </ListView>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyleview"
        android:layout_width="120dp"
        android:layout_weight="9"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

(2.)RightItem

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/tv_type_rightitem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="商品分类"
        android:textSize="20dp"
        android:textColor="#f00"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/type_recyclerView_parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

(3.)TypeAdapter

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电脑"
        android:textSize="20dp"
        android:textColor="#000"
        android:padding="10dp"
        />

</LinearLayout>
 

(4.)GridView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    >
<ImageView
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:id="@+id/iv_grid"
    android:src="@mipmap/ic_launcher"
    android:scaleType="centerCrop"
    />
    <TextView
        android:id="@+id/tv_name_grid"
        android:text="233123123"
        android:gravity="center"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />
</LinearLayout>


(3)购物车布局

(1),主页面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/om"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.bwei.shop_cart.MyTitleView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:title_text="购物车"
        app:titlt_color="@color/colorPrimary"
        >
    </com.bwei.shop_cart.MyTitleView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000"
        />

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="60dp"
        >
    </ExpandableListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="#eeeeee"
        android:gravity="center_horizontal"
        >
        
     <CheckBox
         android:id="@+id/checkbox_qx"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />

     <TextView
         android:id="@+id/tv_qx"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="全选"
         android:textSize="20dp"
         />

      <TextView
          android:id="@+id/tv_hj"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:paddingLeft="20dp"
          android:text="合计:¥0.00"
          android:textSize="20dp"
          />


        <Button
            android:id="@+id/but_qjs"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="去结算(0)"
            />
    </LinearLayout>

</RelativeLayout>

(2.)addView 加减器

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="2dp"
    android:layout_marginLeft="10dp"
    android:layout_width="60dp"
    android:layout_height="30dp"
    android:layout_gravity="center_vertical"
    android:background="#99000000"
    android:gravity="center_vertical">

    <TextView
        android:background="#ffffff"
        android:layout_weight="1"
        android:id="@+id/tv_remove"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="-"
        android:textSize="16sp" />

    <TextView
        android:text="1"
        android:layout_marginLeft="2dp"
        android:background="#ffffff"
        android:layout_weight="1"
        android:id="@+id/tv_number"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
       />

    <TextView
        android:layout_marginLeft="2dp"
        android:background="#ffffff"
        android:layout_weight="1"
        android:id="@+id/tv_add"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="+"
        android:textSize="16sp" />


(3)父亲

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="60dp">

    <CheckBox
        android:id="@+id/checkbox_parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="父亲"
        />

</LinearLayout>
(4.)儿子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:gravity="center_horizontal"
    android:paddingLeft="20dp"
    android:layout_marginTop="20dp"
    android:layout_height="120dp">

   <CheckBox
       android:id="@+id/checkbox_child"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageview_child"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@mipmap/ic_launcher"
        />

    <LinearLayout
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingLeft="@dimen/textandiconmargin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


     <TextView
         android:id="@+id/tv_child_title"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="商品名称"
         android:textSize="20dp"
         android:maxLines="2"
         android:ellipsize="end"
         />

      <TextView
          android:id="@+id/tv_child_price"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="¥0.0"
          android:layout_marginTop="10dp"
          android:textSize="20dp"
          />
    </LinearLayout>

    <com.bwei.shop_cart.mvp.shoppcart.myaddsubview.MyAddSubView
        android:id="@+id/myaddview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dp"
        android:layout_marginTop="10dp"
        >
    </com.bwei.shop_cart.mvp.shoppcart.myaddsubview.MyAddSubView>

</LinearLayout>
 

MyAddView 类

public class MyAddSubView extends LinearLayout implements View.OnClickListener {

    //定义加减器
    private int number=1;

    private TextView tv_remove,tv_number,tv_add;
    private View view;

    public MyAddSubView(Context context) {
        super(context,null);
    }

    public MyAddSubView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);



        //1.加载addview布局
        view = inflate(context, R.layout.item_shopcart_addsubview, this);

        //获取资源id
        tv_remove = view.findViewById(R.id.tv_remove);
        tv_number=view.findViewById(R.id.tv_number);
        tv_add=view.findViewById(R.id.tv_add);

        tv_remove.setOnClickListener(this);
        tv_add.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_remove:
                if(number>1){
                    --number;
                    //把结果回调出去
                    tv_number.setText(number+"");
                    if(onNumberChangeListener!=null){
                        onNumberChangeListener.getonNumberChange(number);
                    }
                }

                break;

            case R.id.tv_add:
                ++number;
                   tv_number.setText(number+"");
                if(onNumberChangeListener!=null){
                    onNumberChangeListener.getonNumberChange(number);
                }

                break;
        }
    }

    //当展示购物车的时候,展示几件商品,展示出来
    public int getNumber() {
        return number;
    }


    public void setNumber(int number) {
        this.number = number;
        tv_number.setText(number+"");
    }




    onNumberChangeListener onNumberChangeListener;

    public void setOnNumberChangeListener(MyAddSubView.onNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

    //定义回调接口
    public interface onNumberChangeListener{
        void getonNumberChange(int num);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值