在电商项目中购物车是必不可少的,也是有一定难度的。现在我们来写一些购物车有关的代码:
我们先看看购物车MainActivity的布局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:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp"
>
<RelativeLayout
android:id="@+id/rl_shopcart_have"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rlv_shopcart"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<View
android:layout_width="match_parent"
android:layout_height="50dp"></View>
</LinearLayout>
<LinearLayout
android: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">
<TextView
android: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" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tv_shopcart_totalprice"
android:layout_width="150dp"
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" />
<TextView
android: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>
<TextView
android: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>
<View
android:id="@+id/emtryview"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="50dp"></View>
</RelativeLayout>
</LinearLayout>
基本的xml,再看看colors中新增的颜色值:
<!--背景颜色-->
<color name="toolbar_color">#f8f8f8</color>
<color name="background_color">#f6f6f6</color>
<color name="edittext_noenter_color">#f5f5f5</color>
<!--辅助色-->
<color name="support_yellow">#f9a340</color>
<color name="btn_unselect_color">#f4736e</color>
<!--分割线-->
<color name="splitline_color">#dddddd</color>
<!--文字-->
<color name="notice_text_color">#999999</color>
<color name="default_text_color">#666666</color>
<color name="main_black_text">#333333</color>
<color name="main_red_text">#e53e42</color>
<color name="main_white_text">#ffffff</color>
<color name="protocol_text">#3C68FE</color>
<!--图标-->
<color name="abate_icon_color">#bbbbbb</color>
<color name="default_icon_color">#999999</color>
<color name="pressed_icon_color">#e53e42</color>
<!--CycleViewPager-->
<color name="cycle_image_bg">#44222222</color>
<!-- 透明 -->
<color name="transparent">#00000000</color>
在定义一个dimens:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="common_button_size">15sp</dimen>
<dimen name="common_title_size">18sp</dimen>
<dimen name="common_font_size">14sp</dimen>
<dimen name="common_smail_font_size">12sp</dimen>
<dimen name="common_font_size_16">16sp</dimen>
<dimen name="common_font_size_14">14sp</dimen>
<dimen name="common_font_size_12">12sp</dimen>
<dimen name="car_radio_margin">10dp</dimen>
<dimen name="car_style_margin_top">5dp</dimen>
<dimen name="car_drawable_left_margin">2dp</dimen>
<dimen name="coupons_margin_left">10dp</dimen>
<dimen name="coupons_margin_top">2dp</dimen>
<dimen name="margin_10">10dp</dimen>
<dimen name="padding_left">14dp</dimen>
<dimen name="padding_right">14dp</dimen>
<dimen name="margin_left">14dp</dimen>
<dimen name="margin_right">14dp</dimen>
<!--外边距-->
<dimen name="margin_1dp">1dp</dimen>
<dimen name="margin_2dp">2dp</dimen>
<dimen name="margin_3dp">3dp</dimen>
<dimen name="margin_4dp">4dp</dimen>
<dimen name="margin_5dp">5dp</dimen>
<dimen name="margin_6dp">6dp</dimen>
<dimen name="margin_7dp">7dp</dimen>
<dimen name="margin_8dp">8dp</dimen>
<dimen name="margin_10dp">10dp</dimen>
<dimen name="margin_11dp">10dp</dimen>
<dimen name="margin_12dp">10dp</dimen>
<dimen name="margin_15dp">15dp</dimen>
<dimen name="margin_20dp">20dp</dimen>
<dimen name="margin_25dp">25dp</dimen>
<dimen name="margin_30dp">30dp</dimen>
<dimen name="margin_50dp">50dp</dimen>
<!--内边距-->
<dimen name="padding_2dp">2dp</dimen>
<dimen name="padding_3dp">3dp</dimen>
<dimen name="padding_4dp">4dp</dimen>
<dimen name="padding_5dp">5dp</dimen>
<dimen name="padding_6dp">6dp</dimen>
<dimen name="padding_7dp">7dp</dimen>
<dimen name="padding_8dp">8dp</dimen>
<dimen name="padding_10dp">10dp</dimen>
<dimen name="padding_12dp">12dp</dimen>
<dimen name="padding_13dp">13dp</dimen>
<dimen name="padding_14dp">14dp</dimen>
<dimen name="padding_15dp">15dp</dimen>
<dimen name="padding_20dp">20dp</dimen>
<!--字体大小-->
<dimen name="textSize_10sp">10sp</dimen>
<dimen name="textSize_12sp">12sp</dimen>
<dimen name="textSize_13sp">13sp</dimen>
<dimen name="textSize_14sp">14sp</dimen>
<dimen name="textSize_15sp">15sp</dimen>
<dimen name="textSize_16sp">16sp</dimen>
<dimen name="textSize_18sp">18sp</dimen>
<dimen name="textSize_20sp">20sp</dimen>
<dimen name="textSize_22sp">22sp</dimen>
<dimen name="textSize_30sp">30sp</dimen>
<!--城市-->
<dimen name="side_letter_bar_letter_size">12sp</dimen>
<dimen name="city_text_size">16sp</dimen>
<dimen name="side_letter_bar_width">36dp</dimen>
<dimen name="width_200dp">200dp</dimen>
<dimen name="height_200dp">200dp</dimen>
<dimen name="margin_size5dp">5dp</dimen>
</resources>
现在这些做的差不多了,我们开始上主要的代码,看MainActivity中的代码:
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView tvShopCartSubmit,tvShopCartSelect,tvShopCartTotalNum;
private View mEmtryView;
private RecyclerView rlvShopCart,rlvHotProducts;
private ShopCartAdapter mShopCartAdapter;
private LinearLayout llPay;
private RelativeLayout rlHaveProduct;
private List<ShopCartBean.CartlistBean> mAllOrderList = new ArrayList<>();
private ArrayList<ShopCartBean.CartlistBean> mGoPayList = new ArrayList<>();
private List<String> mHotProductsList = new ArrayList<>();
private TextView tvShopCartTotalPrice;
private int mCount,mPosition;
private float mTotalPrice1;
private boolean mSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView()
{
tvShopCartSelect = (TextView) findViewById(R.id.tv_shopcart_addselect);
tvShopCartTotalPrice = (TextView) findViewById(R.id.tv_shopcart_totalprice);
tvShopCartTotalNum = (TextView) findViewById(R.id.tv_shopcart_totalnum);
rlHaveProduct = (RelativeLayout) findViewById(R.id.rl_shopcart_have);
rlvShopCart = (RecyclerView) findViewById(R.id.rlv_shopcart);
mEmtryView = (View) findViewById(R.id.emtryview);
mEmtryView.setVisibility(View.GONE);
llPay = (LinearLayout) findViewById(R.id.ll_pay);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
llPay.setLayoutParams(lp);
tvShopCartSubmit = (TextView) findViewById(R.id.tv_shopcart_submit);
rlvShopCart.setLayoutManager(new LinearLayoutManager(this));
mShopCartAdapter = new ShopCartAdapter(this,mAllOrderList);
rlvShopCart.setAdapter(mShopCartAdapter);
//删除商品接口
mShopCartAdapter.setOnDeleteClickListener(new ShopCartAdapter.OnDeleteClickListener() {
@Override
public void onDeleteClick(View view, int position,int cartid) {
mShopCartAdapter.notifyDataSetChanged();
}
});
//修改数量接口
mShopCartAdapter.setOnEditClickListener(new ShopCartAdapter.OnEditClickListener() {
@Override
public void onEditClick(int position, int cartid, int count) {
mCount = count;
mPosition = position;
}
});
//实时监控全选按钮
mShopCartAdapter.setResfreshListener(new ShopCartAdapter.OnResfreshListener() {
@Override
public void onResfresh( boolean isSelect) {
mSelect = isSelect;
if(isSelect){
Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
}else {
Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
}
float mTotalPrice = 0;
int mTotalNum = 0;
mTotalPrice1 = 0;
mGoPayList.clear();
for(int i = 0;i < mAllOrderList.size(); i++)
if(mAllOrderList.get(i).getIsSelect()) {
mTotalPrice += Float.parseFloat(mAllOrderList.get(i).getPrice()) * mAllOrderList.get(i).getCount();
mTotalNum += 1;
mGoPayList.add(mAllOrderList.get(i));
}
mTotalPrice1 = mTotalPrice;
tvShopCartTotalPrice.setText("总价:" + mTotalPrice);
tvShopCartTotalNum.setText("共" + mTotalNum + "件商品");
}
});
//全选
tvShopCartSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSelect = !mSelect;
if(mSelect){
Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
for(int i = 0;i < mAllOrderList.size();i++){
mAllOrderList.get(i).setSelect(true);
mAllOrderList.get(i).setShopSelect(true);
}
}else{
Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
for(int i = 0;i < mAllOrderList.size();i++){
mAllOrderList.get(i).setSelect(false);
mAllOrderList.get(i).setShopSelect(false);
}
}
mShopCartAdapter.notifyDataSetChanged();
}
});
initData();
mShopCartAdapter.notifyDataSetChanged();
}
private void initData(){
for(int i = 0;i < 2;i ++){
ShopCartBean.CartlistBean sb = new ShopCartBean.CartlistBean();
sb.setShopId(1);
sb.setPrice("1.0");
sb.setDefaultPic("http://img2.3lian.com/2014/c7/25/d/40.jpg");
sb.setProductName("狼牙龙珠鼠标");
sb.setShopName("狼牙龙珠");
sb.setColor("蓝色");
sb.setCount(2);
mAllOrderList.add(sb);
}
for(int i = 0;i < 2;i ++){
ShopCartBean.CartlistBean sb = new ShopCartBean.CartlistBean();
sb.setShopId(2);
sb.setPrice("1.0");
sb.setDefaultPic("http://img2.3lian.com/2014/c7/25/d/40.jpg");
sb.setProductName("达尔优鼠标");
sb.setShopName("达尔优");
sb.setColor("绿色");
sb.setCount(2);
mAllOrderList.add(sb);
}
isSelectFirst(mAllOrderList);
}
public static void isSelectFirst(List<ShopCartBean.CartlistBean> list){
if(list.size() > 0) {
list.get(0).setIsFirst(1);
for (int i = 1; i < list.size(); i++) {
if (list.get(i).getShopId() == list.get(i - 1).getShopId()) {
list.get(i).setIsFirst(2);
} else {
list.get(i).setIsFirst(1);
}
}
}
}
}
我们再来看看适配器中需要的代码,逻辑吧!
先看布局xml:
<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">
<LinearLayout
android:id="@+id/ll_shopcart_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/margin_10dp"
android:background="@color/background_color"
android:id="@+id/view"></View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android: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" />
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/margin_1dp"
android:background="@color/background_color"></View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android: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" />
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android: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" />
<ImageView
android:id="@+id/iv_item_shopcart_cloth_pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="@dimen/margin_10dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android: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" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_5dp"
android:layout_marginBottom="@dimen/margin_5dp">
<TextView
android: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" />
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_item_shopcart_cloth_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shopcart_minus_grey" />
<TextView
android: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" />
<ImageView
android: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>
<View
android: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>
<ImageView
android: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>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/margin_1dp"
android:background="@color/background_color"></View>
</LinearLayout>
剩下的就是主要的适配器代码了:
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 lenovo on 2017/10/24.
*/
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;
}
@Override
public ShopCartAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(context).inflate(R.layout.item_shopcart, parent, false);
return new ShopCartAdapter.MyViewHolder(view);
}
@Override
public void onBindViewHolder(final ShopCartAdapter.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() {
@Override
public 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() {
@Override
public 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() {
@Override
public void onClick(View v) {
showDialog(v,position);
}
});
holder.ivShopCartClothSel.setOnClickListener(new View.OnClickListener() {
@Override
public 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() {
@Override
public 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();
}
@Override
public 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 ShopCartAdapter.OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener{
void onItemClick(View view, int position);
}
public void setOnItemClickListener(ShopCartAdapter.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;
}
}
画两个椭圆:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="@dimen/height_200dp"></corners>
<stroke android:color="@color/default_icon_color" android:width="1dp"></stroke>
</shape>
记得改颜色哦!
还有一个bean类,在这就不写了。