Junk shopping cart and junk shopping cart

Source: Internet
Author: User

Junk shopping cart and junk shopping cart

Junk shopping cart


Using MVP + OkHttp, you can easily make this shopping cart.

However, communication between components in the application and between components and background threads is often required during project creation. For example, after time-consuming operations are completed, the result is notified to the UI through Handler or Broadcast, and the N activities need to communicate with each other through Listener. In fact, these operations can be easily implemented through EventBus.
EventBus is a release/subscription event bus optimized for Android. The communication between components in the application and between components and background threads is simplified. The advantage is that the overhead is small, the code is more elegant, and the sender and receiver are decoupled. If the Activity interacts with the Activity, it is a headache to say that if the Fragment interacts with the Fragment, we will use broadcast for processing, but it is a little troublesome and inefficient to use broadcast, if the transmitted data is an entity class that requires serialization, the cost is obviously a little high.

Compile 'org. greenrobot: eventbus: 3.0.0'

EventBus main roles:

The producer ThreadMode of the Subscriber Publisher Event of the Subscriber Event passed by Event defines the thread in which the function is executed.

MVP, be lazy if you are lazy.
M layer

  @Override    public double getSum(List
 
   list, int num) {        double sum = 0;        for (int i = 0; i < list.size(); i++) {            List
  
    list1 = list.get(i).getList();            for (int j = 0; j < list1.size(); j++) {                boolean child_flag = list1.get(j).getChild_flag();                if (child_flag) {                    double v = list1.get(j).getPrice() * num;                    sum = v + sum;                }            }        }        return sum;    }
  
 

P-layer contact layer

