購物車

主佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.ShoppingcartFragment"
    android:background="#ffffff"
    >
  

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/Rl"
        android:layout_alignParentBottom="true"
        >
        
       <CheckBox
           android:id="@+id/Shop_Zong_CheckBox"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="全選"
           />
        <TextView
            android:id="@+id/Shop_Zong_Price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="合計:"
            android:layout_toRightOf="@+id/Shop_Zong_CheckBox"
            android:layout_marginLeft="@dimen/dp_20"

            />

        <Button
            android:id="@+id/Shop_Zong_Btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="去結算"
            android:layout_alignParentRight="true"
            />
    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/Shop_Recy"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/Rl"
        >

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

adapter佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:background="@drawable/head_view_stork"
        android:layout_marginTop="@dimen/dp_20"
        >


    <CheckBox
        android:id="@+id/Shop_CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"

        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        >


    <ImageView
        android:id="@+id/Shop_Image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="20dp"
        android:src="@mipmap/ic_launcher"
        android:scaleType="centerCrop"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_toRightOf="@+id/Shop_Image"
        android:layout_marginLeft="@dimen/dp_20"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Shop_Title"
            android:maxLines="2"
            android:text="商品標題"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Shop_Price"
            android:text="¥0.0"
            android:layout_marginTop="10dp"
            />

    </LinearLayout>

        <com.example.activity.myapplication.weight.JiaJian
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Jia_Jian_View"
            android:layout_alignParentBottom="true"
            >

        </com.example.activity.myapplication.weight.JiaJian>
    </RelativeLayout>

    </LinearLayout>




</LinearLayout>

加減佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

  >



    <TextView
        android:id="@+id/delete_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:gravity="center"
        android:text="-"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/product_number_tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:textSize="22sp"
        android:background="#ffffff"
        android:gravity="center"
        android:text="1" />

    <TextView
        android:id="@+id/add_tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"

        android:text="+"
        android:textSize="30sp" />


</LinearLayout>

main代碼

package com.example.activity.myapplication.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.activity.myapplication.DingDanActivity;
import com.example.activity.myapplication.R;
import com.example.activity.myapplication.adapter.MyShopAdapter;
import com.example.activity.myapplication.bean.ChaShoppData;
import com.example.activity.myapplication.bean.ShopData;
import com.example.activity.myapplication.presenter.PersenterIpml;
import com.example.activity.myapplication.utils.Contacts;
import com.example.activity.myapplication.view.IView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShoppingcartFragment extends Fragment implements View.OnClickListener,IView {


    private CheckBox Shop_Zong_CheckBox;
    private TextView Shop_Zong_Price;
    private Button Shop_Zong_Btn;
    private RecyclerView Shop_Recy;
    private PersenterIpml persenterIpml;
    private SharedPreferences sp;
    private ArrayList<ChaShoppData.ResultBean>cList=new ArrayList<>();
    private MyShopAdapter myShopAdapter;
    private HashMap<String, String> map;
    private HashMap<String, Object> hashMap;
    private boolean allGoods;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shoppingcart, null);
        initView(view);
        persenterIpml = new PersenterIpml(this);
        sp = getContext().getSharedPreferences("long",Context.MODE_PRIVATE);
        map = new HashMap<>();
        hashMap = new HashMap<>();
        int userId = sp.getInt("userId", 1);
        String sessionId = sp.getString("sessionId", "");
        hashMap.put("userId",userId);
        hashMap.put("sessionId",sessionId);
        myShopAdapter = new MyShopAdapter(cList,getContext());
        Shop_Recy.setAdapter(myShopAdapter);
        Shop_Recy.setLayoutManager(new LinearLayoutManager(getContext()));

        myShopAdapter.setCallback(new MyShopAdapter.AdapterCallback() {
            @Override
            public void shuaXin() {

                flushBottomLayout();

            }




        });


        return view;
    }

    private void initView(View view) {
        Shop_Zong_CheckBox = (CheckBox) view.findViewById(R.id.Shop_Zong_CheckBox);
        Shop_Zong_Price = (TextView) view.findViewById(R.id.Shop_Zong_Price);
        Shop_Zong_Btn = (Button) view.findViewById(R.id.Shop_Zong_Btn);
        Shop_Recy = (RecyclerView) view.findViewById(R.id.Shop_Recy);
        Shop_Zong_CheckBox.setOnClickListener(this);
        Shop_Zong_Btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Shop_Zong_Btn:

                if(findCheckedShop().size()==0){
                    Toast.makeText(getContext(),"請選擇再添加",Toast.LENGTH_SHORT).show();
                    return;
                }
                    Intent intent=new Intent(getContext(),DingDanActivity.class);
                ShopData shopData=new ShopData();
                shopData.setResult(findCheckedShop());
                intent.putExtra("checkedshop",shopData);
               startActivity(intent);






                break;
            case R.id.Shop_Zong_CheckBox:
                boolean allGoods = myShopAdapter.isAllGoods();
                myShopAdapter.setAllGoodsIsChecked(allGoods);
                myShopAdapter.notifyDataSetChanged();
                flushBottomLayout();
                break;
        }



    }

    private List<ChaShoppData.ResultBean> findCheckedShop() {
        List<ChaShoppData.ResultBean> list2=new ArrayList<>();
        for (int i = 0; i < cList.size(); i++) {
            if(cList.get(i).isChecked()){
                list2.add(cList.get(i));
            }
        }
        return list2;
    }
    @Override
    public void success(Object data) {
        ChaShoppData chaShoppData= (ChaShoppData) data;
        cList.clear();
        cList.addAll(chaShoppData.getResult());
        myShopAdapter.notifyDataSetChanged();

    }

    @Override
    public void error(Object error) {

    }

    @Override
    public void onResume() {
        super.onResume();

        persenterIpml.requestGEt(Contacts.CHASHOPPING_URL,map,hashMap,ChaShoppData.class);
    }


    private void flushBottomLayout(){

        float allGoodsPrice = myShopAdapter.getAllGoodsPrice();
        boolean allGoods = myShopAdapter.isAllGoods();
        Shop_Zong_CheckBox.setChecked(allGoods);
        Shop_Zong_Price.setText("合計:"+allGoodsPrice+"");
    }

}

