Android实现购物车整体页面逻辑详解-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Android实现购物车整体页面逻辑详解

本文为大家讲解了Android实现购物车的整体页面逻辑,供大家参考,具体内容如下

公司主营业务:成都网站设计、成都做网站、外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出大邑县免费做网站回馈大家。

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  String url = "http://www.zhaoapi.cn/product/getCarts";
  private ExpandableListView el_cart;
  private CheckBox cb_cart_all_select;
  private TextView tv_cart_total_price;
  private Button btn_cart_pay;
  MyAdapter adapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();
    initData();
  }

  private void initData() {

    HashMap map = new HashMap<>();
    map.put("uid","71");
    OkhtttpUtils.getInstance().doPost(url, map, new OkhtttpUtils.OkCallback() {
      @Override
      public void onFailure(Exception e) {

      }

      @Override
      public void onResponse(String json) {

        CartInfo cartInfo = new Gson().fromJson(json, CartInfo.class);
        if ("0".equals(cartInfo.getCode())){
          List data = cartInfo.getData();
          adapter = new MyAdapter(data);
          el_cart.setAdapter(adapter);

          //展开二级列表
          for(int x=0; x

MyAdapter.java

public class MyAdapter extends BaseExpandableListAdapter{

  private List list;
  public MyAdapter(List data) {
    list=data;
  }

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

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

  @Override
  public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {

    //先拿到Bean里组的数据,看hiJson
    CartInfo.DataBean dataBean = list.get(i);
    ParentViewHolder parentViewHolder;
    if (view == null) {
      view = View.inflate(viewGroup.getContext(), R.layout.item_cart_parent, null);
      parentViewHolder = new ParentViewHolder(view);
      view.setTag(parentViewHolder);
    } else {
      parentViewHolder = (ParentViewHolder) view.getTag();
    }
    parentViewHolder.seller_name_tv.setText(dataBean.getSellerName());

    boolean currentSellerAllProductSelected = isCurrentSellerAllProductSelected(i);
    parentViewHolder.seller_cb.setChecked(currentSellerAllProductSelected);

    //D.设置点击CheckBox
    parentViewHolder.seller_cb.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mOnCartListChangeListener !=null){
          mOnCartListChangeListener.onSellerCheckedChange(i);
        }
      }
    });
    return view;
  }


  @Override
  public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {

    CartInfo.DataBean dataBean = list.get(i);
    List list1 = dataBean.getList();
    CartInfo.DataBean.ListBean listBean = list1.get(i1);
    ChildViewHolder childViewHolder;


    if (view == null) {
      view = View.inflate(viewGroup.getContext(), R.layout.item_cart_child, null);
      childViewHolder = new ChildViewHolder(view);
      view.setTag(childViewHolder);
    } else {
      childViewHolder=(ChildViewHolder)view.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.设置商品CheckBox的点击事件,通过接口回调,暴露给外面
    childViewHolder.child_cb.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mOnCartListChangeListener != null){
          mOnCartListChangeListener.onProductCheckedChange(i,i1);
        }
      }
    });

    //D.设置商品数量的点击事件,通过接口回调,暴露给外面
    childViewHolder.add_remove_view.setOnNumberChangeListener(new AddSubView.OnNumberChangeListener() {
      @Override
      public void onNumberChange(int num) {
        if (mOnCartListChangeListener !=null){
          mOnCartListChangeListener.onProducNumberChange(i,i1,num);
        }
      }
    });
    return view;
  }

  public boolean isCurrentSellerAllProductSelected(int i){//根据商品改变商家--如果商品全部选中商家则选中--有一个商品没选中则商家不选中
    CartInfo.DataBean dataBean = list.get(i);
    List beans = dataBean.getList();
    for (CartInfo.DataBean.ListBean bean : beans){

      if (bean.getSelected()==0){
        return false;
      }

    }
        return true;
  }

  public boolean isAllProductsSelected(){ //根据商品改变全选--如果商品全部选中全选则选中--有一个商品没选中则全选不选中
    for (int x=0;x list1 = dataBean.getList();
      for (int j=0;j list1 = dataBean.getList();
      for (int j=0;j list = dataBean.getList();
      for (int j=0;j beans = dataBean.getList();
    for (int j=0;j list = dataBean.getList();
    CartInfo.DataBean.ListBean listBean = list.get(i1);
    listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0 );

  }
  //C.设置所有商品的状态
  public void changeAllProductStatus(boolean selected){
    for(int x=0; x list = dataBean.getList();
      for(int j=0; j list = dataBean.getList();
    CartInfo.DataBean.ListBean listBean = list.get(i1);
    listBean.setNum(number);
  }

  public interface onCartListChangeListener{
    /**
     * 当商家的checkBox点击时回调
     */
    void onSellerCheckedChange(int i);

    /**
     * 当点击子条目商品的CheckBox回调
     */
    void onProductCheckedChange(int i ,int i1);

    /**
     * 当点击加减按钮的回调
     */
    void onProducNumberChange(int i , int i1 , int number);

  }
  //D.
  onCartListChangeListener mOnCartListChangeListener;
  //D.
  public void setOnCartListChangeListener(onCartListChangeListener onCartListChangeListener){
    mOnCartListChangeListener = onCartListChangeListener ;
  }

  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);
    }
  }

  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 AddSubView 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 = (AddSubView) rootView.findViewById(R.id.add_remove_view);
    }
  }

  @Override
  public Object getGroup(int i) {
    return null;
  }

  @Override
  public Object getChild(int i, int i1) {
    return null;
  }

  @Override
  public long getGroupId(int i) {
    return 0;
  }

  @Override
  public long getChildId(int i, int i1) {
    return 0;
  }

  @Override
  public boolean hasStableIds() {
    return false;
  }


  @Override
  public boolean isChildSelectable(int i, int i1) {
    return false;
  }
}

