ShoppingMallApp03

2018-10-03  本文已影响10人  GeekGray

阅读原文

商品详情页面的数据传递和接收

创建数据传递 Bean 对象

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/8/26 8:59
 * @function:商品对象
 *
 */
public class GoodsBean implements Serializable
{
    //价格
    private String cover_price;
    //图片
    private String figure;
    //名称
    private String name;
    //产品id
    private String product_id;

    private int number = 1;

    /**
     * 是否被选中
     */
    private boolean isSelected = true;

    public boolean isSelected()
    {
        return isSelected;
    }

    public void setSelected(boolean selected)
    {
        isSelected = selected;
    }

    public int getNumber()
    {
        return number;
    }

    public void setNumber(int number)
    {
        this.number = number;
    }

    public String getCover_price()
    {
        return cover_price;
    }

    public void setCover_price(String cover_price)
    {
        this.cover_price = cover_price;
    }

    public String getFigure()
    {
        return figure;
    }

    public void setFigure(String figure)
    {
        this.figure = figure;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getProduct_id()
    {
        return product_id;
    }

    public void setProduct_id(String product_id)
    {
        this.product_id = product_id;
    }

    @Override
    public String toString()
    {
        return "GoodsBean{" +
                "cover_price='" + cover_price + '\'' +
                ", figure='" + figure + '\'' +
                ", name='" + name + '\'' +
                ", product_id='" + product_id + '\'' +
                ", number=" + number +
                ", isSelected=" + isSelected +
                '}';
    }
}

传递代码

                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean.setCover_price(listBean.getCover_price());
                    goodsBean.setFigure(listBean.getFigure());
                    goodsBean.setName(listBean.getName());
                    goodsBean.setProduct_id(listBean.getProduct_id());
                    startGoodsInfoActivity(goodsBean);

接收代码

        //接收数据
        goodsBean = (GoodsBean) getIntent().getSerializableExtra("goodsBean");
        if (goodsBean != null)
        {
            setDataForView(goodsBean);
        }

设置文本和图片数据

    /**
     * 设置数据
     *
     * @param goodsBean
     */
    private void setDataForView(GoodsBean goodsBean)
    {
        //设置图片
        //iv_good_info_image
        Glide.with(this)
                .load(Constants.BASE_URL_IMAGE + goodsBean.getFigure())
                .into(ivGoodInfoImage);

        //设置文本
        tvGoodInfoName.setText(goodsBean.getName());

        //设置价格
        tvGoodInfoPrice.setText("¥" + goodsBean.getCover_price());


        setWebViewData(goodsBean.getProduct_id());

    }

使用 WebView 加载设置网页数据

 private void setWebViewData(String product_id)
    {
        if (product_id != null)
        {
            wbGoodInfoMore.loadUrl("http://www.atguigu.com");

            WebSettings webSettings = wbGoodInfoMore.getSettings();
            //支持双击页面变大变小
            webSettings.setUseWideViewPort(true);
            //设置支持JavaScript
            webSettings.setJavaScriptEnabled(true);
            //优先使用缓存
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            wbGoodInfoMore.setWebViewClient(new WebViewClient()
            {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url)
                {
                    //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
                    view.loadUrl(url);
                    return true;
                }
            });
        }

    }

购物车

购物车存储器类

1.存储类CartStorage

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/8/30 16:04
 * @function:
 */
public class CartStorage
{
    public static final String JSON_CART = "json_cart";
    private static CartStorage instance;
    private final Context mContext;
    //SparseArray的性能优于HashMap
    private SparseArray<GoodsBean> sparseArray;

    private CartStorage(Context context)
    {
        this.mContext = context;
        //把之前存储的数据读取
        sparseArray = new SparseArray<>(100);

        listToSparseArray();
    }

    /**
     * 从本地读取的数据加入到SparseArray中
     */
    private void listToSparseArray()
    {
        List<GoodsBean> goodsBeanList = getAllData();
        //把List数据转换成SparseArray
        for (int i = 0; i < goodsBeanList.size(); i++)
        {
            GoodsBean goodsBean = goodsBeanList.get(i);
            sparseArray.put(Integer.parseInt(goodsBean.getProduct_id()), goodsBean);
        }
    }