適配器

package com.example.activity.myapplication.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;

import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.example.activity.myapplication.R;
import com.example.activity.myapplication.bean.ChaShoppData;
import com.example.activity.myapplication.weight.JiaJian;

import java.util.ArrayList;

public class MyShopAdapter extends RecyclerView.Adapter<MyShopAdapter.ViewHolder> {
    private ArrayList<ChaShoppData.ResultBean> cList;
    private Context mContext;

    public MyShopAdapter(ArrayList<ChaShoppData.ResultBean> cList, Context mContext) {
        this.cList = cList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=View.inflate(mContext,R.layout.shop_item,null);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int i) {
        Glide.with(mContext).load(cList.get(i).getPic()).into(holder.shop_image);
        holder.shop_title.setText(cList.get(i).getCommodityName());
        holder.shop_price.setText("¥"+cList.get(i).getPrice()+"");
        holder.jia_jian_view.setNumber(cList.get(i).getCount());
        holder.shop_checkBox.setChecked(cList.get(i).isChecked());
        holder.shop_checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cList.get(i).setChecked(isCbChecked(i));
                adapterCallback.shuaXin();
                notifyDataSetChanged();
            }
        });


        holder.jia_jian_view.setOnChange(new JiaJian.OnCountChange() {
            @Override
            public void setCount(int count) {
              if(adapterCallback!=null){
                  cList.get(i).setCount(count);
                  adapterCallback.shuaXin();
                  notifyDataSetChanged();
              }
            }
        });
    }


    public boolean isCbChecked(int position){
        boolean b=cList.get(position).isChecked();
        return !b;
    }




//價格賦值
    public float getAllGoodsPrice() {
        float allPrice = 0;
        for (int i = 0; i < cList.size(); i++) {
           if(cList.get(i).isChecked()){
               allPrice += cList.get(i).getCount() * Float.parseFloat(cList.get(i).getPrice()+"");
           }

        }

        return allPrice;
    }



    //因爲咱們底部視圖有個一全選和反選
    public boolean isAllGoods() {

        for (int i = 0; i < cList.size(); i++) {
                if (!cList.get(i).isChecked()) {
                   return false;
                }

            }
        return true;
    }

    //全選反選
    public void setAllGoodsIsChecked(boolean isChecked) {
        for (int i = 0; i < cList.size(); i++) {
            cList.get(i).setChecked(!isChecked);
        }
    }




    public interface AdapterCallback {

        void shuaXin();

    }

    private AdapterCallback adapterCallback;

    public void setCallback(AdapterCallback adapterCallback) {
        this.adapterCallback = adapterCallback;
    }

    @Override
    public int getItemCount() {
        return cList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private  CheckBox shop_checkBox;
        private  ImageView shop_image;
        private  TextView shop_title;
        private  TextView shop_price;
        private  JiaJian jia_jian_view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            shop_checkBox = itemView.findViewById(R.id.Shop_CheckBox);
            shop_image = itemView.findViewById(R.id.Shop_Image);
            shop_title = itemView.findViewById(R.id.Shop_Title);
            shop_price = itemView.findViewById(R.id.Shop_Price);
            jia_jian_view = itemView.findViewById(R.id.Jia_Jian_View);

        }
    }





}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章