activity_main.xml



  

  

    

    

    

    

AddSubView.java

public class AddSubView extends LinearLayout implements View.OnClickListener{  //组合式控件
  private int number = 1;
  private TextView sub_tv;
  private TextView product_number_tv;
  private TextView add_tv;

  public AddSubView(Context context) {
    this(context,null);
  }

  public AddSubView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs,0);
  }

  public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    View view = inflate(context, R.layout.add_remove, this);

    sub_tv=view.findViewById(R.id.sub_tv);
    product_number_tv=view.findViewById(R.id.product_number_tv);
    add_tv=view.findViewById(R.id.add_tv);


    sub_tv.setOnClickListener(this);
    add_tv.setOnClickListener(this);
  }

  @Override
  public void onClick(View view) {
    switch (view.getId()){

      case R.id.sub_tv:

        if (number>1){
          --number;
          product_number_tv.setText(number+"");
          if (onNumberChangeListener!=null){
            onNumberChangeListener.onNumberChange(number);
          }
        }else {
          Toast.makeText(getContext(), "不能再少了", Toast.LENGTH_SHORT).show();
        }
        break;

      case R.id.add_tv:

        if (number<8){
          ++number;
          product_number_tv.setText(number+"");
          if (onNumberChangeListener!=null){
            onNumberChangeListener.onNumberChange(number);
          }
        }else {
          Toast.makeText(getContext(), "不能再多了", Toast.LENGTH_SHORT).show();
        }
        break;
    }
  }

  public int getNumber() {
    return number;
  }

  public void setNumber(int number) {
    this.number = number;
    product_number_tv.setText(number + "");
  }

  OnNumberChangeListener onNumberChangeListener;

  public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
    this.onNumberChangeListener = onNumberChangeListener;
  }

  interface OnNumberChangeListener {
    void onNumberChange(int num);
  }
}

add_remove.xml



  

  

  

item_cart_parent.xml




  

  

item_cart_child.xml



  

  

  

    

    
  

  

OkHttpUtils.java

public class OkhtttpUtils {
  private static OkhtttpUtils mOkhtttpUtils;
  private OkHttpClient mOkHttpClien;
  private final Handler mHandler;

  private OkhtttpUtils() {

    //创建一个主线程的handler
    mHandler = new Handler(Looper.getMainLooper());
    mOkHttpClien = new OkHttpClient.Builder()
        .connectTimeout(5000, TimeUnit.MILLISECONDS)
        .readTimeout(5000, TimeUnit.MILLISECONDS)
        .writeTimeout(5000, TimeUnit.MILLISECONDS)
        .build();
  }

  //单例模式
  public static OkhtttpUtils getInstance() {
    if (mOkhtttpUtils == null) {
      synchronized (OkhtttpUtils.class) {
        if (mOkhtttpUtils == null) {
          return mOkhtttpUtils = new OkhtttpUtils();
        }
      }
    }
    return mOkhtttpUtils;
  }

  public interface OkCallback {
    void onFailure(Exception e);
    void onResponse(String json);
  }


  public void doPost(String url, Map map, final OkCallback okCallback) {
    //创建FormBody的对象,把表单添加到formBody中
    FormBody.Builder builder = new FormBody.Builder();
    if (map != null) {
      for (String key : map.keySet()) {
        builder.add(key, map.get(key));
      }
    }
    FormBody formBody = builder.build();

    //创建Request对象
    Request request = new Request.Builder()
        .post(formBody)
        .url(url)
        .build();
    //创建Call对象
    final Call call = mOkHttpClien.newCall(request);
    call.enqueue(new Callback() {
      @Override
      public void onFailure(Call call, final IOException e) {
        if (okCallback != null) {
          //切换到主线程
          mHandler.post(new Runnable() {
            @Override
            public void run() {
              okCallback.onFailure(e);
            }
          });
        }
      }
      @Override
      public void onResponse(Call call, final Response response) throws IOException {
        try {
          if (response != null && response.isSuccessful()) {
            final String json = response.body().string();
            mHandler.post(new Runnable() {
              @Override
              public void run() {
                if (okCallback != null) {
                  okCallback.onResponse(json);
                  return;
                }
              }
            });
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (okCallback != null) {
          okCallback.onFailure(new Exception("网络异常"));
        }
      }
    });
  }




  //封装doGet的网络请求
  public void doGet(String url, final OkCallback okCallback) {
    Request request = new Request.Builder()
        .get()
        .url(url)
        .build();

    final Call call = mOkHttpClien.newCall(request);
    call.enqueue(new Callback() {
      @Override
      public void onFailure(Call call, final IOException e) {
        if (okCallback != null) {
          //切换到主线程
          mHandler.post(new Runnable() {
            @Override
            public void run() {
              okCallback.onFailure(e);
            }
          });
        }
      }

      @Override
      public void onResponse(Call call, final Response response) throws IOException {

        try {
          if (response != null && response.isSuccessful()) {
            final String json = response.body().string();
            mHandler.post(new Runnable() {
              @Override
              public void run() {
                if (okCallback != null) {
                  okCallback.onResponse(json);
                  return;
                }

              }
            });
          }
        } catch (IOException e) {
          e.printStackTrace();
        }

      }
    });
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网站名称:Android实现购物车整体页面逻辑详解
路径分享:http://kswsj.cn/article/gooipp.html

其他资讯