    /**
     * 获取本地所有的数据
     *
     * @return
     */
    public List<GoodsBean> getAllData()
    {
        List<GoodsBean> goodsBeanList = new ArrayList<>();
        //1.从本地获取
        String json = CacheUtils.getString(mContext, JSON_CART);
        //2.使用Gson转换成列表
        //判断不为空的时候执行
        if (!TextUtils.isEmpty(json))
        {
            //把String转换成List
            goodsBeanList = new Gson().fromJson(json, new TypeToken<List<GoodsBean>>()
            {
            }.getType());
        }
        return goodsBeanList;
    }

    /**
     * 得到购车实例
     *
     * @return
     */
    public static CartStorage getInstance()
    {
        if (instance == null)
        {
            instance = new CartStorage(SmApplication.getContext());
        }
        return instance;
    }

    /**
     * 添加数据
     *
     * @param goodsBean
     */
    public void addData(GoodsBean goodsBean)
    {
        //1.添加到内存中SparseArray
        //如果当前数据已经存在,就修改成number递增
        GoodsBean tempData = sparseArray.get(Integer.parseInt(goodsBean.getProduct_id()));
        if (tempData != null)
        {
            //内存中有了这条数据
            tempData.setNumber(tempData.getNumber() + 1);
        }
        else
        {
            tempData = goodsBean;
            tempData.setNumber(1);
        }

        //同步到内存中
        sparseArray.put(Integer.parseInt(tempData.getProduct_id()), tempData);

        //2.同步到本地
        saveLocal();
    }

    /**
     * 删除数据
     *
     * @param goodsBean
     */
    public void deleteData(GoodsBean goodsBean)
    {
        //1.内存中删除
        sparseArray.delete(Integer.parseInt(goodsBean.getProduct_id()));

        //2.把内存的保持到本地
        saveLocal();
    }


    /**
     * 更新数据
     *
     * @param goodsBean
     */
    public void updateData(GoodsBean goodsBean)
    {
        //1.内存中更新
        sparseArray.put(Integer.parseInt(goodsBean.getProduct_id()), goodsBean);

        //2.同步到本地
        saveLocal();
    }

    /**
     * 保持数据到本地
     */
    private void saveLocal()
    {
        //1.SparseArray转换成List
        List<GoodsBean> goodsBeanList = sparseToList();
        //2.使用Gson把列表转换成String类型
        String json = new Gson().toJson(goodsBeanList);
        //3.把String数据保存
        CacheUtils.saveString(mContext, JSON_CART, json);
    }

    private List<GoodsBean> sparseToList()
    {
        List<GoodsBean> goodsBeanList = new ArrayList<>();
        for (int i = 0; i < sparseArray.size(); i++)
        {
            GoodsBean goodsBean = sparseArray.valueAt(i);
            goodsBeanList.add(goodsBean);
        }
        return goodsBeanList;
    }
}

2. 使用 Gson

compile 'com.google.code.gson:gson:2.2.4'

3. 缓存文本工具类

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/8/26 9:13
 * @function:缓存工具类
 */
public class CacheUtils
{
    /**
     * 得到保持的String类型的数据
     *
     * @param context
     * @param key
     * @return
     */
    public static String getString(Context context, String key)
    {
        SharedPreferences sp = context.getSharedPreferences("hashub", Context.MODE_PRIVATE);
        return sp.getString(key, "");
    }


    /**
     * 保持String类型数据
     *
     * @param context 上下文
     * @param key
     * @param value   保持的值
     */
    public static void saveString(Context context, String key, String value)
    {
        SharedPreferences sp = context.getSharedPreferences("hashub", Context.MODE_PRIVATE);
        sp.edit().putString(key, value).commit();
    }
}

4. 在 GoodsInfoActivity 添加商品

case R.id.btn_good_info_addcart:
                CartStorage.getInstance().addData(goodsBean);
                Toast.makeText(this, "添加到成功了", Toast.LENGTH_SHORT).show();
                break;

5. 加上在 sdcard 上的读写权限

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

购物车展示页面

1. 自定义增加删除按钮

布局文件 add_sub_layout.xml

