카트(중)

**
제2편: 일부 카트 특유의 방법을 실현한다. 예를 들어 이 업체와 상품의 선택 여부를 판단하고 상품의 총 수량을 계산하며 상품의 총 가격을 계산한다. 그리고 이것은 사용자의 선택에 따라 업체나 상품의 상태를 바꾼다.
**
이 부분은 전편에 비해 업데이트되었습니다. 이 단계에서 저는 주로 어댑터에서 몇 가지 조작을 했기 때문에 여러분의 읽기 시간을 절약할 수 있습니다. 여기서 어댑터의 구체적인 조작 코드만 보여 드리겠습니다. 구체적인 조작은 다음과 같습니다. 1.카트 특유의 방법 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;
  }


}


여기서도 1편과 3편 링크를 보내드리도록 하겠습니다. 1편: 카트 UI 구현:https://blog.csdn.net/LZ0419/article/details/84317572**제3편: 쇼핑카트의 기능을 한층 더 보완하고 전체 선택 버튼의 클릭에 따라 모든 상품의 복선상자 상태를 바꾸며 전체 수량과 가격을 갱신한다. 어댑터에 대한 감청, 감청 가감 버튼, 업체의 복선상자, 하위 항목의 복선상자의 변경.https://blog.csdn.net/LZ0419/article/details/84328334 **

좋은 웹페이지 즐겨찾기