本地购物车实现
數據bean類格式
package moyuchen.com.shopcartspractise;import java.util.List;public class ShopCartBean {/*** shopId : 2* shopName : null* cartlist : [{"id":2,"shopId":2,"shopName":null,"productId":2,"productName":null,"color":null,"size":null,"price":null,"count":null}]*/private int shopId;private Object shopName;/*** id : 2* shopId : 2* shopName : null* productId : 2* productName : null* color : null* size : null* price : null* count : null*/private List<CartlistBean> cartlist;public int getShopId() {return shopId;}public void setShopId(int shopId) {this.shopId = shopId;}public Object getShopName() {return shopName;}public void setShopName(Object shopName) {this.shopName = shopName;}public List<CartlistBean> getCartlist() {return cartlist;}public void setCartlist(List<CartlistBean> cartlist) {this.cartlist = cartlist;}public static class CartlistBean {private int id;private int shopId;private String shopName;private int productId;private String productName;private String color;private String size;private String price;private String defaultPic;private int count;private boolean isSelect = true;private int isFirst = 2;private boolean isShopSelect = true;public String getDefaultPic() {return defaultPic;}public void setDefaultPic(String defaultPic) {this.defaultPic = defaultPic;}public boolean getIsShopSelect() {return isShopSelect;}public void setShopSelect(boolean shopSelect) {isShopSelect = shopSelect;}public int getIsFirst() {return isFirst;}public void setIsFirst(int isFirst) {this.isFirst = isFirst;}public boolean getIsSelect() {return isSelect;}public void setSelect(boolean select) {isSelect = select;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getShopId() {return shopId;}public void setShopId(int shopId) {this.shopId = shopId;}public String getShopName() {return shopName;}public void setShopName(String shopName) {this.shopName = shopName;}public int getProductId() {return productId;}public void setProductId(int productId) {this.productId = productId;}public String getProductName() {return productName;}public void setProductName(String productName) {this.productName = productName;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String getSize() {return size;}public void setSize(String size) {this.size = size;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}} }RecycleView適配器
package moyuchen.com.shopcartspractise;import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView;import com.bumptech.glide.Glide;import java.util.List;/*** Created by Administrator on 2016/10/14.*/public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartAdapter.MyViewHolder> {private Context context;private List<ShopCartBean.CartlistBean> data;private View headerView;private OnDeleteClickListener mOnDeleteClickListener;private OnEditClickListener mOnEditClickListener;private OnResfreshListener mOnResfreshListener;public ShopCartAdapter(Context context, List<ShopCartBean.CartlistBean> data){this.context = context;this.data = data;}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view;view = LayoutInflater.from(context).inflate(R.layout.item_shopcart, parent, false);return new MyViewHolder(view);}@Overridepublic void onBindViewHolder(final MyViewHolder holder, final int position) {Glide.with(context).load(data.get(position).getDefaultPic()).into(holder.ivShopCartClothPic);if (position > 0) {if (data.get(position).getShopId() == data.get(position - 1).getShopId()) {holder.llShopCartHeader.setVisibility(View.GONE);} else {holder.llShopCartHeader.setVisibility(View.VISIBLE);}}else {holder.llShopCartHeader.setVisibility(View.VISIBLE);}holder.tvShopCartClothColor.setText("顏色:" + data.get(position).getColor());holder.tvShopCartClothSize.setText("尺寸:" + data.get(position).getSize());holder.tvShopCartClothName.setText(data.get(position).getProductName());holder.tvShopCartShopName.setText(data.get(position).getShopName());holder.tvShopCartClothPrice.setText("¥" + data.get(position).getPrice());holder.etShopCartClothNum.setText(data.get(position).getCount() + "");if(mOnResfreshListener != null){boolean isSelect = false;for(int i = 0;i < data.size(); i++){if(!data.get(i).getIsSelect()){isSelect = false;break;}else{isSelect = true;}}mOnResfreshListener.onResfresh(isSelect);}holder.ivShopCartClothMinus.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(data.get(position).getCount() > 1) {int count = data.get(position).getCount() - 1;if (mOnEditClickListener != null) {mOnEditClickListener.onEditClick(position, data.get(position).getId(), count);}data.get(position).setCount(count);notifyDataSetChanged();}}});holder.ivShopCartClothAdd.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int count = data.get(position).getCount() + 1;if(mOnEditClickListener != null){mOnEditClickListener.onEditClick(position,data.get(position).getId(),count);}data.get(position).setCount(count);notifyDataSetChanged();}});if(data.get(position).getIsSelect()){holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));}else {holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));}if(data.get(position).getIsShopSelect()){holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));}else {holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));}holder.ivShopCartClothDelete.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {showDialog(v,position);}});holder.ivShopCartClothSel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {data.get(position).setSelect(!data.get(position).getIsSelect());//通過循環找出不同商鋪的第一個商品的位置for(int i = 0;i < data.size(); i++){if(data.get(i).getIsFirst() == 1) {//遍歷去找出同一家商鋪的所有商品的勾選情況for(int j = 0;j < data.size();j++){//如果是同一家商鋪的商品,并且其中一個商品是未選中,那么商鋪的全選勾選取消if(data.get(j).getShopId() == data.get(i).getShopId() && !data.get(j).getIsSelect()){data.get(i).setShopSelect(false);break;}else{//如果是同一家商鋪的商品,并且所有商品是選中,那么商鋪的選中全選勾選data.get(i).setShopSelect(true);}}}}notifyDataSetChanged();}});holder.ivShopCartShopSel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(data.get(position).getIsFirst() == 1) {data.get(position).setShopSelect(!data.get(position).getIsShopSelect());for(int i = 0;i < data.size();i++){if(data.get(i).getShopId() == data.get(position).getShopId()){data.get(i).setSelect(data.get(position).getIsShopSelect());}}notifyDataSetChanged();}}});}private void showDialog(final View view, final int position){//調用刪除某個規格商品的接口if(mOnDeleteClickListener != null){mOnDeleteClickListener.onDeleteClick(view,position,data.get(position).getId());}data.remove(position);//重新排序,標記所有商品不同商鋪第一個的商品位置MainActivity.isSelectFirst(data);notifyDataSetChanged();}@Overridepublic int getItemCount() {int count = (data == null ? 0 : data.size());if(headerView != null){count++;}return count;}class MyViewHolder extends RecyclerView.ViewHolder{private ImageView ivShopCartShopSel;private TextView tvShopCartShopName;private TextView tvShopCartClothName;private TextView tvShopCartClothPrice;private TextView etShopCartClothNum;private TextView tvShopCartClothColor;private TextView tvShopCartClothSize;private ImageView ivShopCartClothSel;private ImageView ivShopCartClothMinus;private ImageView ivShopCartClothAdd;private ImageView ivShopCartClothDelete;private ImageView ivShopCartClothPic;private LinearLayout llShopCartHeader;public MyViewHolder(View view){super(view);llShopCartHeader = (LinearLayout) view.findViewById(R.id.ll_shopcart_header);ivShopCartShopSel = (ImageView) view.findViewById(R.id.iv_item_shopcart_shopselect);tvShopCartShopName = (TextView) view.findViewById(R.id.tv_item_shopcart_shopname);tvShopCartClothName = (TextView) view.findViewById(R.id.tv_item_shopcart_clothname);tvShopCartClothPrice = (TextView) view.findViewById(R.id.tv_item_shopcart_cloth_price);etShopCartClothNum = (TextView) view.findViewById(R.id.et_item_shopcart_cloth_num);tvShopCartClothColor = (TextView) view.findViewById(R.id.tv_item_shopcart_cloth_color);tvShopCartClothSize = (TextView) view.findViewById(R.id.tv_item_shopcart_cloth_size);ivShopCartClothSel = (ImageView) view.findViewById(R.id.tv_item_shopcart_clothselect);ivShopCartClothMinus = (ImageView) view.findViewById(R.id.iv_item_shopcart_cloth_minus);ivShopCartClothAdd = (ImageView) view.findViewById(R.id.iv_item_shopcart_cloth_add);ivShopCartClothPic = (ImageView) view.findViewById(R.id.iv_item_shopcart_cloth_pic);ivShopCartClothDelete = (ImageView) view.findViewById(R.id.iv_item_shopcart_cloth_delete);}}public View getHeaderView(){return headerView;}private OnItemClickListener mOnItemClickListener;public interface OnItemClickListener{void onItemClick(View view, int position);}public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){this.mOnItemClickListener = mOnItemClickListener;}public interface OnDeleteClickListener{void onDeleteClick(View view, int position, int cartid);}public void setOnDeleteClickListener(OnDeleteClickListener mOnDeleteClickListener){this.mOnDeleteClickListener = mOnDeleteClickListener;}public interface OnEditClickListener{void onEditClick(int position, int cartid, int count);}public void setOnEditClickListener(OnEditClickListener mOnEditClickListener){this.mOnEditClickListener = mOnEditClickListener;}public interface OnResfreshListener{void onResfresh(boolean isSelect);}public void setResfreshListener(OnResfreshListener mOnResfreshListener){this.mOnResfreshListener = mOnResfreshListener;}}MainActivity類的代碼
條目布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/main_white_text"><LinearLayoutandroid:id="@+id/ll_shopcart_header"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Viewandroid:layout_width="match_parent"android:layout_height="@dimen/margin_10dp"android:background="@color/background_color"android:id="@+id/view"></View><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"><ImageViewandroid:id="@+id/iv_item_shopcart_shopselect"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/shopcart_selected"android:paddingLeft="@dimen/margin_15dp"android:paddingRight="@dimen/margin_15dp"android:paddingTop="@dimen/margin_10dp"android:paddingBottom="@dimen/margin_10dp"/><TextViewandroid:id="@+id/tv_item_shopcart_shopname"android:layout_width="match_parent"android:layout_height="wrap_content"android:drawableLeft="@drawable/shopcart_shop"android:text="寶兒家服裝"android:padding="@dimen/padding_10dp"android:drawablePadding="@dimen/padding_5dp"/></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Viewandroid:layout_width="match_parent"android:layout_height="@dimen/margin_1dp"android:background="@color/background_color"></View><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/shopcart_selected"android:layout_marginLeft="@dimen/margin_15dp"android:layout_marginRight="@dimen/margin_15dp"android:visibility="invisible"/><TextViewandroid:id="@+id/tv_item_shopcart_clothname"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="穿秋裝情侶字母徽章風衣"android:paddingLeft="@dimen/padding_10dp"android:paddingTop="@dimen/padding_10dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center_vertical"><ImageViewandroid:id="@+id/tv_item_shopcart_clothselect"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/shopcart_selected"android:padding="@dimen/margin_15dp"/><ImageViewandroid:id="@+id/iv_item_shopcart_cloth_pic"android:layout_width="60dp"android:layout_height="60dp"android:layout_margin="@dimen/margin_10dp"/><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/tv_item_shopcart_cloth_price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="¥185"android:textColor="@color/main_red_text"android:textSize="@dimen/common_font_size_14"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="@dimen/margin_5dp"android:layout_marginBottom="@dimen/margin_5dp"><TextViewandroid:id="@+id/tv_item_shopcart_cloth_color"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="顏色:黑色"android:textSize="@dimen/common_font_size_12"/><TextViewandroid:id="@+id/tv_item_shopcart_cloth_size"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="尺寸:XL"android:textSize="@dimen/common_font_size_12"android:layout_marginLeft="@dimen/margin_10dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"><ImageViewandroid:id="@+id/iv_item_shopcart_cloth_minus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/shopcart_minus_grey"/><TextViewandroid:id="@+id/et_item_shopcart_cloth_num"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingTop="@dimen/padding_2dp"android:paddingBottom="@dimen/padding_2dp"android:paddingLeft="@dimen/padding_20dp"android:paddingRight="@dimen/padding_20dp"android:background="@drawable/shopcart_add_btn"android:layout_marginLeft="@dimen/margin_5dp"android:text="1"/><ImageViewandroid:id="@+id/iv_item_shopcart_cloth_add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/shopcart_add_red"android:layout_marginLeft="@dimen/margin_5dp"/></LinearLayout></LinearLayout><Viewandroid:layout_width="@dimen/margin_1dp"android:layout_height="match_parent"android:layout_marginTop="@dimen/padding_10dp"android:layout_marginBottom="@dimen/padding_10dp"android:background="@color/splitline_color"></View><ImageViewandroid:id="@+id/iv_item_shopcart_cloth_delete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="@dimen/margin_20dp"android:src="@drawable/shopcart_delete"/></LinearLayout></LinearLayout></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="@dimen/margin_1dp"android:background="@color/background_color"></View></LinearLayout>MainActivity類中布局文件<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayoutandroid:id="@+id/rl_shopcart_have"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><android.support.v7.widget.RecyclerViewandroid:id="@+id/rlv_shopcart"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></android.support.v7.widget.RecyclerView><Viewandroid:layout_width="match_parent"android:layout_height="50dp"></View></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:background="@color/main_white_text"android:gravity="center_vertical"android:id="@+id/ll_pay"android:layout_above="@+id/emtryview"><TextViewandroid:id="@+id/tv_shopcart_addselect"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="@dimen/margin_10dp"android:drawableLeft="@drawable/shopcart_selected"android:text="全選"android:drawablePadding="@dimen/padding_5dp"/><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/tv_shopcart_totalprice"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="@dimen/padding_10dp"android:paddingTop="@dimen/padding_10dp"android:text="總價:¥0"android:textColor="@color/main_red_text"android:textSize="@dimen/common_font_size_16"/><TextViewandroid:id="@+id/tv_shopcart_totalnum"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="@dimen/padding_10dp"android:text="共0件商品"android:textSize="@dimen/common_font_size_14"android:paddingBottom="@dimen/padding_10dp"/></LinearLayout><TextViewandroid:id="@+id/tv_shopcart_submit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/login_btn"android:text="去結算"android:paddingLeft="@dimen/margin_30dp"android:paddingRight="@dimen/margin_30dp"android:paddingTop="@dimen/padding_10dp"android:paddingBottom="@dimen/padding_10dp"android:textColor="@color/main_white_text"android:layout_marginRight="@dimen/margin_10dp"/></LinearLayout><Viewandroid:id="@+id/emtryview"android:layout_width="match_parent"android:layout_alignParentBottom="true"android:layout_height="50dp"></View></RelativeLayout></LinearLayout>總結
- 上一篇: RTOS系列文章(2):PendSV功能
- 下一篇: java后台关联微信公众号开发