<?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"
    android:gravity="center"
    android:orientation="horizontal">


    <ImageView
        android:id="@+id/iv_sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/goods_sub_btn" />

    <TextView
        android:textColor="@android:color/background_dark"
        android:id="@+id/tv_value"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="1"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/iv_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/goods_add_btn" />


</LinearLayout>

具体代码 AddSubView 类

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/8/30 16:22
 * @function:自定义删除增加按钮
 */
public class AddSubView extends LinearLayout implements View.OnClickListener
{
    private final Context mContext;
    private OnNumberChangeListener onNumberChangeListener;

    private ImageView ivSub;
    private TextView tvValue;
    private ImageView ivAdd;

    private int value = 1;
    private int minValue = 1;
    private int maxValue = 5;

    public AddSubView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.mContext = context;
        //把布局文件实例化,并且加载到AddSubView类中
        View.inflate(context, R.layout.add_sub_view, this);

        ivSub = (ImageView) findViewById(R.id.iv_sub);
        tvValue = (TextView) findViewById(R.id.tv_value);
        ivAdd = (ImageView) findViewById(R.id.iv_add);
        initData();
    }

    private void initData()
    {
        int value = getValue();
        setValue(value);

        //设置点击事件
        ivSub.setOnClickListener(this);
        ivAdd.setOnClickListener(this);
    }



    private void addNumebr()
    {
        if (value < maxValue)
        {
            value++;
        }
        setValue(value);

        if (onNumberChangeListener != null)
        {
            onNumberChangeListener.onNumberChange(value);
        }
    }

    /**
     * 减
     */
    private void subNumber()
    {
        if (value > minValue)
        {
            value--;
        }
        setValue(value);

        if (onNumberChangeListener != null)
        {
            onNumberChangeListener.onNumberChange(value);
        }

    }

    public int getValue()
    {
        String valueStr = tvValue.getText().toString().trim();
        if (!TextUtils.isEmpty(valueStr))
        {
            value = Integer.parseInt(valueStr);
        }
        return value;
    }

    public void setValue(int value)
    {
        this.value = value;
        tvValue.setText(value + "");
    }

    public int getMinValue()
    {
        return minValue;
    }

    public void setMinValue(int minValue)
    {
        this.minValue = minValue;
    }

    public int getMaxValue()
    {
        return maxValue;
    }

    public void setMaxValue(int maxValue)
    {
        this.maxValue = maxValue;
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.iv_sub://减
                subNumber();
                break;

            case R.id.iv_add://加
                addNumebr();
                break;
        }
    }

    /**
     * 当数量发生变化的时候回调
     */
    public interface OnNumberChangeListener
    {
        /**
         * 当数据发生变化的时候回调
         *
         * @param value
         */
        public void onNumberChange(int value);
    }

    /**
     * 设置数量变化的监听
     *
     * @param onNumberChangeListener
     */
    public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener)
    {
        this.onNumberChangeListener = onNumberChangeListener;
    }
}

2. 页面分析

最外边是 LinearLayout
1:相对布局
2:View 分割线
3:FrameLayou
1:LinearLayout
1:RecyclerView
2:LinearLayout(结算)
3:LinearLayout(删除)
2:购车显示布局 empty_cart.xml

3. 购物车展示页面布局文件fragment_shoppingcart.xml

