最近看了一些淘宝购物车的demo,于是也写了一个。
效果图如下:
主要代码如下:
actvity中的代码:
public class ShoppingCartActivity extends baseActivity {
private List data;
private ListView mListView;
private ShoppingCartAdapter adapter;
private RelativeLayout rlRefresh;
private TextView tvRefresh;
private ProgressBar barRefresh;
private LinearLayout clear;
private CheckBox checkBox_select_all;
private CheckBox checkBox_add;
private TextView integral_sum;
private int sum = 0;
private int[] sumIntegral;
private Context context;
@Override
protected void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.activity_shopping_cart);
initView();
}
private void initView() {
context = this;
showpage = 1;
isPermitFlag = true;
data = new ArrayList();
// 测试数据
data.add(new Test("id", "color", "type", "100"));
data.add(new Test("id", "color", "type", "200"));
data.add(new Test("id", "color", "type", "300"));
data.add(new Test("id", "color", "type", "0"));
data.add(new Test("id", "color", "type", "300"));
data.add(new Test("id", "color", "type", "100"));
data.add(new Test("id", "color", "type", "500"));
data.add(new Test("id", "color", "type", "0"));
data.add(new Test("id", "color", "type", "900"));
adapter = new ShoppingCartAdapter(context, handler, data);
sumIntegral = new int[data.size() + 1];
checkBox_add = (CheckBox) findViewById(R.id.checkbox_add);
integral_sum = (TextView) findViewById(R.id.integral_sum);
clear = (LinearLayout) findViewById(R.id.clear);
clear.setonClickListener(new onClickListener() {
@Override
public void onClick(View v) {
data.clear();
adapter.notifyDataSetChanged();
integral_sum.setText(0 + "");
checkBox_select_all.setChecked(false);
checkBox_add.setClickable(false);
}
});
checkBox_select_all = (CheckBox) findViewById(R.id.checkbox_select);
checkBox_select_all.setonClickListener(new onClickListener() {
@Override
public void onClick(View v) {
HashMap isSelected = ShoppingCartAdapter
.getIsSelected();
Iterator iterator = isSelected.entrySet().iterator();
List array = new ArrayList();//列表中checkbox选中状态
List nums = new ArrayList();//列表中商品数量
while (iterator.hasNext()) {
HashMap.Entry entry = (HashMap.Entry) iterator.next();
Integer key = (Integer) entry.getKey();
Boolean val = (Boolean) entry.getValue();
array.add(val);
}
for (int i = 0; i < data.size(); i++) {
int num = data.get(i).getNum();
int integral = Integer.valueOf(data.get(i).getIntegral());
nums.add(num);
}
if (checkBox_select_all.isChecked()) {
for (int i = 0; i < data.size(); i++) {
ShoppingCartAdapter.getIsSelected().put(i, true);
}
checkBox_add.setChecked(true);
adapter.notifyDataSetChanged();
} else {
for (int i = 0; i < data.size(); i++) {
ShoppingCartAdapter.getIsSelected().put(i, false);
}
checkBox_add.setChecked(false);
adapter.notifyDataSetChanged();
integral_sum.setText(0 + "");
}
}
});
mListView= (ListView) findViewById(R.id.finance_list);
mListView.setAdapter(adapter);
mListView.setonItemClickListener(new onItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
Toast.makeText(context, position + "", Toast.LENGTH_LONG)
.show();
int pos = position - 1;
ViewHolder viewHolder = (ViewHolder) view.getTag();
int num = data.get(pos).getNum();
if (num == 0) {
Toast.makeText(context, "请选择商品数量", Toast.LENGTH_LONG)
.show();
} else {
boolean cu = !ShoppingCartAdapter.getIsSelected().get(pos);
ShoppingCartAdapter.getIsSelected().put(pos, cu);
adapter.notifyDataSetChanged();
//遍历获取列表中checkbox的选中状态
HashMap isSelected = ShoppingCartAdapter
.getIsSelected();
Iterator iterator = isSelected.entrySet().iterator();
List array = new ArrayList();
while (iterator.hasNext()) {
HashMap.Entry entry = (HashMap.Entry) iterator.next();
Integer key = (Integer) entry.getKey();
Boolean val = (Boolean) entry.getValue();
array.add(val);
}
if (Test.isAllFalse(array)) {
checkBox_select_all.setChecked(false);
checkBox_add.setChecked(false);
}
if (Test.isAllTrue(array)) {
checkBox_select_all.setChecked(true);
checkBox_add.setChecked(true);
}
if (Test.isHaveoneFasle(array)) {
checkBox_select_all.setChecked(false);
}
if (Test.isHaveoneTrue(array)) {
checkBox_add.setChecked(true);
}
}
}
});
}
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
@SuppressWarnings("unchecked")
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 10){ //更改选中商品的总价格
float price = (Float)msg.obj;
if(price > 0){
integral_sum.setText(price+"");
}else{
integral_sum.setText("0");
}
}
else if(msg.what == 11){
//列表选中状态
List array = (List) msg.obj;
if (Test.isAllFalse(array)) {
checkBox_select_all.setChecked(false);
checkBox_add.setChecked(false);
}
if (.isAllTrue(array)) {
checkBox_select_all.setChecked(true);
checkBox_add.setChecked(true);
}
if (Test.isHaveoneFasle(array)) {
checkBox_select_all.setChecked(false);
}
if (Test.isHaveoneTrue(array)) {
checkBox_add.setChecked(true);
}
}
}
};
actvity中XML的代码:
-XML中头部可以到网上找一个这里就不放上来了
.checkbox和button的样式可以根据个人喜好设置。
Adaper中的代码:
public class ShoppingCartAdapter extends baseAdapter {
private Context context;
private List loans;
private LayoutInflater inflater;
private static HashMap isSelected;
private static HashMap numbers;
private Handler handler;
int num;// 商品数量
static class ViewHolder { // 自定义控件集合
public CheckBox ck_select;
public ImageView pic_goods;
public TextView id_goods;
public TextView color_goods;
public TextView type_goods;
public TextView integral_goods;
public AddMinusWidget add_minus;
public LinearLayout layout;
public TextView number;
public Button minus;
public Button plus;
}
public ShoppingCartAdapter(Context context, Handler handler, List data) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.loans = data;
this.handler = handler;
isSelected = new HashMap();
numbers = new HashMap();
initDate();
}
private void initDate() {
for (int i = 0; i < loans.size(); i++) {
getIsSelected().put(i, false);
getNumbers().put(i, 1);
}
}
@Override
public int getCount() {
return loans.size();
}
@Override
public Object getItem(int position) {
return loans.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// 自定义视图
ViewHolder itemView = null;
if (convertView == null) {
// 获取list_item布局文件的视图
itemView = new ViewHolder();
convertView = inflater.inflate(R.layout.list_shopping_cart_item,
null);
// 获取控件对象
itemView.ck_select = (CheckBox) convertView
.findViewById(R.id.ck_select);
itemView.pic_goods = (ImageView) convertView
.findViewById(R.id.pic_goods);
itemView.id_goods = (TextView) convertView
.findViewById(R.id.id_goods);
itemView.color_goods = (TextView) convertView
.findViewById(R.id.color_goods);
itemView.type_goods = (TextView) convertView
.findViewById(R.id.type_goods);
itemView.integral_goods = (TextView) convertView
.findViewById(R.id.integral_goods);
itemView.number = (TextView) convertView.findViewById(R.id.number);
itemView.minus = (Button) convertView.findViewById(R.id.minus);
itemView.plus = (Button) convertView.findViewById(R.id.plus);
convertView.setTag(itemView);
} else {
itemView = (ViewHolder) convertView.getTag();
}
init(itemView, position);
itemView.ck_select.setChecked(getIsSelected().get(position));
itemView.number.setText(getNumbers().get(position).toString());
if (getIsSelected().get(position)) {
itemView.ck_select.setChecked(true);
} else {
itemView.ck_select.setChecked(false);
}
String a = itemView.number.getText().toString();
loans.get(position).setNum(Integer.valueOf(a));
Test test = loans.get(position);
itemView.id_goods.setText((CharSequence) test.getId());
itemView.color_goods.setText((CharSequence) test.getColor());
itemView.type_goods.setText((CharSequence) test.getType());
itemView.integral_goods.setText((CharSequence) test.getIntegral());
itemView.pic_goods.setImageResource(R.drawable.shopping);
return convertView;
}
private void init(final ViewHolder itemView, final int position) {
itemView.ck_select
.setonCheckedChangeListener(new onCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
isSelected.put(position, true);
getIsSelected().put(position, isChecked);
itemView.ck_select.setChecked(getIsSelected().get(
position));
handler.sendMessage(handler.obtainMessage(10,
getTotalPrice()));
Iterator iterator = isSelected.entrySet().iterator();
List array = new ArrayList();
while (iterator.hasNext()) {
HashMap.Entry entry = (HashMap.Entry) iterator
.next();
Integer key = (Integer) entry.getKey();
Boolean val = (Boolean) entry.getValue();
array.add(val);
}
handler.sendMessage(handler.obtainMessage(11, array));
}
});
final String numString = itemView.number.getText().toString();
itemView.plus.setonClickListener(new onClickListener() {
@Override
public void onClick(View v) {
if (numString == null || numString.equals("")) {
num = 1;
itemView.number.setText("1");
} else {
if (++num < 1) // 先加,再判断
{
num--;
Toast.makeText(context, "请输入一个大于0的数字",
Toast.LENGTH_SHORT).show();
} else {
itemView.number.setText(String.valueOf(num));
loans.get(position).setNum(num);
numbers.put(position, num);
handler.sendMessage(handler.obtainMessage(10,
getTotalPrice()));
Log.i("test", "+:" + num);
}
}
}
});
itemView.minus.setonClickListener(new onClickListener() {
@Override
public void onClick(View v) {
if (numString == null || numString.equals("")) {
num = 1;
itemView.number.setText("1");
} else {
if (--num < 1) // 先加,再判断
{
num++;
Log.i("test", "-:" + num);
Toast.makeText(context, "请输入一个大于0的数字",
Toast.LENGTH_SHORT).show();
Log.i("test", "-:" + num);
} else {
itemView.number.setText(String.valueOf(num));
Log.i("test", "-:" + num);
loans.get(position).setNum(num);
numbers.put(position, num);
handler.sendMessage(handler.obtainMessage(10,
getTotalPrice()));
}
}
}
});
}
private float getTotalPrice() {
Test bean = null;
float totalPrice = 0;
for (int i = 0; i < loans.size(); i++) {
bean = loans.get(i);
if (ShoppingCartAdapter.getIsSelected().get(i)) {
totalPrice += bean.getNum()
* Integer.valueOf(bean.getIntegral());
}
}
return totalPrice;
}
public static HashMap getIsSelected() {
return isSelected;
}
public static void setIsSelected(HashMap isSelected) {
ShoppingCartAdapter.isSelected = isSelected;
}
public static HashMap getNumbers() {
return numbers;
}
public static void setNumbers(HashMap numbers) {
ShoppingCartAdapter.numbers = numbers;
}
}
Adapter中的XML代码:
实体类:
package com.autoserve.net33.model;
public class Test {
@Override
public String toString() {
return "test [id=" + id + ", color=" + color
+ ", type=" + type + ", integral=" + integral + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getIntegral() {
return integral;
}
public void setIntegral(String integral) {
this.integral = integral;
}
private String id;
private String color;
private String type;
private String integral;
private int num;//商品数量
private int sumIntegral;
private boolean isChoosed; //商品是否在购物车中被选中
public Test(String id, String color, String type, String integral) {
super();
this.id = id;
this.color = color;
this.type = type;
this.integral = integral;
}
public Test() {
super();
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getSumIntegral() {
return sumIntegral;
}
public void setSumIntegral(int sumIntegral) {
this.sumIntegral = sumIntegral;
}
public boolean isChoosed() {
return isChoosed;
}
public void setChoosed(boolean isChoosed) {
this.isChoosed = isChoosed;
}
}
以上就是本文的全部内容,祝大家在新的一年里工作顺利,事事顺心,我们大家共同努力。



