ショッピングカート(中)


**
第二編:いくつかのショッピングカート特有の方法を実現し、例えば、この業者と商品が選択されているかどうかを判断し、商品の総数量を計算し、商品の総価格を計算し、またこれはユーザーの選択によって、業者や商品の状態を変える.
**
この文章は前の文章に比べて更新されました.この一歩は主にAdapterでいくつかの操作をしました.だから、みんなの読書時間を節約しました.ここではAdapterの具体的な操作コードの具体的な操作は以下のステップがあります.ショッピングカート特有の方法2.ユーザの選択により、チェックボックスの状態を変更する.インタフェースの固定形式を定義してコードを見ます
package com.cart.cartdemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.zip.Inflater;

/**
* date:2018/11/20
* author:  (HUAWEI)
* function:
*/
class MyAdapter extends BaseExpandableListAdapter {

  private List mDataBeans;
  Context mContext;
  public MyAdapter(List data, MainActivity mainActivity) {
      this.mDataBeans = data;
      mContext = mainActivity;
  }

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

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

  /////////////////////////           ////////////////////
  @Override
  public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      //   bean       
      CartInfo.DataBean dataBean = mDataBeans.get(groupPosition);
      ParentViewHolder parentViewHolder;
      if (convertView == null) {
          convertView = View.inflate(mContext, R.layout.item_cart_parent, null);
          parentViewHolder = new ParentViewHolder(convertView);
          convertView.setTag(parentViewHolder);
      } else {
          parentViewHolder = (ParentViewHolder) convertView.getTag();
      }
      //     
      parentViewHolder.seller_name_tv.setText(mDataBeans.get(groupPosition).getSellerName());
      //D.           ,     Checkbox    
      boolean goodsSelected = isGoodsSelected(groupPosition);
      //D.    Boolean      
      parentViewHolder.seller_cb.setChecked(goodsSelected);
      //D.    Checkbox
      parentViewHolder.seller_cb.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (mOnCartListener != null){
                  mOnCartListener.onParentCheckboxChange(groupPosition);
              }
          }
      });
      return convertView;
  }

  public static class ParentViewHolder {
      public CheckBox seller_cb;
      public TextView seller_name_tv;

      public ParentViewHolder(View rootView) {
         this.seller_cb = (CheckBox) rootView.findViewById(R.id.seller_cb);
          this.seller_name_tv = (TextView) rootView.findViewById(R.id.seller_name_tv);
      }

  }

  ///////////////////            ///////////////
  @Override
  public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      //           
      CartInfo.DataBean dataBean = mDataBeans.get(groupPosition);
      List list = dataBean.getList();

      //  List          
      CartInfo.DataBean.ListBean listBean = list.get(childPosition);

      ChildViewHolder childViewHolder;
      if (convertView == null) {
          convertView = View.inflate(mContext,R.layout.item_cart_child,null);
          childViewHolder = new ChildViewHolder(convertView);
          convertView.setTag(childViewHolder);
      } else {
          childViewHolder = (ChildViewHolder) convertView.getTag();
      }
      //      
      childViewHolder.product_title_name_tv.setText(listBean.getTitle());
      //      
      childViewHolder.product_price_tv.setText(listBean.getPrice()+"");
      //         
      childViewHolder.child_cb.setChecked(listBean.getSelected() == 1);
      //               
      childViewHolder.add_remove_view.setNumber(listBean.getNum());

      //D.    Checkbook     ,      ,     
      childViewHolder.child_cb.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (mOnCartListener != null){
                  mOnCartListener.onChildCheckboxChange(groupPosition,childPosition);
              }
          }
      });

      //D.           ,      ,     
      childViewHolder.add_remove_view.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {
          @Override
          public void onNumberChange(int num) {
              if (mOnCartListener != null){
                  mOnCartListener.onAddSubNumberChange(groupPosition,childPosition,num);
              }
          }
      });
      return convertView;
  }

  public static class ChildViewHolder {
      public CheckBox child_cb;
      public ImageView product_icon_iv;
      public TextView product_title_name_tv;
      public TextView product_price_tv;
      public MyAddSubView add_remove_view;

      public ChildViewHolder(View rootView) {
          this.child_cb = (CheckBox) rootView.findViewById(R.id.child_cb);
          this.product_icon_iv = (ImageView) rootView.findViewById(R.id.product_icon_iv);
          this.product_title_name_tv = (TextView) rootView.findViewById(R.id.product_title_name_tv);
          this.product_price_tv = (TextView) rootView.findViewById(R.id.product_price_tv);
          this.add_remove_view = (MyAddSubView) rootView.findViewById(R.id.add_remove_view);
      }

  }

  //////////////////////////////B.            ///////////////////////////
 //B.               (          ,           )
  public boolean isGoodsSelected(int groupPosition){
       //        
      CartInfo.DataBean dataBean = mDataBeans.get(groupPosition);
      //           ,     
      List list = dataBean.getList();

      for (CartInfo.DataBean.ListBean listBean : list){
          //               ,          ,           
          if (listBean.getSelected() == 0){
              return false;
          }
      }
      return true;
  }
  //B.            ,    ,      ,           (          ,           )
  public boolean isAllGoodsSelected(){
      for (int x = 0; x < mDataBeans.size(); x++){
          CartInfo.DataBean dataBean = mDataBeans.get(x);
          List list = dataBean.getList();
          for (int j = 0; j < list.size(); j++){
              //               ,          ,             
              if (list.get(j).getSelected() == 0){
                  return false;
              }
          }
      }
      return true;
  }
 //B        
  public int goodsTotalNumber(){
      int totalNumber = 0;
      for (int x=0; x < mDataBeans.size(); x++){
          CartInfo.DataBean dataBean = mDataBeans.get(x);
          List list = dataBean.getList();
          for (int j=0; j < list.size(); j++){
              //         
              if (list.get(j).getSelected() == 1){
                  //           
                  int num = list.get(j).getNum();
                  totalNumber += num;
              }
          }
      }
      return totalNumber;
  }
  //B      
  public float goodsTotalPrice(){
      float totalPrice = 0;
      for (int x=0; x < mDataBeans.size(); x++){
          CartInfo.DataBean dataBean = mDataBeans.get(x);
          List list = dataBean.getList();
          for (int j=0; j < list.size(); j++){
              //         
              if (list.get(j).getSelected() == 1){
                  //           
                  float price = list.get(j).getPrice();
                  //           
                  int num = list.get(j).getNum();
                  //    *           
                  totalPrice += price * num;
              }
          }
      }
      return totalPrice;
  }


  /////////////////////////////////////C.       ,         ///////////////////////////////////
  //C.           ,         
  public void changeParentGoods(int groupPosition, boolean isSelected){
      CartInfo.DataBean dataBean = mDataBeans.get(groupPosition);
      List list = dataBean.getList();
      for (int x=0; x < list.size(); x++){
          CartInfo.DataBean.ListBean listBean = list.get(x);
          listBean.setSelected(isSelected ? 1 : 0);
      }
  }
  //C.              ,     
  public void changeChildGoods(int groupPosition, int childPosition){
      CartInfo.DataBean dataBean = mDataBeans.get(groupPosition);
      List list = dataBean.getList();
      CartInfo.DataBean.ListBean listBean = list.get(childPosition);
      listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
  }

  //C.         
  public void changeAllGoods(boolean selected){
      for (int x=0; x < mDataBeans.size(); x++){
          CartInfo.DataBean dataBean = mDataBeans.get(x);
          List list = dataBean.getList();
          for (int j=0; j < list.size(); j++){
              list.get(j).setSelected(selected ? 1 : 0);
          }
      }
  }

  //C.        ,  ,            .   1      ,   2      ,   3            
  public void changeAddSubNumber(int groupPosition , int childPosition, int number){
      CartInfo.DataBean dataBean = mDataBeans.get(groupPosition);
      List list = dataBean.getList();
      CartInfo.DataBean.ListBean listBean = list.get(childPosition);
      listBean.setNum(number);
  }



  ////////////////////////////////////////////D.         //////////////////////////////////////////
  //D.
  public interface onCartListener{
      /**
       *     Checkbox     
       */
      void onParentCheckboxChange(int groupPosition);

      /**
       *          Checkbox   
       */
      void onChildCheckboxChange(int groupPosition, int childPosition);

      /**
       *           
       */
      void onAddSubNumberChange(int groupPosition , int childPosition, int number);
  }
  onCartListener mOnCartListener;
  public void setOnCartListener(onCartListener onCartListener){
      this.mOnCartListener = onCartListener;
  }

  //////////////////////////////BaseExpandableListAdapter     ,         ///////////////////////////
  @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 false;
  }


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


}


ここでは、ショッピングカートのUI効果を実現するために、第1編と第3編のリンクもお送りします.https://blog.csdn.net/LZ0419/article/details/84317572**第三篇:ショッピングカートの機能をさらに充実させ、全選択ボタンのクリックによって、すべての商品のチェックボックスの状態を変更し、総数量と価格を更新し、アダプタに対してリスニング、リスニングプラスマイナスボタン、商店のチェックボックス、サブエントリのチェックボックスの変更を設定する.https://blog.csdn.net/LZ0419/article/details/84328334 **