<?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="match_parent"
    android:background="#fff"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#fff">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="30dp"
            android:layout_weight="1"
            android:text="购物车"
            android:textColor="#303235"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_shopcart_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="编辑"
            android:textColor="#303235" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#eeee" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--线性布局-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#eee" />

            <!--结算的线性布局-->

            <LinearLayout
                android:id="@+id/ll_check_all"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#fff"
                android:orientation="horizontal"
                android:visibility="visible">

                <CheckBox
                    android:id="@+id/checkbox_all"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:button="@null"
                    android:drawableLeft="@drawable/checkbox_selector"
                    android:drawablePadding="10dp"
                    android:padding="10dp"
                    android:paddingLeft="0dp"
                    android:text="全选"
                    android:textColor="#303235"
                    android:textSize="15sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="合计:"
                    android:textColor="#303235"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_shopcart_total"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:padding="10dp"
                    android:text="¥0.00"
                    android:textColor="#ed3f3f"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/btn_check_out"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#ed3f3f"
                    android:text="去结算"
                    android:textColor="#fff" />
            </LinearLayout>

            <!--删除的显示布局-->
            <LinearLayout
                android:id="@+id/ll_delete"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#fff"
                android:orientation="horizontal"
                android:padding="5dp"
                android:visibility="gone">

                <CheckBox
                    android:id="@+id/cb_all"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_weight="1"
                    android:button="@null"
                    android:drawableLeft="@drawable/checkbox_selector"
                    android:drawablePadding="10dp"
                    android:padding="10dp"
                    android:paddingLeft="0dp"
                    android:text="全选"
                    android:textColor="#303235"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/btn_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="35dp"
                    android:background="@drawable/words"
                    android:text="删除"
                    android:textColor="#303235"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/btn_collection"
                    android:layout_width="wrap_content"
                    android:layout_height="35dp"
                    android:layout_marginLeft="15dp"
                    android:background="@drawable/wordsred"
                    android:text="收藏"
                    android:textColor="#ed3f3f"
                    android:textSize="15sp" />
            </LinearLayout>


        </LinearLayout>

        <!--当数据为空的时候显示的布局-->
        <include layout="@layout/empty_cart"></include>
    </FrameLayout>

</LinearLayout>

4. 设置购物车内容为空的布局 empty_cart.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_empty_shopcart"
    android:layout_width="match_parent"
    android:background="#fff"
    android:visibility="gone"
    android:layout_height="match_parent">

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

        <ImageView
            android:id="@+id/iv_empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/empty_cart" />

        <TextView
            android:id="@+id/tv_empty_cart_tobuy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/iv_empty"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:background="@drawable/shape_rec_textview"
            android:paddingBottom="5dp"
            android:paddingLeft="25dp"
            android:paddingRight="25dp"
            android:paddingTop="5dp"
            android:text="去逛逛"
            android:textColor="#ed3f3f"
            android:textSize="15sp" />
    </RelativeLayout>
</LinearLayout>

5. item 布局 item_shop_cart.xml

<?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"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">

        <CheckBox
            android:id="@+id/cb_gov"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:button="@drawable/checkbox_selector"
            android:checked="true"
            android:clickable="false" />

        <ImageView
            android:id="@+id/iv_gov"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/icon_good_detail_cart"
            android:scaleType="fitXY" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_desc_gov"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:lines="2"
                android:text="华硕(ASUS)经典系列X554LP 15.6英寸笔记本 (i5-5200U 4G 500G R5-M230 1G独显 蓝牙 Win8.1 黑色)"
                android:textColor="#404040"
                android:textSize="16sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv_price_gov"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:text="¥0.0"
                    android:textColor="#f00"
                    android:textSize="16sp" />

                <com.example.shoppingmall.shoppingcart.view.AddSubView
                    android:id="@+id/numberAddSubView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>


    </LinearLayout>

    <View

        android:layout_width="match_parent"
        android:layout_height="0.6dp"
        android:background="#22000000" />

</LinearLayout>

2. ShoppingCartFragment 初始化布局

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/8/24 17:03
 * @function:购物车的Fragment
 */
public class ShoppingCartFragment extends BaseFragment implements View.OnClickListener
{
    //编辑状态
    private static final int ACTION_EDIT = 1;
    //完成状态
    private static final int ACTION_COMPLETE = 2;
    private ShoppingCartAdapter adapter;

    private TextView tvShopcartEdit;
    private RecyclerView recyclerview;
    private LinearLayout llCheckAll;
    private CheckBox checkboxAll;
    private TextView tvShopcartTotal;
    private Button btnCheckOut;
    private LinearLayout llDelete;
    private CheckBox cbAll;
    private Button btnDelete;
    private Button btnCollection;

    private ImageView ivEmpty;
    private TextView tvEmptyCartTobuy;
    private LinearLayout ll_empty_shopcart;

