//主MainActivity
@BindView(R.id.third_recyclerview) RecyclerView thirdRecyclerview; @BindView(R.id.third_allselect) TextView thirdAllselect; @BindView(R.id.third_totalprice) TextView thirdTotalprice; @BindView(R.id.third_totalnum) TextView thirdTotalnum; @BindView(R.id.third_submit) TextView thirdSubmit; @BindView(R.id.third_pay_linear) LinearLayout thirdPayLinear; private ThirdFragmentAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // 1 选中 2 未选中 thirdAllselect.setTag(1); showData(); } //存放购物车中所有的商品 private List<ShopBean.OrderDataBean.CartlistBean> mAllOrderList = new ArrayList<>(); private void showData() { LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); adapter = new ThirdFragmentAdapter(this); thirdRecyclerview.setAdapter(adapter); thirdRecyclerview.setLayoutManager(linearLayoutManager); try { InputStream inputStream = getAssets().open("shop.json"); String data = StringUtils.convertStreamToString(inputStream); Gson gson = new Gson(); ShopBean shopBean = gson.fromJson(data, ShopBean.class); for (int i = 0; i < shopBean.getOrderData().size(); i++) { int length = shopBean.getOrderData().get(i).getCartlist().size(); for (int j = 0; j < length; j++) { mAllOrderList.add(shopBean.getOrderData().get(i).getCartlist().get(j)); } } setFirstState(mAllOrderList); adapter.setData(mAllOrderList); } catch (Exception e) { e.printStackTrace(); } //删除数据回调 adapter.setOnDeleteClickListener(new ThirdFragmentAdapter.OnDeleteClickListener() { @Override public void onDeleteClick(View view, int position, int cartid) { } }); // adapter.setOnRefershListener(new ThirdFragmentAdapter.OnRefershListener() { @Override public void onRefersh(boolean isSelect, List<ShopBean.OrderDataBean.CartlistBean> list) { //标记底部 全选按钮 if (isSelect) { Drawable left = getResources().getDrawable(R.drawable.shopcart_selected); thirdAllselect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); } else { Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected); thirdAllselect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); } //总价 float mTotlaPrice = 0f; int mTotalNum = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i).isSelect()) { mTotlaPrice += list.get(i).getPrice() * list.get(i).getCount(); mTotalNum += list.get(i).getCount(); } } System.out.println("mTotlaPrice = " + mTotlaPrice); thirdTotalprice.setText("总价 : " + mTotlaPrice); thirdTotalnum.setText("共" + mTotalNum + "件商品"); } }); } /** * 标记第一条数据 isfirst 1 显示商户名称 2 隐藏 * * @param list */ public static void setFirstState(List<ShopBean.OrderDataBean.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); } } } } @OnClick({R.id.third_allselect, R.id.third_totalprice, R.id.third_totalnum, R.id.third_submit}) public void onClick(View view) { switch (view.getId()) { case R.id.third_allselect: // 全选 int state = (Integer) thirdAllselect.getTag() ; adapter.setUnSelected(state); if(state == 1){ thirdAllselect.setTag(2); }else { thirdAllselect.setTag(1); } break; case R.id.third_totalprice: break; case R.id.third_totalnum: break; case R.id.third_submit: break; } } }
//适配器
public class ThirdFragmentAdapter extends RecyclerView.Adapter<ThirdFragmentAdapter.IViewHolder> { private Activity context; private List<ShopBean.OrderDataBean.CartlistBean> list ; public ThirdFragmentAdapter(MainActivity context) { this.context = context; } public void setData(List<ShopBean.OrderDataBean.CartlistBean> list){ if(this.list == null){ this.list = new ArrayList<>(); } this.list.addAll(list); notifyDataSetChanged(); } @Override public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.third_fragment_item, parent, false); IViewHolder viewHolder = new IViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(IViewHolder holder, final int position) { if (position > 0) { //头 if (list.get(position).getShopId() == list.get(position - 1).getShopId()) { holder.llShopcartHeader.setVisibility(View.GONE); } else { holder.llShopcartHeader.setVisibility(View.VISIBLE); } }else { // position = 0 holder.llShopcartHeader.setVisibility(View.VISIBLE); } System.out.println("holder = " + list.get(position).getShopName()); holder.tvItemShopcartClothColor.setText("颜色:" + list.get(position).getColor()); holder.tvItemShopcartClothSize.setText("尺寸:" + list.get(position).getSize()); holder.tvItemShopcartClothname.setText(list.get(position).getProductName()); holder.tvItemShopcartShopname.setText(list.get(position).getShopName()); holder.tvItemShopcartClothPrice.setText("¥" + list.get(position).getPrice()); holder.etItemShopcartClothNum.setText(list.get(position).getCount() + ""); Glide.with(context).load(list.get(position).getDefaultPic()).into(holder.ivItemShopcartClothPic); //标记 商品是否被选中 if(list.get(position).isSelect()){ holder.tvItemShopcartClothselect.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected)); }else { holder.tvItemShopcartClothselect.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected)); } //标记商店是否被选中 if(list.get(position).isShopSelect()){ holder.ivItemShopcartShopselect.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected)); }else { holder.ivItemShopcartShopselect.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected)); } //? if(onRefershListener != null){ boolean isSelect = false; for(int i=0;i<list.size();i++){ if(!list.get(i).isSelect()){ isSelect = false; // 只要有一个商品是 未选中的状态 ,全选按钮就是未选中 break; }else { isSelect = true; } } onRefershListener.onRefersh(isSelect,list); } //删除事件 回调 holder.ivItemShopcartClothDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(onDeleteClickListener != null){ onDeleteClickListener.onDeleteClick(v,position,list.get(position).getId()); } list.remove(position); //如果删除的是第一条数据(或者是 数据带有商户名称的数据) 更新数据源, 标记 那条数据 显示商户名称 MainActivity.setFirstState(list); notifyDataSetChanged(); } }); // - 商品数量事件 holder.ivItemShopcartClothMinus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(list.get(position).getCount() > 1){ int count = list.get(position).getCount() - 1 ; list.get(position).setCount(count); notifyDataSetChanged(); if(onEditListener != null){ onEditListener.onEditListener(position,list.get(position).getId(),count); } } } }); // + 商品数量事件 holder.ivItemShopcartClothAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int count = list.get(position).getCount()+ 1 ; list.get(position).setCount(count); notifyDataSetChanged(); if(onEditListener != null){ onEditListener.onEditListener(position,list.get(position).getId(),count); } } }); //商品 选中和未选中 事件点击 holder.tvItemShopcartClothselect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //标记 当前 item 的选中状态 list.get(position).setSelect(!list.get(position).isSelect()); for(int i=0;i<list.size();i++){ for(int j=0;j<list.size();j++){ //如果是同一家商铺的商品,并且其中一个商品是未选中,那么商铺的全选勾选取消 if(list.get(j).getShopId() == list.get(i).getShopId() && !list.get(j).isSelect()){ list.get(i).setShopSelect(false); break; } else { //如果是同一家商铺的商品,并且所有商品是选中,那么商铺的选中全选勾选 list.get(i).setShopSelect(true); } } } notifyDataSetChanged(); } }); // 店铺 选中 yu 未选中 holder.ivItemShopcartShopselect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(list.get(position).getIsFirst() == 1){ // 三只松鼠 isShopSelect false , isSelect false // 三只松鼠 isShopSelect true 三只松鼠 isSelect true , 小米手环 true list.get(position).setShopSelect(!list.get(position).isShopSelect()); for(int i=0;i<list.size();i++){ if(list.get(i).getShopId() == list.get(position).getShopId()){ list.get(i).setSelect(list.get(position).isShopSelect()); } } notifyDataSetChanged(); } } }); } // 全选 public void setUnSelected(int selected){ if(list != null && list.size() > 0){ for (int i=0;i<list.size();i++){ if(selected == 1){ list.get(i).setSelect(false); list.get(i).setShopSelect(false); } else { list.get(i).setSelect(true); list.get(i).setShopSelect(true); } } notifyDataSetChanged(); } } @Override public int getItemCount() { return list == null ? 0 : list.size() ; } // 点击事件 public OnItemClickListener onItemClickListener; public interface OnItemClickListener { void onItemClick(View view, int position); } public void setOnItemClickListener(OnItemClickListener listener){ this.onItemClickListener = listener ; } //删除 public OnDeleteClickListener onDeleteClickListener; public interface OnDeleteClickListener { void onDeleteClick(View view, int position, int cartid); } public void setOnDeleteClickListener(OnDeleteClickListener deleteClickListener){ this.onDeleteClickListener = deleteClickListener; } public OnEditListener onEditListener; //添加 减少 public interface OnEditListener { void onEditListener(int position, int cartid, int count); } public void setOnEditListener(OnEditListener onEditListener){ this.onEditListener = onEditListener; } // 商品 选中状态发生变化 public OnRefershListener onRefershListener; public interface OnRefershListener{ //isSelect true 表示商品全部选中 false 未全部选中 void onRefersh(boolean isSelect, List<ShopBean.OrderDataBean.CartlistBean> list); } public void setOnRefershListener(OnRefershListener listener){ this.onRefershListener = listener ; } class IViewHolder extends RecyclerView.ViewHolder{ @BindView(R.id.view) View view; @BindView(R.id.iv_item_shopcart_shopselect) ImageView ivItemShopcartShopselect; @BindView(R.id.tv_item_shopcart_shopname) TextView tvItemShopcartShopname; @BindView(R.id.ll_shopcart_header) LinearLayout llShopcartHeader; @BindView(R.id.tv_item_shopcart_clothname) TextView tvItemShopcartClothname; @BindView(R.id.tv_item_shopcart_clothselect) ImageView tvItemShopcartClothselect; @BindView(R.id.iv_item_shopcart_cloth_pic) ImageView ivItemShopcartClothPic; @BindView(R.id.tv_item_shopcart_cloth_price) TextView tvItemShopcartClothPrice; @BindView(R.id.tv_item_shopcart_cloth_color) TextView tvItemShopcartClothColor; @BindView(R.id.tv_item_shopcart_cloth_size) TextView tvItemShopcartClothSize; @BindView(R.id.iv_item_shopcart_cloth_minus) ImageView ivItemShopcartClothMinus; @BindView(R.id.et_item_shopcart_cloth_num) TextView etItemShopcartClothNum; @BindView(R.id.iv_item_shopcart_cloth_add) ImageView ivItemShopcartClothAdd; @BindView(R.id.iv_item_shopcart_cloth_delete) ImageView ivItemShopcartClothDelete; public IViewHolder(View view) { super(view); ButterKnife.bind(this, view); } } }
//Ben类
Bean类里的属性从json.xml文件里获取
//缓冲
public class StringUtils { public static String convertStreamToString(InputStream is) { /* * To convert the InputStream to String we use the BufferedReader.readLine() * method. We iterate until the BufferedReader return null which means * there's no more data to read. Each line will appended to a StringBuilder * and returned as String. */ BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } }
//json.xml格式assets文件写在写在main文件下
//所需添加的依赖
compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.github.bumptech.glide:glide:4.2.0' compile 'com.google.code.gson:gson:2.8.2' annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
//布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 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="com.bawei.ceshi.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/third_recyclerview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_weight="0" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#FFFFFF" android:gravity="center_vertical" android:id="@+id/third_pay_linear" > <TextView android:id="@+id/third_allselect" 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/third_totalprice" android:layout_width="200dp" android:layout_height="wrap_content" android:paddingLeft="@dimen/padding_10dp" android:paddingTop="@dimen/padding_10dp" android:text="总价:" android:textColor="@color/cblack" android:textSize="@dimen/common_font_size_16" /> <TextView android:textColor="@color/cblack" android:id="@+id/third_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/third_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="#000000" android:layout_marginRight="@dimen/margin_10dp" /> </LinearLayout> </LinearLayout>
//条目布局
<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/cwhite"> <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"/> <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:textColor="@color/cblack" 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" /> <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:textColor="@color/cblack" 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:textColor="@color/cblack" 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:textColor="@color/cblack" 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:textColor="@color/cblack" 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" /> <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" /> </LinearLayout>
//colors下的颜色
<color name="cblack">#000000</color> <color name="cwhite">#FFFFFF</color> <color name="background_color">#f6f6f6</color> <color name="main_red_text">#e53e42</color> <color name="splitline_color">#dddddd</color> <color name="pressed_icon_color">#e53e42</color>
//dimens文件下的
<dimen name="margin_10dp">10dp</dimen> <dimen name="padding_20dp">20dp</dimen> <dimen name="padding_5dp">5dp</dimen> <dimen name="padding_10dp">10dp</dimen> <dimen name="common_font_size_16">16sp</dimen> <dimen name="common_font_size_14">14sp</dimen> <dimen name="height_200dp">200dp</dimen> <dimen name="margin_30dp">30dp</dimen> <dimen name="margin_15dp">15dp</dimen> <dimen name="margin_1dp">1dp</dimen> <dimen name="margin_5dp">5dp</dimen> <dimen name="common_font_size_12">12sp</dimen> <dimen name="padding_2dp">2dp</dimen> <dimen name="margin_20dp">20dp</dimen>
//draweble文件下的shopcart_add_but.xml
<corners android:radius="@dimen/height_200dp"></corners> <stroke android:color="@color/background_color" android:width="1dp"></stroke>
//draweble文件下的login.xml
<corners android:radius="@dimen/height_200dp"></corners> <solid android:color="@color/pressed_icon_color"></solid>