├── .classpath
├── .gitignore
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
└── org.eclipse.jdt.core.prefs
├── AndroidManifest.xml
├── README.md
├── libs
└── android-support-v4.jar
├── proguard-project.txt
├── project.properties
├── res
├── drawable
│ └── ic_launcher.png
├── layout
│ ├── activity_main.xml
│ ├── product_item.xml
│ └── store_item.xml
└── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── screenshot1.png
├── screenshot2.png
└── src
└── net
└── fengg
└── app
└── lcb
├── activity
└── MainActivity.java
├── adapter
├── ProductAdapter.java
└── StoreAdapter.java
├── model
├── Product.java
└── Store.java
├── util
└── Util.java
└── view
└── CustomListView.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 | # Proguard folder generated by Eclipse
23 | proguard/
24 |
25 | # Log Files
26 | *.log
27 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ListViewCheckBox
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 模仿淘宝购物车,按店铺归类,带复选框
2 | ================
3 |
4 | 
5 |
6 | 
7 |
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dotfeng/ListViewCheckBox/1dcedd17c771b4c3ac3dccc1186c8ead728bf804/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 |
--------------------------------------------------------------------------------
/res/drawable/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dotfeng/ListViewCheckBox/1dcedd17c771b4c3ac3dccc1186c8ead728bf804/res/drawable/ic_launcher.png
--------------------------------------------------------------------------------
/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
21 |
22 |
30 |
31 |
40 |
41 |
42 |
46 |
47 |
60 |
61 |
--------------------------------------------------------------------------------
/res/layout/product_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
25 |
26 |
32 |
33 |
39 |
40 |
41 |
48 |
55 |
65 |
66 |
74 |
75 |
84 |
85 |
96 |
97 |
107 |
108 |
118 |
119 |
120 |
121 |
--------------------------------------------------------------------------------
/res/layout/store_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
21 |
29 |
30 |
31 |
45 |
46 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ListViewCheckBox
5 |
6 |
7 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/screenshot1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dotfeng/ListViewCheckBox/1dcedd17c771b4c3ac3dccc1186c8ead728bf804/screenshot1.png
--------------------------------------------------------------------------------
/screenshot2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dotfeng/ListViewCheckBox/1dcedd17c771b4c3ac3dccc1186c8ead728bf804/screenshot2.png
--------------------------------------------------------------------------------
/src/net/fengg/app/lcb/activity/MainActivity.java:
--------------------------------------------------------------------------------
1 | package net.fengg.app.lcb.activity;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.ListIterator;
6 |
7 | import net.fengg.app.lcb.R;
8 | import net.fengg.app.lcb.adapter.ProductAdapter;
9 | import net.fengg.app.lcb.adapter.StoreAdapter;
10 | import net.fengg.app.lcb.model.Product;
11 | import net.fengg.app.lcb.model.Store;
12 | import android.app.Activity;
13 | import android.os.Bundle;
14 | import android.view.View;
15 | import android.view.View.OnClickListener;
16 | import android.widget.AdapterView;
17 | import android.widget.AdapterView.OnItemClickListener;
18 | import android.widget.CheckBox;
19 | import android.widget.ListView;
20 | import android.widget.TextView;
21 |
22 |
23 | public class MainActivity extends Activity
24 | implements OnClickListener, OnItemClickListener {
25 |
26 | private List list;
27 | private StoreAdapter adapter;
28 | private ListView lv_store;
29 | private CheckBox cb_select_all ;
30 | private TextView tv_amount;
31 |
32 | @Override
33 | protected void onCreate(Bundle savedInstanceState) {
34 | super.onCreate(savedInstanceState);
35 | setContentView(R.layout.activity_main);
36 | cb_select_all = (CheckBox) findViewById(R.id.cb_select_all);
37 | lv_store = (ListView) findViewById(R.id.lv_store);
38 | tv_amount = (TextView) findViewById(R.id.tv_amount);
39 |
40 | list = new ArrayList();
41 | initData();
42 | adapter = new StoreAdapter(this, list);
43 |
44 | lv_store.setAdapter(adapter);
45 | lv_store.setOnItemClickListener(this);
46 | }
47 |
48 | private void initData() {
49 | for (int i = 0; i < 5; i++) {
50 | Store store = new Store();
51 | store.setId(i);
52 | store.setName("店铺" + i);
53 |
54 | List plist = new ArrayList();
55 |
56 | for (int j = 0; j < 5; j++) {
57 | Product info = new Product();
58 | info.setId(j);
59 | info.setPrice(j+1);
60 | info.setContent("店铺中的商品" + i+j);
61 | info.setQuantity(1);
62 | plist.add(info);
63 | }
64 | store.setProducts(plist);
65 |
66 | list.add(store);
67 | }
68 |
69 | }
70 |
71 | @Override
72 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
73 |
74 | }
75 |
76 | @Override
77 | public void onClick(View view) {
78 | switch (view.getId()) {
79 | case R.id.btn_delete:
80 | ListIterator storeB = adapter.getSelect().listIterator();
81 | ListIterator storeL = list.listIterator();
82 | ListIterator adapterL = adapter.getPAdapterList().listIterator();
83 | while(storeB.hasNext()) {
84 | boolean it = storeB.next();
85 | Store store = storeL.next();
86 | ProductAdapter pAdapter = adapterL.next();
87 | ListIterator productB = pAdapter.getSelect().listIterator();
88 | ListIterator productL = store.getProducts().listIterator();
89 | if(it) {
90 | while(productB.hasNext()) {
91 | productB.next();productB.remove();
92 | productL.next();productL.remove();
93 | }
94 | storeB.remove();
95 | storeL.remove();
96 | adapterL.remove();
97 | }else {
98 | while(productB.hasNext()) {
99 | productL.next();
100 | if(productB.next()) {
101 | productB.remove();productL.remove();
102 | }
103 | }
104 | }
105 | }
106 | updateAmount();
107 | adapter.notifyDataSetChanged();
108 | cb_select_all.setChecked(false);
109 | break;
110 | case R.id.cb_select_all:
111 | boolean flag = cb_select_all.isChecked();
112 | for (int i = 0; i < adapter.getSelect().size(); i++) {
113 | adapter.getSelect().set(i, flag);
114 | for(int j=0;j selected = new ArrayList();
24 |
25 | private LayoutInflater inflater;
26 | private List list;
27 | StoreAdapter adapter;
28 | int storePosition;
29 | MainActivity context;
30 |
31 | public ProductAdapter(MainActivity context, List list, StoreAdapter adapter, int storePosition) {
32 | this.inflater = LayoutInflater.from(context);
33 | this.list = list;
34 | this.adapter = adapter;
35 | this.storePosition = storePosition;
36 | this.context = context;
37 | for (int j = 0; j < list.size(); j++) {
38 | getSelect().add(false);
39 | }
40 | }
41 |
42 | public List getSelect() {
43 | return selected;
44 | }
45 |
46 | @Override
47 | public View getView(final int position, View convertView, ViewGroup parent) {
48 | ViewHolder holder = null;
49 | if (convertView == null) {
50 | convertView = inflater.inflate(R.layout.product_item, null);
51 |
52 | holder = new ViewHolder();
53 | holder.cb_select = (CheckBox) convertView.findViewById(R.id.cb_select);
54 | holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
55 | holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
56 | holder.ll_quantity = (LinearLayout) convertView.findViewById(R.id.ll_quantity);
57 | holder.tv_quantity = (TextView) convertView.findViewById(R.id.tv_quantity);
58 | holder.btn_edit = (Button) convertView.findViewById(R.id.btn_edit);
59 | holder.ll_edit_quantity = (LinearLayout) convertView.findViewById(R.id.ll_edit_quantity);
60 | holder.btn_decrease = (Button) convertView.findViewById(R.id.btn_decrease);
61 | holder.btn_increase = (Button) convertView.findViewById(R.id.btn_increase);
62 | holder.et_quantity = (EditText) convertView.findViewById(R.id.et_quantity);
63 | holder.btn_done = (Button) convertView.findViewById(R.id.btn_done);
64 | convertView.setTag(holder);
65 | } else {
66 | holder = (ViewHolder) convertView.getTag();
67 | }
68 | final EditText et_quantity = holder.et_quantity;
69 | final TextView tv_quantity = holder.tv_quantity;
70 | final LinearLayout l_quantity = holder.ll_quantity;
71 | final LinearLayout le_quantity = holder.ll_edit_quantity;
72 |
73 | final Product product = list.get(position);
74 | holder.tv_content.setText(product.getContent());
75 | holder.tv_price.setText(product.getPrice() + "");
76 |
77 |
78 | holder.btn_edit.setOnClickListener(new OnClickListener() {
79 | @Override
80 | public void onClick(View v) {
81 | l_quantity.setVisibility(View.GONE);
82 | le_quantity.setVisibility(View.VISIBLE);
83 | }
84 | });
85 | holder.btn_done.setOnClickListener(new OnClickListener() {
86 | @Override
87 | public void onClick(View v) {
88 | l_quantity.setVisibility(View.VISIBLE);
89 | le_quantity.setVisibility(View.GONE);
90 | product.setQuantity(Integer.parseInt(et_quantity.getText().toString()));
91 | tv_quantity.setText(et_quantity.getText().toString());
92 | context.updateAmount();
93 | }
94 | });
95 |
96 | holder.et_quantity.setText(product.getQuantity() + "");
97 | holder.tv_quantity.setText(product.getQuantity() + "");
98 | holder.cb_select.setChecked(selected.get(position));
99 | holder.cb_select.setOnClickListener(new OnClickListener() {
100 |
101 | @Override
102 | public void onClick(View v) {
103 | System.out.println("selected set position:" + position);
104 | selected.set(position, !selected.get(position));
105 |
106 | if(selected.contains(false)) {
107 | adapter.getSelect().set(storePosition, false);
108 | }else {
109 | adapter.getSelect().set(storePosition, true);
110 | }
111 |
112 | if(adapter.getSelect().contains(false)) {
113 | context.checkAll(false);
114 | }else {
115 | context.checkAll(true);
116 | }
117 |
118 | context.updateAmount();
119 | adapter.notifyDataSetChanged();
120 | }
121 | });
122 |
123 | holder.btn_decrease.setOnClickListener(new OnClickListener() {
124 | @Override
125 | public void onClick(View v) {
126 | if(!(TextUtils.isEmpty(et_quantity.getText().toString())
127 | || "1".equals(et_quantity.getText().toString()))) {
128 | et_quantity.setText(Integer.parseInt(et_quantity.getText().toString()) - 1 + "");
129 | }
130 | }
131 | });
132 | holder.btn_increase.setOnClickListener(new OnClickListener() {
133 | @Override
134 | public void onClick(View v) {
135 | if(TextUtils.isEmpty(et_quantity.getText().toString())) {
136 | et_quantity.setText("1");
137 | } else {
138 | et_quantity.setText(Integer.parseInt(et_quantity.getText().toString()) + 1 + "");
139 | }
140 | }
141 | });
142 |
143 | return convertView;
144 | }
145 |
146 | @Override
147 | public int getCount() {
148 | return list.size();
149 | }
150 |
151 | @Override
152 | public Object getItem(int position) {
153 | return list.get(position);
154 | }
155 |
156 | @Override
157 | public long getItemId(int position) {
158 | return position;
159 | }
160 |
161 | public class ViewHolder {
162 | CheckBox cb_select;
163 | TextView tv_content;
164 | TextView tv_price;
165 | LinearLayout ll_quantity;
166 | TextView tv_quantity;
167 | Button btn_edit;
168 | LinearLayout ll_edit_quantity;
169 | EditText et_quantity;
170 | Button btn_decrease;
171 | Button btn_increase;
172 | Button btn_done;
173 | }
174 | }
175 |
--------------------------------------------------------------------------------
/src/net/fengg/app/lcb/adapter/StoreAdapter.java:
--------------------------------------------------------------------------------
1 | package net.fengg.app.lcb.adapter;
2 |
3 | import java.util.ArrayList;
4 | import java.util.LinkedList;
5 | import java.util.List;
6 |
7 | import net.fengg.app.lcb.R;
8 | import net.fengg.app.lcb.activity.MainActivity;
9 | import net.fengg.app.lcb.model.Store;
10 | import net.fengg.app.lcb.view.CustomListView;
11 | import android.view.LayoutInflater;
12 | import android.view.View;
13 | import android.view.View.OnClickListener;
14 | import android.view.ViewGroup;
15 | import android.widget.BaseAdapter;
16 | import android.widget.CheckBox;
17 | import android.widget.TextView;
18 |
19 | public class StoreAdapter extends BaseAdapter {
20 |
21 | private final LinkedList selected = new LinkedList();
22 |
23 | private LayoutInflater inflater;
24 | private List list;
25 | List pAdapterList = new ArrayList();
26 | MainActivity context;
27 |
28 | public StoreAdapter(MainActivity context, List list) {
29 | this.inflater = LayoutInflater.from(context);
30 | this.list = list;
31 | this.context = context;
32 | for (int i = 0; i < list.size(); i++) {
33 | getSelect().add(false);
34 | ProductAdapter pAdapter = new ProductAdapter(context, list.get(i).getProducts(),
35 | this, i);
36 | pAdapterList.add(pAdapter);
37 | }
38 | }
39 |
40 | public List getSelect() {
41 | return selected;
42 | }
43 |
44 | public ProductAdapter getPAdapter(int position) {
45 | return pAdapterList.get(position);
46 | }
47 |
48 | public List getPAdapterList() {
49 | return pAdapterList;
50 | }
51 |
52 | @Override
53 | public View getView(final int position, View convertView, ViewGroup parent) {
54 | ViewHolder holder = null;
55 | if (convertView == null) {
56 | convertView = inflater.inflate(R.layout.store_item, null);
57 |
58 | holder = new ViewHolder();
59 | holder.cb_select = (CheckBox) convertView.findViewById(R.id.cb_select);
60 | holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
61 | holder.lv_product = (CustomListView) convertView.findViewById(R.id.lv_product);
62 |
63 | convertView.setTag(holder);
64 | } else {
65 | holder = (ViewHolder) convertView.getTag();
66 | }
67 |
68 | holder.lv_product.setAdapter(pAdapterList.get(position));
69 | // Util.setListViewHeight(holder.lv_product);//设置listview高度,或者使用CustomListView
70 |
71 | final Store store = list.get(position);
72 |
73 | holder.tv_content.setText(store.getName());
74 |
75 | holder.cb_select.setChecked(selected.get(position));
76 | holder.cb_select.setOnClickListener(new OnClickListener() {
77 |
78 | @Override
79 | public void onClick(View v) {
80 | System.out.println("selected set position:" + position);
81 | selected.set(position, !selected.get(position));
82 | for(int i=0;i products;
14 | public int getId() {
15 | return id;
16 | }
17 | public void setId(int id) {
18 | this.id = id;
19 | }
20 | public String getName() {
21 | return name;
22 | }
23 | public void setName(String name) {
24 | this.name = name;
25 | }
26 | public List getProducts() {
27 | return products;
28 | }
29 | public void setProducts(List products) {
30 | this.products = products;
31 | }
32 |
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/net/fengg/app/lcb/util/Util.java:
--------------------------------------------------------------------------------
1 | package net.fengg.app.lcb.util;
2 |
3 | import android.view.View;
4 | import android.view.ViewGroup;
5 | import android.widget.ListAdapter;
6 | import android.widget.ListView;
7 |
8 | public class Util {
9 | public static void setListViewHeight(ListView listView) {
10 |
11 | ListAdapter listAdapter = listView.getAdapter();
12 |
13 | if (listAdapter == null) {
14 |
15 | return;
16 |
17 | }
18 |
19 | int totalHeight = 0;
20 |
21 | for (int i = 0; i < listAdapter.getCount(); i++) {
22 |
23 | View listItem = listAdapter.getView(i, null, listView);
24 |
25 | listItem.measure(0, 0);
26 |
27 | totalHeight += listItem.getMeasuredHeight();
28 |
29 | }
30 |
31 | ViewGroup.LayoutParams params = listView.getLayoutParams();
32 |
33 | params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
34 |
35 | listView.setLayoutParams(params);
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/net/fengg/app/lcb/view/CustomListView.java:
--------------------------------------------------------------------------------
1 | package net.fengg.app.lcb.view;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.widget.ListView;
6 |
7 | public class CustomListView extends ListView {
8 | public CustomListView(Context context) {
9 | super(context);
10 | }
11 |
12 | public CustomListView(Context context, AttributeSet attrs) {
13 | super(context, attrs);
14 | }
15 |
16 | public CustomListView(Context context, AttributeSet attrs,
17 | int defStyle) {
18 | super(context, attrs, defStyle);
19 | }
20 |
21 | @Override
22 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
23 | int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
24 | MeasureSpec.AT_MOST);
25 | super.onMeasure(widthMeasureSpec, expandSpec);
26 | }
27 | }
--------------------------------------------------------------------------------