    @Override
    public View initView()
    {
        Log.e(TAG, "购物车的Fragment的UI被初始化了");

        View view = View.inflate(mContext, R.layout.fragment_shopping_cart, null);

        tvShopcartEdit = (TextView) view.findViewById(R.id.tv_shopcart_edit);
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        llCheckAll = (LinearLayout) view.findViewById(R.id.ll_check_all);
        checkboxAll = (CheckBox) view.findViewById(R.id.checkbox_all);
        tvShopcartTotal = (TextView) view.findViewById(R.id.tv_shopcart_total);
        btnCheckOut = (Button) view.findViewById(R.id.btn_check_out);
        llDelete = (LinearLayout) view.findViewById(R.id.ll_delete);
        cbAll = (CheckBox) view.findViewById(R.id.cb_all);
        btnDelete = (Button) view.findViewById(R.id.btn_delete);
        btnCollection = (Button) view.findViewById(R.id.btn_collection);

        ll_empty_shopcart = (LinearLayout) view.findViewById(R.id.ll_empty_shopcart);
        ivEmpty = (ImageView) view.findViewById(R.id.iv_empty);
        tvEmptyCartTobuy = (TextView) view.findViewById(R.id.tv_empty_cart_tobuy);

        btnCheckOut.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnCollection.setOnClickListener(this);
        initListener();

        return view;
    }

    private void initListener()
    {
        //设置默认的编辑状态
        tvShopcartEdit.setTag(ACTION_EDIT);
        tvShopcartEdit.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                int action = (int) v.getTag();
                if (action == ACTION_EDIT)
                {
                    //切换为完成状态
                    showDelete();
                }
                else
                {
                    //切换成编辑状态
                    hideDelete();
                }
            }
        });
    }

    private void hideDelete()
    {
        //1.设置状态和文本-编辑
        tvShopcartEdit.setTag(ACTION_EDIT);
        tvShopcartEdit.setText("编辑");
        //2.变成非勾选
        if (adapter != null)
        {
            adapter.checkAll_none(true);
            adapter.checkAll();
            adapter.showTotalPrice();
        }
        //3.删除视图显示
        llDelete.setVisibility(View.GONE);
        //4.结算视图隐藏
        llCheckAll.setVisibility(View.VISIBLE);
    }

    private void showDelete()
    {
        //1.设置状态和文本-完成
        tvShopcartEdit.setTag(ACTION_COMPLETE);
        tvShopcartEdit.setText("完成");
        //2.变成非勾选
        if (adapter != null)
        {
            adapter.checkAll_none(false);
            adapter.checkAll();
        }
        //3.删除视图显示
        llDelete.setVisibility(View.VISIBLE);
        //4.结算视图隐藏
        llCheckAll.setVisibility(View.GONE);
    }

    @Override
    public void initData()
    {
        super.initData();
        Log.e(TAG, "购物车的Fragment的数据被初始化了");
    }

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

    /**
     *
     */
    private void showData()
    {
        List<GoodsBean> goodsBeanList = CartStorage.getInstance().getAllData();

        if (goodsBeanList != null && goodsBeanList.size() > 0)
        {
            tvShopcartEdit.setVisibility(View.VISIBLE);
            llCheckAll.setVisibility(View.VISIBLE);
            //有数据
            //把当没有数据显示的布局-隐藏
            ll_empty_shopcart.setVisibility(View.GONE);

            //设置适配器
            adapter = new ShoppingCartAdapter(mContext, goodsBeanList, tvShopcartTotal, checkboxAll, cbAll);
            recyclerview.setAdapter(adapter);

            //设置布局管理器
            recyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));


        }
        else
        {
            //没有数据
            //显示数据为空的布局
            emptyShoppingCart();
        }
    }


    private void emptyShoppingCart()
    {
        ll_empty_shopcart.setVisibility(View.VISIBLE);
        tvShopcartEdit.setVisibility(View.GONE);
        llDelete.setVisibility(View.GONE);
    }


    @Override
    public void onClick(View v)
    {
        if (v == btnCheckOut)
        {
            // Handle clicks for btnCheckOut
            //结算
           // pay(v);
        }
        else if (v == btnDelete)
        {
            // Handle clicks for btnDelete
            //删除选中的
            adapter.deleteData();
            //校验状态
            adapter.checkAll();
            //数据大小为0
            if (adapter.getItemCount() == 0)
            {
                emptyShoppingCart();
            }

        }
        else if (v == btnCollection)
        {
            // Handle clicks for btnCollection
        }
    }
}