/*** Display quantity and price */public void showCountAndPrice (List
 
  
List, int num) {double sum = loadCartListData. getSum (list, num); double round = round (sum, 2); cartView. priceAndNum (round, num);} public static double round (double v, int scale) {if (scale <0) {throw new IllegalArgumentException ("The scale must be a positive integer or zero");} BigDecimal B = new BigDecimal (Double. toString (v); BigDecimal one = new BigDecimal ("1"); return B. pide (one, scale, BigDecimal. ROUND_HALF_UP ). doubleValue ();}
 

Okay, the adapter is missing.
I am tired of writing this, so don't bother with it, and reference various calls.

Public class CartAdapter extends BaseExpandableListAdapter {private static final String TAG = "Shopping Cart adapter"; private List
 
  
List; private Context context; private CartView cartView; private CartPresenter cartPresenter; public CartAdapter (List
  
   
List, Context context, CartView cartView) {this. list = list; this. context = context; this. cartView = cartView; this. cartPresenter = new CartPresenter (cartView);} @ Override public int getGroupCount () {return list. size () ;}@ Override public int getChildrenCount (int groupPosition) {return list. get (groupPosition ). getList (). size () ;}@ Override public View getGroupView (final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {final GroupViewHolder groupViewHolder; if (convertView = null) {groupViewHolder = new GroupViewHolder (); convertView = View. inflate (context, R. layout. group_view, null); groupViewHolder. groupcheckbox = convertView. findViewById (R. id. chack_merchant); groupViewHolder. merchant = convertView. findViewById (R. id. merchant); convertView. setTag (groupViewHolder);} else {groupViewHolder = (GroupViewHolder) convertView. getTag ();} // obtain merchant data final CartBean. dataBean dataBean = list. get (groupPosition); // The Merchant check box to set the status value groupViewHolder. groupcheckbox. setChecked (dataBean. isGroup_flag (); // merchant name groupViewHolder. merchant. setText (dataBean. getSellerName () + ""); // select the Product Event groupViewHolder under the seller. groupcheckbox. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {boolean checked = groupViewHolder. groupcheckbox. isChecked (); // set the status dataBean for the check box. setGroup_flag (checked); changeMerchantGoods (checked, groupPosition); // boolean flag = isCheckAllGoods (); // you can check whether all commodities have been selected. policydatasetchanged (); // display the quantity and price of cartPresenter. showCountAndPrice (list, 1) ;}}); return convertView ;}/ *** select all items */public void checkAllGoods (boolean checked) {for (int I = 0; I <list. size (); I ++) {list. get (I ). setGroup_flag (checked); List
   
    
ChildList = list. get (I ). getList (); for (int j = 0; j <childList. size (); j ++) {childList. get (j ). setChild_flag (checked) ;}} yydatasetchanged () ;}/ *** determine whether to select all items */public boolean isCheckAllGoods () {boolean flag = true; // traverse whether the parent list is selected for (int I = 0; I <list. size (); I ++) {boolean group_flag = list. get (I ). getGroup_flag (); if (group_flag = false) {flag = false;} // traverses whether the sublist is selected
    
     
ChildList = list. get (I ). getList (); for (int j = 0; j <childList. size (); j ++) {boolean child_flag = childList. get (j ). getChild_flag (); if (child_flag = false) {flag = false ;}} return flag ;} /*** select all and reselect the commodities under the seller */private void changeMerchantGoods (boolean checked, int groupPosition) {List
     
      
ChildList = list. get (groupPosition ). getList (); for (int I = 0; I <childList. size (); I ++) {childList. get (I ). completed (checked);} policydatasetchanged () ;}@ Override public View getChildView (final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {final ChildViewHolder childViewHolder; if (convertView = null) {childViewHolder = new ChildViewHolder (); convertView = View. inflate (context, R. layout. child_view, null); childViewHolder. goods = convertView. findViewById (R. id. goods _); childViewHolder. goodprice = convertView. findViewById (R. id. goods_price); childViewHolder. goodsImage = convertView. findViewById (R. id. goods_img); childViewHolder. amountView = convertView. findViewById (R. id. add_and_down); childViewHolder. childgroupcheckbox = convertView. findViewById (R. id. check_goods); convertView. setTag (childViewHolder);} else {childViewHolder = (ChildViewHolder) convertView. getTag () ;}// get the List of the current sublist set
      
        ChildList = list. get (groupPosition ). getList (); // obtain the bean class final CartBean. goodsBean goodsBean = childList. get (childPosition); // sets the status value childViewHolder. childgroupcheckbox. setChecked (goodsBean. isChild_flag (); // The product title childViewHolder. goods. setText (goodsBean. get () + ""); childViewHolder. goodprice. setText ("$" + goodsBean. getPrice (); Glide. with (context ). load (list. get (groupPosition ). getList (). get (childPosition ). getImages (). split ("[|]") [0]). into (childViewHolder. goodsImage); childViewHolder. amountView. setGoods_storage (50); // The childViewHolder in the shopping cart. amountView. setOnAmountChangeListener (new AmountView. onAmountChangeListener () {@ Override public void onAmountChange (View view, int amount) {cartPresenter. showCountAndPrice (list, amount) ;}}); childViewHolder. childgroupcheckbox. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// sublist setting State boolean checked = childViewHolder. childgroupcheckbox. isChecked (); goodsBean. setChild_flag (checked); boolean flag = isCheckAllChildGoods (list. get (groupPosition ). getList (); // determines whether all items under the seller are selected. get (groupPosition ). setGroup_flag (flag); // based on the judgment result, set the status of the parent list to yydatasetchanged (); cartPresenter. showCountAndPrice (list, 1) ;}}); return convertView ;}/ *** determines whether all commodities under the seller are selected */private boolean isCheckAllChildGoods (List
       
         ChildList) {// traverses the word list to determine whether to select all the items in the sublist for (int I = 0; I <childList. size (); I ++) {boolean child_flag = childList. get (I ). getChild_flag (); if (child_flag = false) {return false ;}return true;} class GroupViewHolder {CheckBox groupcheckbox; TextView merchant;} class ChildViewHolder {CheckBox childgroupcheckbox; ImageView goodsImage; textView goods; TextView goodprice; AmountView amountView ;}}
       
      
     
    
   
  
 

Activity Layer

// Cart @ Override public void loadCartListData (CartBean cartBean) {data = cartBean. getData (); final CartAdapter cartAdapter = new CartAdapter (data, getActivity (), this); mExpandableCart. setAdapter (cartAdapter); mExpandableCart. setOnGroupClickListener (new ExpandableListView. onGroupClickListener () {@ Override public boolean onGroupClick (ExpandableListView parent, View v, int groupPosition, long id) {return true ;}}); int count = mExpandableCart. getCount (); for (int I = 0; I <count; I ++) {mExpandableCart. expandGroup (I);} // select all/reselect mQuanxuan. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {cartAdapter. checkAllGoods (mQuanxuan. isChecked (); cartPresenter. showCountAndPrice (data, 1) ;}}) ;}// display price and quantity @ Override public void priceAndNum (double sum, int num) {mQujiesuan. setText ("Total:" + sum );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.