本人最近工作不忙,所以没事就编写一些常用的demo发到网上,不是很难得技术,但是需要花费时间,需要的朋友可以直接拿来用,而且自己以后在项目中遇到可以直接拿来用。
效果图:
package com.example.aidlclicent;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
public class GoodsActivity extends ActionBarActivity
implements OnClickListener, CompoundButton.OnCheckedChangeListener {
private List productList;
GoodsAdapter goodsAdapter;
private ListView lv;
private Button bt_confrim_buy;
private TextView sum_goods_price;
private CheckBox cb_all_select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods);
initView();
initListDate();
}
private void initView() {
lv = (ListView) findViewById(R.id.listView);
bt_confrim_buy = (Button) findViewById(R.id.bt_confrim_buy);
sum_goods_price = (TextView) findViewById(R.id.sum_goods_price);
cb_all_select = (CheckBox) findViewById(R.id.cb_all_select);
cb_all_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//清空,原来数据
sum = 0;
for (Product product : productList) {
product.setSelected(true);
goodsAdapter.notifyDataSetChanged();
sum += product.getSumPrice();
}
sum_goods_price.setText("¥" + String.valueOf(sum));
} else {
for (Product product : productList) {
product.setSelected(false);
goodsAdapter.notifyDataSetChanged();
// sum =sum- product.getSumPrice();
}
// sum_goods_price.setText("¥" + String.valueOf(sum));
sum_goods_price.setText("¥:0");
}
}
});
}
public void confrimBuy(View view) {
}
private void initListDate() {
productList = new ArrayList<>();
for (int i = 0; i < 30; i++) {
Product product = new Product();
product.setName("商品" + i + "号");
product.setNum(i);
product.setPrice(i + 10);
product.setSumPrice(i * (i + 10));
productList.add(product);
}
goodsAdapter = new GoodsAdapter(this, productList);
lv.setAdapter(goodsAdapter);
goodsAdapter.setOnAddNum(this);
goodsAdapter.setOnsunNum(this);
goodsAdapter.setOnCheckedSelect(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.goods, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
Object tag = v.getTag();
switch (v.getId()) {
// +
case R.id.item_btn_add:
int position = (Integer) tag;
int num = productList.get(position).getNum();
int price = productList.get(position).getPrice();
num++;
int sum = price * num;
productList.get(position).setNum(num);
productList.get(position).setSumPrice(sum);
goodsAdapter.notifyDataSetChanged();
updateSumPrice(position, price, 1);
break;
// -
case R.id.item_btn_sub:
int price1 = 0;
int position1 = (Integer) tag;
int num1 = productList.get(position1).getNum();
if (num1 > 0) {
num1--;
price1 = productList.get(position1).getPrice();
int sum1 = price1 * num1;
productList.get(position1).setSumPrice(sum1);
updateSumPrice(position1, price1, 0);
}
productList.get(position1).setNum(num1);
goodsAdapter.notifyDataSetChanged();
break;
default:
break;
}
}
private void updateSumPrice(int position, int price1, int add) {
boolean isSelected = productList.get(position).isSelected();
if (isSelected && add == 1) {
sum = sum + price1;
sum_goods_price.setText("¥" + String.valueOf(sum));
} else if (isSelected && add == 0) {
sum = sum - price1;
sum_goods_price.setText("¥" + String.valueOf(sum));
}
}
int sum = 0;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = 0;
if (buttonView.getTag() != null) {
position = (int) buttonView.getTag();
} else {
return;
}
boolean isSelected = productList.get(position).isSelected();
if (isChecked && !isSelected) {
sum += productList.get(position).getSumPrice();
productList.get(position).setSelected(true);
} else if (!isChecked && isSelected) {
sum = sum - productList.get(position).getSumPrice();
productList.get(position).setSelected(false);
}
sum_goods_price.setText("¥" + String.valueOf(sum));
}
}
适配器GoodsAdapter.java类
package com.example.aidlclicent;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;
public class GoodsAdapter extends BaseAdapter {
// 集合 ,存放ListView的商品实体类数据
private List products;
Context context;
private View.OnClickListener addNum;
private View.OnClickListener subNum;
private OnCheckedChangeListener selectCheck;
public void setOnAddNum(View.OnClickListener addNum) {
this.addNum = addNum;
}
public void setOnsunNum(View.OnClickListener subNum) {
this.subNum = subNum;
}
public void setOnCheckedSelect(OnCheckedChangeListener select) {
this.selectCheck = select;
}
public GoodsAdapter(Context context,List products) {
super();
this.context = context;
this.products = products;
}
@Override
public int getCount() {
int ret = 0;
if(products!= null){
ret = products.size();
}
return ret;
}
@Override
public Object getItem(int position) {
return products.get(position);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
View v =null;
if(v!=null){
v=arg1;
}else{
v = LayoutInflater.from(context).inflate(R.layout.goods_item,arg2,false);
}
ViewHolder viewHolder = (ViewHolder) v.getTag();
if(viewHolder == null){
viewHolder = new ViewHolder();
viewHolder.item_btn_add = (ImageButton) v.findViewById(R.id.item_btn_add);
viewHolder.item_btn_sub = (ImageButton) v.findViewById(R.id.item_btn_sub);
viewHolder.item_product_name = (TextView) v.findViewById(R.id.item_product_name);
viewHolder.item_product_num = (TextView) v.findViewById(R.id.item_product_num);
viewHolder.item_product_price = (TextView) v.findViewById(R.id.item_product_price);
viewHolder.item_priduct_sumPrice = (TextView) v.findViewById(R.id.sum_price);
viewHolder.cb_select = (CheckBox) v.findViewById(R.id.cb_select);
viewHolder.item_btn_add .setOnClickListener(addNum);
viewHolder.item_btn_sub.setOnClickListener(subNum);
viewHolder.cb_select.setOnCheckedChangeListener(selectCheck);
v.setTag(viewHolder);
}
viewHolder.item_product_name.setText(products.get(position).getName());
viewHolder.item_product_num.setText(String.valueOf(products.get(position).getNum()));
viewHolder.item_product_price.setText(String.valueOf(products.get(position).getPrice()));
viewHolder.item_priduct_sumPrice.setText("¥"+String.valueOf(products.get(position).getSumPrice()));
boolean isSelected = products.get(position).isSelected();
if(isSelected){
viewHolder.cb_select.setChecked(true);
}else if(!isSelected){
viewHolder.cb_select.setChecked(false);;
}
viewHolder.item_btn_add.setTag(position);
viewHolder.item_btn_sub.setTag(position);
viewHolder.cb_select.setTag(position);
return v;
}
private static class ViewHolder{
//商品名称,数量,总价
private TextView item_product_name;
private TextView item_product_num;
private TextView item_product_price;
//增减商品数量按钮
private ImageButton item_btn_add;
private ImageButton item_btn_sub;
private TextView item_priduct_sumPrice;
private CheckBox cb_select;
}
}
godds.xml代码:
需要源码的同学请点击地址:
http://download.csdn.net/detail/bighua_mm/9603420