适配器 ShopCartAdapter

/**
 * @author: Hashub
 * @WeChat: NGSHMVP
 * @Date: 2018/9/1 8:51
 * @function:购物车适配器
 */
public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartAdapter.ViewHolder>
{

    private final Context mContext;
    private final List<GoodsBean> datas;
    private final TextView tvShopcartTotal;
    private final CheckBox checkboxAll;
    //完成状态下的删除CheckBox
    private final CheckBox cbAll;


    public ShoppingCartAdapter(Context mContext, List<GoodsBean> goodsBeanList, TextView tvShopcartTotal, CheckBox checkboxAll, CheckBox cbAll)
    {
        this.mContext = mContext;
        this.datas = goodsBeanList;
        this.tvShopcartTotal = tvShopcartTotal;
        this.checkboxAll = checkboxAll;
        this.cbAll = cbAll;
        showTotalPrice();
        //设置点击事件
        setListener();
        //校验是否全选
        checkAll();
    }

    private void setListener()
    {
        setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(int position)
            {
                //1.根据位置找到对应的Bean对象
                GoodsBean goodsBean = datas.get(position);
                //2.设置取反状态
                goodsBean.setSelected(!goodsBean.isSelected());
                //3.刷新状态
                notifyItemChanged(position);
                //4.校验是否全选
                checkAll();
                //5.重新计算总价格
                showTotalPrice();
            }
        });

        //CheckBox的点击事件
        checkboxAll.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                //1.得到状态
                boolean isCheck = checkboxAll.isChecked();

                //2.根据状态设置全选和非全选
                checkAll_none(isCheck);

                //3.计算总价格
                showTotalPrice();

            }
        });

        cbAll.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                //1.得到状态
                boolean isCheck = cbAll.isChecked();

                //2.根据状态设置全选和非全选
                checkAll_none(isCheck);


            }
        });

    }

    /**
     * 设置全选和非全选
     *
     * @param isCheck
     */
    public void checkAll_none(boolean isCheck)
    {
        if (datas != null && datas.size() > 0)
        {
            for (int i = 0; i < datas.size(); i++)
            {
                GoodsBean goodsBean = datas.get(i);
                goodsBean.setSelected(isCheck);
                notifyItemChanged(i);
            }
        }
    }

    public void checkAll()
    {
        if (datas != null && datas.size() > 0)
        {
            int number = 0;
            for (int i = 0; i < datas.size(); i++)
            {
                GoodsBean goodsBean = datas.get(i);
                if (!goodsBean.isSelected())
                {
                    //非全选
                    checkboxAll.setChecked(false);
                    cbAll.setChecked(false);
                }
                else
                {
                    //选中的
                    number++;
                }
            }

            if (number == datas.size())
            {
                //全选
                checkboxAll.setChecked(true);
                cbAll.setChecked(true);
            }
        }
        else
        {
            checkboxAll.setChecked(false);
            cbAll.setChecked(false);
        }
    }


    public void showTotalPrice()
    {
        tvShopcartTotal.setText("合计:" + getTotalPrice());
    }

    /**
     * 计算总价格
     *
     * @return
     */
    public double getTotalPrice()
    {
        double totalPrice = 0.0;
        if (datas != null && datas.size() > 0)
        {

            for (int i = 0; i < datas.size(); i++)
            {
                GoodsBean goodsBean = datas.get(i);
                if (goodsBean.isSelected())
                {

                    totalPrice = totalPrice + Double.valueOf(goodsBean.getNumber()) * Double.valueOf(goodsBean.getCover_price());
                }
            }
        }
        return totalPrice;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = View.inflate(mContext, R.layout.item_shop_cart, null);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position)
    {
        //1.根据位置得到对应的Bean对象
        final GoodsBean goodsBean = datas.get(position);
        //2.设置数据
        holder.cb_gov.setChecked(goodsBean.isSelected());
        Glide.with(mContext).load(Constants.BASE_URL_IMAGE + goodsBean.getFigure()).into(holder.iv_gov);
        holder.tv_desc_gov.setText(goodsBean.getName());
        holder.tv_price_gov.setText("¥" + goodsBean.getCover_price());
        holder.ddSubView.setValue(goodsBean.getNumber());
        holder.ddSubView.setMinValue(1);
        holder.ddSubView.setMaxValue(8);

        //设置商品数量的变化
        holder.ddSubView.setOnNumberChangeListener(new AddSubView.OnNumberChangeListener()
        {
            @Override
            public void onNumberChange(int value)
            {
                //1.当前列表内存中
                goodsBean.setNumber(value);
                //2本地要更新
                CartStorage.getInstance().updateData(goodsBean);
                //3.刷新适配器
                notifyItemChanged(position);
                //4.再次计算总价格
                showTotalPrice();

            }
        });

    }

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

    public void deleteData()
    {
        if (datas != null && datas.size() > 0)
        {
            for (int i = 0; i < datas.size(); i++)
            {
                //删除选中的
                GoodsBean goodsBean = datas.get(i);
                if (goodsBean.isSelected())
                {
                    //内存-把移除
                    datas.remove(goodsBean);
                    //保持到本地
                    CartStorage.getInstance().deleteData(goodsBean);
                    //刷新
                    notifyItemRemoved(i);
                    i--;
                }
            }
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder
    {
        private CheckBox cb_gov;
        private ImageView iv_gov;
        private TextView tv_desc_gov;
        private TextView tv_price_gov;
        private AddSubView ddSubView;

        public ViewHolder(View itemView)
        {
            super(itemView);
            cb_gov = (CheckBox) itemView.findViewById(R.id.cb_gov);
            iv_gov = (ImageView) itemView.findViewById(R.id.iv_gov);
            tv_desc_gov = (TextView) itemView.findViewById(R.id.tv_desc_gov);
            tv_price_gov = (TextView) itemView.findViewById(R.id.tv_price_gov);
            ddSubView = (AddSubView) itemView.findViewById(R.id.numberAddSubView);
            //设置item的点击事件
            itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    if (onItemClickListener != null)
                    {
                        onItemClickListener.onItemClick(getLayoutPosition());
                    }
                }
            });
        }
    }

    /**
     * 点击item的监听者
     */
    public interface OnItemClickListener
    {
        /**
         * 当点击某条的时候被回调
         *
         * @param position
         */
        public void onItemClick(int position);
    }

    private OnItemClickListener onItemClickListener;

    /**
     * 设置item的监听
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener)
    {
        this.onItemClickListener = onItemClickListener;
    }
}

集成支付宝

1. 常见的支付厂商简介

**支付宝**:阿里公司,支付宝使用比较多.
**微信**:腾讯公司,也是越来越多.
易付宝:易付宝是苏宁云商旗下的一家独立的第三方支付公司
易宝支付:易宝支付
财付通:腾讯公司
**银联**:不属于某一个公司
百度钱包:百度
支付宝钱包:阿里的.
快钱:
ping++:整合了很多的支付平台
万普广告平台

2. 申请签约支付宝移动支付

1.在支付宝网站注册企业账号
2.个人账号暂时不支持
3.申请签约支付宝移动支付

3. 运行支付宝 SDK 提供的示例程序

1.下载 sdk 地址

https://open.alipay.com/platform/home.htm

2.参照文档地址

https://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103563&docType=1

3.在签约管理这里找到 pidkey 为:

2088911876712776

4.签名机制

https://doc.open.alipay.com/doc2/detailtreeId=58&articleId=103242&docType=1

5.开发者将私钥保留,将公钥提交给支付宝网关,用于信息加密及解密

6.生成 key

4. 集成到自己的应用中

5. 支付整个流程

1. 手机客户端集成支付宝SDK,发起订单请求,服务器记录订单信息

2. 服务器集成支付宝服务SDK,响应订单请求

3. 请求成功后回来后,立刻携带相关订单信息向支付宝服务器发起支付请求

4. 支付成功或者失败都会记录到服务器上,且该过程有肯能会延迟

5. 服务器同时将支付成功或者失败的结果回调给客户端

6. 成功询问服务器是否支付成功
上一篇下一篇

猜你喜欢

热点阅读