gouwuche加载view

来源:互联网 发布:无情刀永不知错 编辑:程序博客网 时间:2024/05/21 23:45


//CartActivity在view

public class CartActivity extends AppCompatActivity implements IMainActivity, View.OnClickListener{    private CartExpanableListview expanableListview;    private CartPresenter cartPresenter;    private CheckBox check_all;    private TextView text_total;    private TextView text_buy;    private CartBean cartBean;    private RelativeLayout relative_progress;    private MyAdapter myAdapter;    private LinearLayout linear_bottom;    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            if (msg.what == 0){                CountPriceBean countPriceBean = (CountPriceBean) msg.obj;                text_total.setText("合计:¥"+countPriceBean.getPriceString());                text_buy.setText("去结算("+countPriceBean.getCount()+")");            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_cart);        check_all = (CheckBox) findViewById(R.id.check_all);        text_total = (TextView) findViewById(R.id.text_total);        text_buy = (TextView) findViewById(R.id.text_buy);        expanableListview = (CartExpanableListview) findViewById(R.id.expanable_listview);        relative_progress = (RelativeLayout) findViewById(R.id.relative_progress);        linear_bottom = (LinearLayout) findViewById(R.id.linear_layout);        //去掉默认的指示器        expanableListview.setGroupIndicator(null);        cartPresenter = new CartPresenter(this);        //1.点击全选:选中/未选中...调用适配器中的方法...myAdapter.setIsCheckAll(true);来设置所有的一级和二级是否选中,计算        check_all.setOnClickListener(this);        text_buy.setOnClickListener(this);    }    @Override    protected void onResume() {        super.onResume();        relative_progress.setVisibility(View.VISIBLE);        //请求数据        cartPresenter.getCartData(ApiUtil.cartUrl);    }    @Override    public void getSuccessCartData(CartBean cartBean) {        relative_progress.setVisibility(View.GONE);        this.cartBean = cartBean;        if (cartBean != null){            //显示下面的            linear_bottom.setVisibility(View.VISIBLE);            //1.根据组中子条目是否选中,,,决定该组是否选中...初始化一下每一组中isGroupCheck这个数据            for (int i = 0;i<cartBean.getData().size();i++){                if (isAllChildInGroupSelected(i)){                    //更改i位置 组的选中状态                    cartBean.getData().get(i).setGroupChecked(true);                }            }            //2.根据每一个组是否选中的状态,,,初始化全选是否选中            check_all.setChecked(isAllGroupChecked());            //设置适配器            myAdapter = new MyAdapter(CartActivity.this, cartBean,handler,cartPresenter,relative_progress);            expanableListview.setAdapter(myAdapter);            //展开            for (int i= 0;i<cartBean.getData().size();i++){                expanableListview.expandGroup(i);            }            //3.根据子条目是否选中  初始化价格和数量            myAdapter.sendPriceAndCount();        }else {            //隐藏下面的全选.... 等            linear_bottom.setVisibility(View.GONE);            //显示去逛逛,,,添加购物车            Toast.makeText(CartActivity.this,"购物车为空,去逛逛",Toast.LENGTH_SHORT).show();        }    }    /**     * 所有的一级列表是否选中     */    private boolean isAllGroupChecked() {        for (int i =0;i<cartBean.getData().size();i++){            if (! cartBean.getData().get(i).isGroupChecked()){//代表一级列表有没选中的                return false;            }        }        return true;    }    /**     * 判断当前组里面所有的子条目是否选中     * @param groupPosition     * @return     */    private boolean isAllChildInGroupSelected(int groupPosition) {        for (int i= 0;i<cartBean.getData().get(groupPosition).getList().size();i++){            //只要有一个没选中就返回false            if (cartBean.getData().get(groupPosition).getList().get(i).getSelected() ==0){                return false;            }        }        return true;    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.check_all:                myAdapter.setAllChildState(check_all.isChecked());                break;            case R.id.text_buy://去结算...试一下创建订单                break;        }    }}

//IView

public interface IMainActivity {    void getSuccessCartData(CartBean cartBean);}

//custom

public class CartExpanableListview extends ExpandableListView {    public CartExpanableListview(Context context) {        super(context);    }    public CartExpanableListview(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CartExpanableListview(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, height);    }}

//adapter

public class MyAdapter extends BaseExpandableListAdapter{    private RelativeLayout relative_progress;    private CartPresenter cartPresenter;    private Handler handler;    private CartBean cartBean;    private Context context;    private int size;    private int childI;    private int allSize;    private int index;    public MyAdapter(Context context, CartBean cartBean, Handler handler, CartPresenter cartPresenter, RelativeLayout relative_progress) {        this.context = context;        this.cartBean = cartBean;        this.handler = handler;        this.cartPresenter = cartPresenter;        this.relative_progress = relative_progress;    }    @Override    public int getGroupCount() {        return cartBean.getData().size();    }    @Override    public int getChildrenCount(int groupPosition) {        return cartBean.getData().get(groupPosition).getList().size();    }    @Override    public Object getGroup(int groupPosition) {        return cartBean.getData().get(groupPosition);    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return cartBean.getData().get(groupPosition).getList().get(childPosition);    }    @Override    public long getGroupId(int groupPosition) {        return groupPosition;    }    @Override    public long getChildId(int groupPosition, int childPosition) {        return childPosition;    }    @Override    public boolean hasStableIds() {        return true;    }    @Override    public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {        final GroupHolder holder;        if (view == null){            view = View.inflate(context, R.layout.group_item_layout,null);            holder = new GroupHolder();            holder.check_group = view.findViewById(R.id.check_group);            holder.text_group = view.findViewById(R.id.text_group);            view.setTag(holder);        }else {            holder = (GroupHolder) view.getTag();        }        final CartBean.DataBean dataBean = cartBean.getData().get(groupPosition);        //赋值        holder.check_group.setChecked(dataBean.isGroupChecked());        holder.text_group.setText(dataBean.getSellerName());        //组的点击事件...也要去请求更新的接口        holder.check_group.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                relative_progress.setVisibility(View.VISIBLE);//显示                size = dataBean.getList().size();                childI = 0;                updateAllInGroup(holder.check_group.isChecked(),dataBean);            }        });        return view;    }    /**     * 更新一组的状态     * @param checked     * @param dataBean     */    private void updateAllInGroup(final boolean checked, final CartBean.DataBean dataBean) {        CartBean.DataBean.ListBean listBean = dataBean.getList().get(childI);//0        //?uid=71&sellerid=1&pid=1&selected=0&num=10        Map<String, String> params = new HashMap<>();        params.put("uid","3690");        params.put("sellerid", String.valueOf(listBean.getSellerid()));        params.put("pid", String.valueOf(listBean.getPid()));        params.put("selected", String.valueOf(checked ? 1:0));        params.put("num", String.valueOf(listBean.getNum()));        OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                if (response.isSuccessful()){                    childI = childI+1;//0,1,2...3                    if (childI <size){                        updateAllInGroup(checked,dataBean);                    }else {                        //所有的条目已经更新完成....再次请求查询购物车的数据                        cartPresenter.getCartData(ApiUtil.cartUrl);                    }                }            }        });    }    @Override    public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {        ChildHolder holder;        if (view == null){            view = View.inflate(context, R.layout.child_item_layout,null);            holder = new ChildHolder();            holder.text_add = view.findViewById(R.id.text_add);            holder.text_num = view.findViewById(R.id.text_num);            holder.text_jian = view.findViewById(R.id.text_jian);            holder.text_title = view.findViewById(R.id.text_title);            holder.text_price = view.findViewById(R.id.text_price);            holder.image_good = view.findViewById(R.id.image_good);            holder.check_child = view.findViewById(R.id.check_child);            holder.text_delete = view.findViewById(R.id.text_delete);            view.setTag(holder);        }else {            holder = (ChildHolder) view.getTag();        }        //赋值        final CartBean.DataBean.ListBean listBean = cartBean.getData().get(groupPosition).getList().get(childPosition);        holder.text_num.setText(listBean.getNum()+"");//......注意        holder.text_price.setText("¥"+listBean.getBargainPrice());        holder.text_title.setText(listBean.getTitle());        //listBean.getSelected().....0false,,,1true        //设置checkBox选中状态        holder.check_child.setChecked(listBean.getSelected()==0? false:true);        /*implementation 'com.github.bumptech.glide:glide:4.4.0'        annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'*/        //Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(holder.image_good);        String[] split = listBean.getImages().split("\\|");        ImageLoader.getInstance().displayImage                (split[0],holder.image_good, ImageUtilText.ImageUtil());        //点击事件        holder.check_child.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //点击的时候 更新当前条目选中的状态,,,更新完之后,请求查询购物车,重新展示数据                updateChildChecked(listBean);            }        });        //加号        holder.text_add.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //请求更新的接口                updateChildNum(listBean,true);            }        });        //减号        holder.text_jian.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {            if (listBean.getNum() == 1){                return;            }            //更新数量,,,减            updateChildNum(listBean,false);            }        });        return view;    }    /**     * 更新数量     * @param listBean     * @param     */    private void updateChildNum(CartBean.DataBean.ListBean listBean, boolean isAdded) {        //一旦执行更新的操作,,,progressBar显示        relative_progress.setVisibility(View.VISIBLE);        //?uid=71&sellerid=1&pid=1&selected=0&num=10        Map<String, String> params = new HashMap<>();        params.put("uid","3690");        params.put("sellerid", String.valueOf(listBean.getSellerid()));        params.put("pid", String.valueOf(listBean.getPid()));        params.put("selected", String.valueOf(listBean.getSelected()));        if (isAdded){            params.put("num", String.valueOf(listBean.getNum() + 1));        }else {            params.put("num", String.valueOf(listBean.getNum() - 1));        }        OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                //更新成功之后...网络上的数据发生了改变...再次请求购物车的接口进行数据的展示                if (response.isSuccessful()){                    cartPresenter.getCartData(ApiUtil.cartUrl);                }            }        });    }    /**     * 更新子条目 网络上的状态     * @param listBean     */    private void updateChildChecked(CartBean.DataBean.ListBean listBean) {        //一旦执行更新的操作,,,progressBar显示        relative_progress.setVisibility(View.VISIBLE);        //?uid=71&sellerid=1&pid=1&selected=0&num=10        Map<String, String> params = new HashMap<>();        params.put("uid","3690");        params.put("sellerid", String.valueOf(listBean.getSellerid()));        params.put("pid", String.valueOf(listBean.getPid()));        params.put("selected", String.valueOf(listBean.getSelected() == 0? 1:0));        params.put("num", String.valueOf(listBean.getNum()));        OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                //更新成功之后...网络上的数据发生了改变...再次请求购物车的接口进行数据的展示                if (response.isSuccessful()){                    cartPresenter.getCartData(ApiUtil.cartUrl);                }            }        });    }    @Override    public boolean isChildSelectable(int i, int i1) {        return true;    }    /**     * 计算价格和数量 并发送显示     */    public void sendPriceAndCount() {        double price = 0;        int count = 0;        //通过判断二级列表是否勾选,,,,计算价格数量        for (int i=0;i<cartBean.getData().size();i++){            for (int j = 0;j<cartBean.getData().get(i).getList().size();j++){                if (cartBean.getData().get(i).getList().get(j).getSelected() == 1){                    //价格是打折的价格...........                    price += cartBean.getData().get(i).getList().get(j).getNum() * cartBean.getData().get(i).getList().get(j).getBargainPrice();                    count += cartBean.getData().get(i).getList().get(j).getNum();                }            }        }        //精准的保留double的两位小数        DecimalFormat decimalFormat = new DecimalFormat("#.00");        String priceString = decimalFormat.format(price);        CountPriceBean countPriceBean = new CountPriceBean(priceString, count);        //发送...显示        Message msg = Message.obtain();        msg.what = 0;        msg.obj = countPriceBean;        handler.sendMessage(msg);    }    /**     * 根据全选的状态,,,,跟新每一个子条目的状态,,,全部更新完成后,查询购物车的数据进行展示     * @param checked     */    public void setAllChildState(boolean checked) {        //创建一个集合 装所有的子条目        List<CartBean.DataBean.ListBean> allList = new ArrayList<>();        for (int i=0;i<cartBean.getData().size();i++){            for (int j=0;j<cartBean.getData().get(i).getList().size();j++){                allList.add(cartBean.getData().get(i).getList().get(j));            }        }        relative_progress.setVisibility(View.VISIBLE);        allSize = allList.size();        index = 0;        //通过 递归 更新所有子条目的选中        updateAllChild(allList,checked);    }    /**     * 根据全选 跟新所有的子条目     * @param allList     * @param checked     */    private void updateAllChild(final List<CartBean.DataBean.ListBean> allList, final boolean checked) {        CartBean.DataBean.ListBean listBean = allList.get(index);//0        //跟新的操作        //?uid=71&sellerid=1&pid=1&selected=0&num=10        Map<String, String> params = new HashMap<>();        params.put("uid","3690");        params.put("sellerid", String.valueOf(listBean.getSellerid()));        params.put("pid", String.valueOf(listBean.getPid()));        params.put("selected", String.valueOf(checked ? 1:0));        params.put("num", String.valueOf(listBean.getNum()));        OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                if (response.isSuccessful()){                    index = index +1;//0,1,2......3                    if (index < allSize){                        updateAllChild(allList,checked);                    }else {                        //查询购物车                        cartPresenter.getCartData(ApiUtil.cartUrl);                    }                }            }        });    }    private class GroupHolder{        CheckBox check_group;        TextView text_group;    }    private class ChildHolder{        CheckBox check_child;        ImageView image_good;        TextView text_title;        TextView text_price;        TextView text_jian;        TextView text_num;        TextView text_add;        TextView text_delete;    }}

//drawable

//check_box_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_checked="true" android:drawable="@drawable/shopping_cart_checked"/>    <item android:state_checked="false" android:drawable="@drawable/shopping_cart_none_check"/>    <item android:drawable="@drawable/shopping_cart_none_check"/></selector>
//bian_kuang_line.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#ffffff" />    <stroke        android:width="0.1dp"        android:color="#000000" /></shape>

//layout

//activity的activity_cart

<RelativeLayout

<ScrollView    android:layout_above="@+id/linear_layout"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent">        <!--购物车的二级列表-->        <com.bwie.mygouwuche.view.custom.CartExpanableListview            android:id="@+id/expanable_listview"            android:layout_width="match_parent"            android:layout_height="wrap_content">        </com.bwie.mygouwuche.view.custom.CartExpanableListview>        <!--为你推荐-->        <LinearLayout            android:layout_marginTop="20dp"            android:orientation="vertical"            android:background="#00ff00"            android:layout_width="match_parent"            android:layout_height="500dp">        </LinearLayout>    </LinearLayout></ScrollView><RelativeLayout    android:visibility="gone"    android:id="@+id/relative_progress"    android:layout_above="@+id/linear_layout"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ProgressBar        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout><LinearLayout    android:id="@+id/linear_layout"    android:layout_alignParentBottom="true"    android:gravity="center_vertical"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="50dp">    <CheckBox        android:layout_marginLeft="10dp"        android:button="@null"        android:background="@drawable/check_box_selector"        android:id="@+id/check_all"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/text_total"        android:text="合计:¥0.00"        android:layout_weight="2"        android:layout_width="0dp"        android:layout_height="wrap_content" />    <TextView        android:text="去结算(0)"        android:background="#ff0000"        android:textColor="#ffffff"        android:gravity="center"        android:id="@+id/text_buy"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent" /></LinearLayout>