Android实现购物车功能最近看了⼀些淘宝购物车的demo,于是也写了⼀个。
效果图如下:
主要代码如下:
actvity中的代码:
public class ShoppingCartActivity extends BaseActivity {
private List<Test> 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
setContentView(R.layout.activity_shopping_cart);
initView();
}
private void initView() {
context = this;
showpage = 1;
isPermitFlag = true;
data = new ArrayList<Test>();
// 测试数据
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();
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<Integer, Boolean> isSelected = ShoppingCartAdapter
.getIsSelected();
Iterator iterator = Set().iterator();
List<Boolean> array = new ArrayList<Boolean>();//列表中checkbox选中状态        List<Integer> nums = new ArrayList<Integer>();//列表中商品数量
while (iterator.hasNext()) {
HashMap.Entry entry = (HashMap.Entry) ();
Integer key = (Integer) Key();
Boolean val = (Boolean) Value();
array.add(val);
}
for (int i = 0; i < data.size(); i++) {
int num = (i).getNum();
int integral = Integer.(i).getIntegral());
nums.add(num);
}
if (checkBox_select_all.isChecked()) {
for (int i = 0; i < data.size(); i++) {
}
checkBox_add.setChecked(true);
} else {
for (int i = 0; i < data.size(); i++) {
}
checkBox_add.setChecked(false);
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) Tag();
int num = (pos).getNum();
if (num == 0) {
Toast.makeText(context, "请选择商品数量", Toast.LENGTH_LONG)              .show();
} else {
boolean cu = !IsSelected().get(pos);
//遍历获取列表中checkbox的选中状态
HashMap<Integer, Boolean> isSelected = ShoppingCartAdapter
.getIsSelected();
Iterator iterator = Set().iterator();
List<Boolean> array = new ArrayList<Boolean>();
while (iterator.hasNext()) {
HashMap.Entry entry = (HashMap.Entry) ();
Integer key = (Integer) Key();
Boolean val = (Boolean) Value();
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<Boolean> array = (List<Boolean>) 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 version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android/apk/res/android" xmlns:header=schemas.android/apk/res/st"  android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_background"
android:orientation="vertical" >
<widget.HeaderWidget
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
header:text="我的购物车" />
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/header"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@color/white"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkbox_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="全选"
android:textColor="@color/gry_666666"
android:textSize="@dimen/small_size" />
</LinearLayout>
<LinearLayout
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:gravity="center_vertical|right"
android:orientation="horizontal" >
<CheckBox
android:layout_width="12dp"
android:layout_height="12dp"
android:background="@drawable/clear" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="清空购物车"
android:textColor="#b61d1d"
android:textSize="@dimen/small_size" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/layout1"
android:layout_marginBottom="50dp"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="@color/divider_color" />    <ListView
android:id="@+id/finance_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:divider="@color/lucency" />
<include
layout="@layout/include_refresh"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@color/gry_999999"
android:gravity="center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkbox_add"
          android:layout_width="wrap_content"
android:clickable="false"
android:layout_height="wrap_content" />      </LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="合计:"
android:textColor="@color/white"
android:textSize="@dimen/small_size" />        <TextView
android:id="@+id/integral_sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="0"
android:textColor="@color/theme_color"

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。