andriod——简单实现二级购物车
2017-11-19 18:47
302 查看
compile 'com.squareup.picasso:picasso:2.5.1'
//ChildBean public class ChildBean { private String title; private String price; private String path; private Boolean isSelected=false; private int num; public ChildBean() { } public ChildBean(String title, String price, String path, Boolean isSelected, int num) { this.title = title; this.price = price; this.path = path; this.isSelected = isSelected; this.num = num; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public Boolean getSelected() { return isSelected; } public void setSelected(Boolean selected) { isSelected = selected; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
/bean-GroupBean
public class GroupBean { public String group_name; public Boolean isSelected=false; public GroupBean() { } public GroupBean(String group_name, Boolean isSelected) { this.group_name = group_name; this.isSelected = isSelected; } public String getGroup_name() { return group_name; } public void setGroup_name(String group_name) { this.group_name = group_name; } public Boolean getSelected() { return isSelected; } public void setSelected(Boolean selected) { isSelected = selected; } }
//utils-CartUtils
//CartUtils public class CartUtils { private static ElvAdapter adapter; public static void setCartData(Context context, final List<GroupBean> flist, final List<List<ChildBean>> slist, ExpandableListView elv, final CheckBox all, final TextView all_count, final TextView all_money){ all_count.setBackgroundResource(R.color.count); all_money.setBackgroundResource(R.color.money); all_count.setTextColor(Color.WHITE); all_money.setTextColor(Color.WHITE); elv.setGroupIndicator(null); adapter = new ElvAdapter(flist,slist,context,all,all_count,all_money); elv.setAdapter(adapter); for (int i=0;i<flist.size();i++){ elv.expandGroup(i); } all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i=0;i<flist.size();i++){ flist.get(i).setSelected(all.isChecked()); for (int j=0;j<slist.get(i).size();j++){ slist.get(i).get(j).setSelected(all.isChecked()); } } adapter.notifyDataSetChanged(); checkNum(flist,slist,all_count,all_money); } }); } public static void checkNum(List<GroupBean> flist, List<List<ChildBean>> slist, TextView count, TextView money){ int all_count=0; int all_money = 0; for (int i=0;i<flist.size();i++){ for (int j=0;j<slist.get(i).size();j++) { if(slist.get(i).get(j).getSelected()){ all_count += slist.get(i).get(j).getNum(); int v = (int) (Double.parseDouble(slist.get(i).get(j).getPrice())*slist.get(i).get(j).getNum()); all_money += v; } } } count.setText("总计:"+all_count); money.setText("计算:"+all_money); } static class ElvAdapter extends BaseExpandableListAdapter{ List<GroupBean> flist; List<List<ChildBean>> slist; Context context; CheckBox all; TextView all_count; TextView all_money; public ElvAdapter(List<GroupBean> flist, List<List<ChildBean>> slist,Context context,CheckBox all,TextView all_count,TextView all_money) { this.flist = flist; this.slist = slist; this.context=context; this.all=all; this.all_count=all_count; this.all_money=all_money; } @Override public int getGroupCount() { return flist.size(); } @Override public int getChildrenCount(int groupPosition) { return slist.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return flist.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return slist.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view=null; GroupHolder holder=null; if(convertView==null){ convertView=View.inflate(context, R.layout.group,null); 1d7be view=convertView; holder=new GroupHolder(); holder.group=(CheckBox)view.findViewById(R.id.group); holder.group_name=(TextView) view.findViewById(R.id.group_name); convertView.setTag(holder); }else{ view=convertView; holder= (GroupHolder) convertView.getTag(); } holder.group_name.setText(flist.get(groupPosition).getGroup_name()); holder.group.setChecked(flist.get(groupPosition).getSelected()); holder.group.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(flist.get(groupPosition).getSelected()){ flist.get(groupPosition).setSelected(false); all.setChecked(false); for (int i=0;i<slist.get(groupPosition).size();i++){ slist.get(groupPosition).get(i).setSelected(false); } }else{ int count=0; flist.get(groupPosition).setSelected(true); for (int i=0;i<slist.get(groupPosition).size();i++){ slist.get(groupPosition).get(i).setSelected(true); } for (int i=0;i<flist.size();i++){ if(flist.get(i).getSelected()){ count++; } } if(count==flist.size()){ all.setChecked(true); } } checkNum(flist,slist,all_count,all_money); notifyDataSetChanged(); } }); return view; } class GroupHolder{ CheckBox group; TextView group_name; } class ChildHolder{ CheckBox child; TextView title; ImageView image; TextView price; Jiajianqi amount_view; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view=null; ChildHolder holder=null; if(convertView==null){ convertView=View.inflate(context,R.layout.child,null); view=convertView; holder=new ChildHolder(); holder.child=(CheckBox)view.findViewById(R.id.child); holder.title=(TextView) view.findViewById(R.id.title); holder.price=(TextView)view.findViewById(R.id.price); holder.image=(ImageView)view.findViewById(R.id.image); holder.amount_view=(Jiajianqi)view.findViewById(R.id.amount_view); convertView.setTag(holder); }else{ view=convertView; holder= (ChildHolder) convertView.getTag(); } Picasso.with(context).load(slist.get(groupPosition).get(childPosition).getPath()).into(holder.image); holder.title.setText(slist.get(groupPosition).get(childPosition).getTitle()); holder.price.setText("¥"+slist.get(groupPosition).get(childPosition).getPrice()); holder.child.setChecked(slist.get(groupPosition).get(childPosition).getSelected()); final ChildHolder finalHolder = holder; holder.amount_view.setGoods_storage(100); holder.amount_view.setAmount(slist.get(groupPosition).get(childPosition).getNum()); holder.amount_view.setOnAmountChangeListener(new Jiajianqi.OnAmountChangeListener() { @Override public void onAmountChange(View view, int amount) { slist.get(groupPosition).get(childPosition).setNum(amount); checkNum(flist,slist,all_count,all_money); } }); holder.child.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(slist.get(groupPosition).get(childPosition).getSelected()){ slist.get(groupPosition).get(childPosition).setSelected(false); flist.get(groupPosition).setSelected(false); all.setChecked(false); }else{ int fcount=0; int scount=0; slist.get(groupPosition).get(childPosition).setSelected(true); for (int i=0;i<slist.get(groupPosition).size();i++){ if(slist.get(groupPosition).get(i).getSelected()){ scount++; } } if(scount==slist.get(groupPosition).size()){ flist.get(groupPosition).setSelected(true); } for (int i=0;i<flist.size();i++){ if(flist.get(i).getSelected()){ fcount++; } } if(fcount==flist.size()){ all.setChecked(true); } } checkNum(flist,slist,all_count,all_money); notifyDataSetChanged(); } }); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }
//Jiajianqi public class Jiajianqi extends LinearLayout implements View.OnClickListener, TextWatcher { private static final String TAG = "AmountView"; private int amount = 1; //购买数量 private int goods_storage = 1; //商品库存 private OnAmountChangeListener mListener; private EditText etAmount; private Button btnDecrease; private Button btnIncrease; public Jiajianqi(Context context) { this(context, null); } public Jiajianqi(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.view_amount, this); etAmount = (EditText) findViewById(R.id.etAmount); btnDecrease = (Button) findViewById(R.id.btnDecrease); btnIncrease = (Button) findViewById(R.id.btnIncrease); btnDecrease.setOnClickListener(this); btnIncrease.setOnClickListener(this); etAmount.addTextChangedListener(this); TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.AmountView); int btnWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnWidth, LayoutParams.WRAP_CONTENT); int tvWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvWidth, 80); int tvTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvTextSize, 0); int btnTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnTextSize, 0); obtainStyledAttributes.recycle(); LayoutParams btnParams = new LayoutParams(btnWidth, LayoutParams.MATCH_PARENT); btnDecrease.setLayoutParams(btnParams); btnIncrease.setLayoutParams(btnParams); if (btnTextSize != 0) { btnDecrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize); btnIncrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize); } LayoutParams textParams = new LayoutParams(tvWidth, LayoutParams.MATCH_PARENT); etAmount.setLayoutParams(textParams); if (tvTextSize != 0) { etAmount.setTextSize(tvTextSize); } } public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) { this.mListener = onAmountChangeListener; } public void setGoods_storage(int goods_storage) { this.goods_storage = goods_storage; } @Override public void onClick(View v) { int i = v.getId(); if (i == R.id.btnDecrease) { if (amount > 1) { amount--; etAmount.setText(amount + ""); } } else if (i == R.id.btnIncrease) { if (amount < goods_storage) { amount++; etAmount.setText(amount + ""); } } etAmount.clearFocus(); if (mListener != null) { mListener.onAmountChange(this, amount); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { } public int getAmount(){ return amount; } public void setAmount(int amount){ this.amount = amount; etAmount.setText(amount+""); } public interface OnAmountChangeListener { void onAmountChange(View view, int amount); } }
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.ExpandableListView;import android.widget.TextView;import com.example.thecar.bean.ChildBean;import com.example.thecar.bean.GroupBean;import com.example.thecar.utils.CartUtils;import java.util.ArrayList;import java.util.List;///////////////MainActivitypublic class MainActivity extends AppCompatActivity {private ExpandableListView listView;private TextView price,num;private CheckBox both;private List<GroupBean> glist=new ArrayList<>();private List<List<ChildBean>> clists=new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView= (ExpandableListView) findViewById(R.id.listview);price= (TextView) findViewById(R.id.price);num= (TextView) findViewById(R.id.num);both= (CheckBox) findViewById(R.id.both);for(int i=0;i<2;i++){glist.add(new GroupBean("商家"+i,false));}List<ChildBean> list1=new ArrayList<>();for(int i=0;i<5;i++){list1.add(new ChildBean("商品"+i,1+"","https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=594559231,2167829292&fm=27&gp=0.jpg",false,2));}List<ChildBean> list2=new ArrayList<>();for(int i=0;i<5;i++){list2.add(new ChildBean("商品"+i,1+"","https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=594559231,2167829292&fm=27&gp=0.jpg",false,2));}clists.add(list1);clists.add(list2);CartUtils.setCartData(this,glist,clists,listView,both,num,price);}}
//bg_amount_layout.xml<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#FFFFFF" /><strokeandroid:width="1dp"android:color="@color/divider" /><paddingandroid:bottom="1dp"android:left="1dp"android:right="1dp"android:top="1dp" /></shape>
//btn_amount.xml<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@color/divider" /><item android:state_enabled="false" android:drawable="@color/divider" /><item android:drawable="@android:color/white" /></selector>
//divider.xml<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><sizeandroid:width="0.5dp"/><solid android:color="@color/divider"/></shape>
//activity_main.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><ExpandableListViewandroid:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentTop="true"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"></ExpandableListView><RelativeLayoutandroid:id="@+id/a"android:layout_width="match_parent"android:layout_height="50dp"android:layout_alignParentBottom="true"><CheckBoxandroid:id="@+id/both"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="全选"android:layout_centerVertical="true"/><TextViewandroid:id="@+id/price"android:layout_width="wrap_content"android:layout_height="match_parent"android:text="价格"android:layout_centerInParent="true"android:gravity="center"/><TextViewandroid:id="@+id/num"android:layout_width="wrap_content"android:layout_height="match_parent"android:text="价格"android:layout_alignParentRight="true"android:gravity="center"/></RelativeLayout></RelativeLayout>
//child.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="100dp"android:gravity="center_vertical"android:layout_marginTop="10dp"android:layout_marginBottom="10dp"><CheckBoxandroid:id="@+id/child"android:layout_width="50dp"android:layout_height="50dp"android:layout_marginLeft="30dp"/><ImageViewandroid:id="@+id/image"android:layout_width="100dp"android:layout_height="100dp"android:scaleType="centerCrop"/><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"android:layout_marginLeft="10dp"><TextViewandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:layout_marginRight="10dp"android:maxLines="2"android:textSize="16sp"android:layout_marginTop="10dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center_vertical"android:orientation="horizontal"android:layout_marginRight="10dp"android:layout_marginLeft="10dp"><TextViewandroid:id="@+id/price"android:layout_width="wrap_content"android:layout_height="match_parent"android:textSize="17sp"android:gravity="center_vertical"android:textColor="#ff8100"/><com.example.thecar.utils.Jiajianqiandroid:id="@+id/amount_view"android:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"android:layout_marginLeft="10dp"app:btnTextSize="14sp"android:gravity="center"app:btnWidth="36dp"android:layout_marginBottom="5dp"app:tvWidth="50dp"></com.example.thecar.utils.Jiajianqi></LinearLayout></LinearLayout></LinearLayout>
//group.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center_vertical"><CheckBoxandroid:id="@+id/group"android:layout_width="50dp"android:layout_height="50dp"android:layout_marginLeft="10dp"/><TextViewandroid:id="@+id/group_name"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginLeft="10dp"android:textSize="18sp"android:layout_weight="1"android:gravity="center_vertical"/></LinearLayout>
//view_amount.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:focusable="true"android:divider="@drawable/divider"android:background="@drawable/bg_amount_layout"android:showDividers="middle"android:orientation="horizontal"><Buttonandroid:id="@+id/btnDecrease"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:background="@drawable/btn_amount"android:text="-"/><EditTextandroid:id="@+id/etAmount"android:layout_width="0dp"android:layout_height="match_parent"android:minWidth="60dp"android:layout_weight="2"android:background="@null"android:inputType="number"android:gravity="center"android:text="1"/><Buttonandroid:id="@+id/btnIncrease"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:background="@drawable/btn_amount"android:text="+"/></LinearLayout>
//attrs.xml<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="AmountView"><!-- 左右2边+-按钮的宽度 --><attr name="btnWidth" format="dimension" /><!-- 中间TextView的宽度 --><attr name="tvWidth" format="dimension" /><!--<attr name="tvColor" format="color"/>--><attr name="tvTextSize" format="dimension"/><attr name="btnTextSize" format="dimension"/></declare-styleable></resources>
//colors.xml<?xml version="1.0" encoding="utf-8"?><resources><color name="divider">#ffd2d2d2</color><color name="count">#ffae01</color><color name="money">#ff6e01</color></resources>
//styles.xml<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --></style></resources>
相关文章推荐
- Andriod购物车实现二级列表(MVP)
- andriod——二级列表完美实现购物车
- Android二级购物车(带商家)的简单实现
- Andriodjie——二级列表实现购物车
- Andriodjie——二级列表实现购物车
- App中二级列表购物车的简单实现,查,删,改的功能
- andriod——Retrofit+Fresco+MVP+解绑实现二级购物车
- 简单实现 二级列表购物车(固定数据)
- 利用JS实现一个简单的二级联动菜单
- JavaScript 实现简单二级联动
- 一个简单的二级表单实现
- php 实现简单加入购物车
- jQuery实现简单二级下拉菜单
- jquery学习笔记-购物车表单简单实现
- php简单实现二级联动
- PHP简单实现购物车
- andriod饼图简单实现
- JSP只用Hashtable和session实现购物车功能的简单实例
- 利用JQuery实现简单的购物车结算
- 二级列表实现购物车的效果