购物车 ( Retrofit+RxJava+MVP)

来源:互联网 发布:杭州 大学生家教 知乎 编辑:程序博客网 时间:2024/06/07 23:02

加减器

public class Addview extends RelativeLayout {    public TextView count;    public Addview(Context context) {        this(context,null);    }    public Addview(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public Addview(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView(context, attrs, defStyleAttr);    }    private OnAddDeleteClickListener listener;    //对外提供一个点击的回调接口    public interface OnAddDeleteClickListener{        void onAddClick(View v);        void onDelClick(View v);    }    public void setOnAddDeleteClick(OnAddDeleteClickListener listener){        this.listener = listener;    }    private void initView(final Context context, AttributeSet attrs, int defStyleAttr) {        View view = View.inflate(context, R.layout.view_add, this);        ImageView delete = (ImageView)view.findViewById(R.id.delete);        count = (TextView)view.findViewById(R.id.count);        ImageView add = (ImageView)view.findViewById(R.id.add);        delete.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                listener.onDelClick(view);            }        });        add.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                listener.onAddClick(view);            }        });    }    //对外提供设置EditText值的方法    public void setNumber(int number){        count.setText(number + "");    }    //得到控件原来的值,并转成int类型    public int getNumber(){        int number = Integer.parseInt(count.getText().toString().trim());        return number;    }}
加减器布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/delete"        android:src="@drawable/down"        android:layout_width="25dp"        android:layout_height="25dp" />    <View        android:id="@+id/view"        android:layout_width="20dp"        android:layout_height="1dp"        android:layout_marginTop="2dp"        android:background="#5c595c"        android:layout_toRightOf="@id/delete"/>    <TextView        android:id="@+id/count"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="1"        android:layout_toRightOf="@id/delete"        android:layout_marginTop="5dp"        android:layout_marginLeft="5dp"/>    <View        android:layout_width="20dp"        android:layout_height="1dp"        android:background="#5c595c"        android:layout_toRightOf="@id/delete"        android:layout_marginTop="23dp"/>    <ImageView        android:id="@+id/add"        android:src="@drawable/up"        android:layout_width="25dp"        android:layout_height="25dp"        android:layout_alignParentTop="true"        android:layout_toRightOf="@+id/view"        android:layout_toEndOf="@+id/view" /></RelativeLayout>

购物车布局:

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:orientation="vertical"        android:layout_height="match_parent"        android:layout_above="@+id/linearLayout">        <ExpandableListView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:groupIndicator="@null"            android:id="@+id/ev">        </ExpandableListView>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:id="@+id/linearLayout">        <CheckBox            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:id="@+id/cb_all"            android:text="全选"/>        <TextView            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:id="@+id/text_sum"            android:text="总价:0.0  "/>        <Button            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:id="@+id/btn_del"            android:text="删除"/>    </LinearLayout></RelativeLayout>

购物车适配器

public class CarAdapter extends BaseExpandableListAdapter{    private Context context;    private List<FatherBean> list;    private List<List<ChildBean>> lists;    private CarFragment f;    public CarAdapter(Context context, List<FatherBean> list, List<List<ChildBean>> lists, CarFragment f) {        this.context = context;        this.list = list;        this.lists = lists;        this.f=f;    }    @Override    public int getGroupCount() {        return list.size();    }    @Override    public int getChildrenCount(int i) {        return lists.get(i).size();    }    @Override    public Object getGroup(int i) {        return list.get(i);    }    @Override    public Object getChild(int i, int i1) {        return lists.get(i).get(i1);    }    @Override    public long getGroupId(int i) {        return i;    }    @Override    public long getChildId(int i, int i1) {        return i1;    }    @Override    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {        final FatherBean fatherbean = list.get(i);        final List<ChildBean> childbeen = lists.get(i);        view = View.inflate(this.context, R.layout.view_father_car, null);        TextView textView = (TextView) view.findViewById(R.id.title_father_car);        final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_father_car);        textView.setText(fatherbean.getName());        checkBox.setChecked(fatherbean.isflag());        //复选框点击事件        checkBox.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                boolean checked = checkBox.isChecked();                fatherbean.setIsflag(checked);                for (int j=0;j<childbeen.size();j++){                    childbeen.get(j).setIsflag(checked);                }                //计算总价方法                f.mouth(f);                boolean setcheck = f.setcheck();                f.all.setChecked(setcheck);                notifyDataSetChanged();            }        });        return view;    }    @Override    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {        final ChildBean bean = lists.get(i).get(i1);        //加载布局        view= View.inflate(context, R.layout.view_child_car,null);        //获取构件        ImageView img = (ImageView) view.findViewById(R.id.child_img);        TextView title = (TextView) view.findViewById(R.id.child_tit);        Addview addview = (Addview) view.findViewById(R.id.addview);        final TextView price = (TextView) view.findViewById(R.id.child_price);        final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_child);        //修改内容        addview.setNumber(bean.getNum());        price.setText(bean.getPrice()+"");        title.setText(bean.getName());        checkBox.setChecked(lists.get(i).get(i1).isflag());        Glide.with(context).load(bean.getImgurl()).into(img);        //加减器点击事件        addview.setOnAddDeleteClick(new Addview.OnAddDeleteClickListener() {            @Override            public void onAddClick(View v) {                int num = bean.getNum();                num++;                bean.setNum(num);                //计算总价方法                f.mouth(f);                notifyDataSetChanged();            }            @Override            public void onDelClick(View v) {                int num = bean.getNum();                if(num>1){                    num--;                }                bean.setNum(num);                //计算总价方法                f.mouth(f);                notifyDataSetChanged();            }        });        checkBox.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                boolean checked = checkBox.isChecked();                lists.get(i).get(i1).setIsflag(checked);                boolean b=true;                if (checked){                    for (int j=0;j<lists.get(i).size();j++){                        boolean flag = lists.get(i).get(j).isflag();                        if (flag==false){                            b=false;                        }                    }                    //修改父级的选中状态                    if (b){                        list.get(i).setIsflag(true);                    }                }else{                    list.get(i).setIsflag(false);                }                //计算总价方法                f.mouth(f);                boolean setcheck = f.setcheck();                f.all.setChecked(setcheck);                notifyDataSetChanged();            }        });        return view;    }    @Override    public boolean isChildSelectable(int i, int i1) {        return true;    }    @Override    public boolean hasStableIds() {        return true;    }}


子类布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal">        <CheckBox            android:id="@+id/cb_child"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:layout_marginTop="35dp" />        <ImageView            android:id="@+id/child_img"            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@mipmap/ic_launcher_round" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginTop="30dp"                android:text="xxx"                android:id="@+id/child_tit"/>            <LinearLayout                android:layout_marginTop="10dp"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="horizontal">                <TextView                    android:layout_width="0dp"                    android:layout_weight="1"                    android:layout_height="wrap_content"                    android:text="xxx"                    android:id="@+id/child_price"/>                <com.bwie.car.View.Addview                    android:id="@+id/addview"                    android:layout_width="0dp"                    android:layout_weight="1"                    android:layout_height="wrap_content">                </com.bwie.car.View.Addview>            </LinearLayout>        </LinearLayout>    </LinearLayout></LinearLayout>

父类布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <CheckBox            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="5dp"            android:id="@+id/cb_father_car"/>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="xxx"            android:id="@+id/title_father_car"/>    </LinearLayout></LinearLayout>

子类bean

public class ChildBean {    private String name;    private String imgurl;    private String pid ;    private boolean isflag;    private double price;    private int num;    public ChildBean(String name, String imgurl, String pid, double price, boolean isflag, int num) {        this.name = name;        this.imgurl = imgurl;        this.pid = pid;        this.isflag = isflag;        this.price = price;        this.num=num;    }    public int getNum() {        return num;    }    public void setNum(int num) {        this.num = num;    }    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    public boolean isflag() {        return isflag;    }    public void setIsflag(boolean isflag) {        this.isflag = isflag;    }    public ChildBean(String name, String imgurl, String pid) {        this.name = name;        this.imgurl = imgurl;        this.pid = pid;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getImgurl() {        return imgurl;    }    public void setImgurl(String imgurl) {        this.imgurl = imgurl;    }    public String getPid() {        return pid;    }    public void setPid(String pid) {        this.pid = pid;    }}

购物车逻辑

public class CarFragment extends Fragment implements IView {    private View view;    public CheckBox all;    private Button del;    public TextView sum;    private ExpandableListView ev;    private List<FatherBean> list;    private List<List<ChildBean>> lists;    private Presenter presenter;    private CarAdapter adapter;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        view = inflater.inflate(R.layout.view_car, container, false);        return view;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        all = (CheckBox) view.findViewById(R.id.cb_all);        del = (Button) view.findViewById(R.id.btn_del);        sum = (TextView) view.findViewById(R.id.text_sum);        ev = (ExpandableListView) view.findViewById(R.id.ev);        list=new ArrayList<>();        lists=new ArrayList<>();        //配置适配器        adapter = new CarAdapter(getActivity(),list,lists,this);        ev.setAdapter(adapter);        //全选按钮点击事件        all.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                for(int i=0;i<list.size();i++){                    list.get(i).setIsflag(all.isChecked());                }                for(int i=0;i<lists.size();i++){                    for(int j=0;j<lists.get(i).size();j++){                        lists.get(i).get(j).setIsflag(all.isChecked());                    }                }                int n=0;                for (int j=0;j<lists.size();j++){                    List<ChildBean> beanList = lists.get(j);                    for (int k=0;k<beanList.size();k++){                        if(beanList.get(k).isflag()){                            int num = beanList.get(k).getNum();                            double price1 = beanList.get(k).getPrice();                            n+=price1*num;                        }                    }                }                sum.setText("总价为:"+n);                //刷新适配器                adapter.notifyDataSetChanged();            }        });    }    //请求网络方法    public void init(){        presenter = new Presenter(this);        Map<String,String> map = new HashMap<>();        map.put("source", "android");        map.put("uid","4479");        presenter.get("http://120.27.23.105/",map);    }    //判断选中状态    public boolean setcheck(){        boolean b=true;        for (int i=0;i<list.size();i++){            if (!list.get(i).isflag()){                b=list.get(i).isflag();                break;            }            List<ChildBean> beanList = lists.get(i);            for(int j=0;j<beanList.size();j++){                if (!beanList.get(j).isflag()){                    b=beanList.get(j).isflag();                    break;                }            }        }        return b;    }    //计算总价的方法    public void mouth(CarFragment f){        float n=0;        for (int j=0;j<lists.size();j++){            List<ChildBean> beanList = lists.get(j);            for (int k=0;k<beanList.size();k++){                if(beanList.get(k).isflag()){                    int num = beanList.get(k).getNum();                    double price1 = beanList.get(k).getPrice();                    n+=price1*num;                }            }        }        f.sum.setText("总价为:"+n);    }@Override    public void onSuccess(Object o, String Tag) {        if(Tag.equals("1")){            list.clear();            lists.clear();            bean a = (bean) o;            List<bean.DataBean> beanList = a.getData();            //建立数据源            for(int i=0;i<beanList.size();i++) {                String name = beanList.get(i).getSellerName();                list.add(new FatherBean(name,true));                List<bean.DataBean.ListBean> listBeen = beanList.get(i).getList();                List<ChildBean> p=new ArrayList<>();                for (int j = 0; j < listBeen.size(); j++) {                    double price = listBeen.get(j).getPrice();                    String s = listBeen.get(j).getImages();                    String[] strings = s.split("!");                    String title = listBeen.get(j).getTitle();                    int id = listBeen.get(j).getPid();                    int num = listBeen.get(j).getNum();                    p.add(new ChildBean(title, strings[0], id + "", price, true, num));                }                lists.add(p);            }            //默认展开           for (int s = 0; s < adapter.getGroupCount(); s++) {                ev.expandGroup(s);            }            //刷新适配器            adapter.notifyDataSetChanged();        }else {        }    }    @Override    public void onFailed(Exception e) {    }



删除点击事件

 //删除按钮点击事件        delete.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                for(int i=0;i<lists.size();i++){                    List<ChildBean> beanList = lists.get(i);                    for (int j=0;j<beanList.size();j++){                        ChildBean bean = beanList.get(j);                        if (bean.isflag()){                            String pid = bean.getPid();                            lists.get(i).remove(j);                            Map<String,String> map1=new HashMap<String, String>();                            map1.put("uid",App.sp.getString("userid", ""));                            map1.put("pid",pid);                            presenter.get("http://120.27.23.105/product/deleteCart",map1, AddBean.class,"2");                            mouth(Frag_shopcar.this);                        }                    }                    if (lists.get(i).size()==0){                        lists.remove(i);                        list.remove(i);                    }                }                adapter.notifyDataSetChanged();            }        });


 
原创粉丝点击