Skip to content

Commit c1f3bfa

Browse files
committed
添加购物车demo功能
1 parent f6cdcd4 commit c1f3bfa

15 files changed

+585
-9
lines changed

.idea/misc.xml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ android {
1919
}
2020

2121
dependencies {
22+
androidTestCompile('com.android.support:support-annotations:26.1.0') {
23+
force = true
24+
}
2225
implementation fileTree(dir: 'libs', include: ['*.jar'])
2326
implementation 'com.android.support:appcompat-v7:26.1.0'
2427
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
@@ -29,4 +32,6 @@ dependencies {
2932
testImplementation 'junit:junit:4.12'
3033
androidTestImplementation 'com.android.support.test:runner:1.0.2'
3134
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
35+
compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'
3236
}
37+

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<category android:name="android.intent.category.LAUNCHER" />
1717
</intent-filter>
1818
</activity>
19+
<activity android:name=".ShopCarActivity"></activity>
1920
</application>
2021

2122
</manifest>

app/src/main/java/com/example/strivecheng/appdragview/MainActivity.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.strivecheng.appdragview;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.support.v7.app.AppCompatActivity;
56
import android.support.v7.widget.GridLayoutManager;
@@ -131,10 +132,13 @@ public void onItemChildClick(BaseQuickAdapter adapter, View view, int position)
131132
allAppItemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
132133
@Override
133134
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
135+
AllAppSection allAppSection = allAppItemAdapter.getItem(position);
136+
AppInfo appInfo = null;
137+
if (allAppSection != null) {
138+
appInfo = allAppSection.t;
139+
}
134140
switch (view.getId()) {
135141
case R.id.small_delete_iv:
136-
AllAppSection allAppSection = allAppItemAdapter.getItem(position);
137-
AppInfo appInfo = allAppSection.t;
138142
if (appInfo != null) {
139143
AppInfo info = isContain(appInfo.getCode(), myAppInfos);
140144
if (info != null) {
@@ -151,6 +155,11 @@ public void onItemChildClick(BaseQuickAdapter adapter, View view, int position)
151155
myAppDragAdapter.notifyDataSetChanged();
152156
}
153157

158+
break;
159+
case R.id.info_rl:
160+
if (appInfo != null && "QGW".equals(appInfo.getCode())) {
161+
startActivity(new Intent(MainActivity.this, ShopCarActivity.class));
162+
}
154163
break;
155164
default:
156165
}
@@ -234,6 +243,7 @@ private List<AllAppSection> getData() {
234243
allAppSections.add(new AllAppSection(new AppInfo("汇报", R.mipmap.work_report_icon, true, "HB")));
235244
allAppSections.add(new AllAppSection(new AppInfo("文件", R.mipmap.wj_icon, true, "WJ")));
236245
allAppSections.add(new AllAppSection(new AppInfo("项目跟踪", R.mipmap.xmgz_icon, true, "XMZZ")));
246+
allAppSections.add(new AllAppSection(new AppInfo("去购物", R.mipmap.xmgz_icon, true, "QGW")));
237247

238248

239249
allAppSections.add(new AllAppSection(true, "继续教育"));
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package com.example.strivecheng.appdragview;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.support.v7.widget.DividerItemDecoration;
6+
import android.support.v7.widget.LinearLayoutManager;
7+
import android.support.v7.widget.RecyclerView;
8+
import android.text.Html;
9+
import android.view.View;
10+
import android.widget.Button;
11+
import android.widget.CheckBox;
12+
import android.widget.TextView;
13+
import android.widget.Toast;
14+
15+
import com.chad.library.adapter.base.BaseQuickAdapter;
16+
import com.example.strivecheng.appdragview.adapter.ShopCarListAdapter;
17+
import com.example.strivecheng.appdragview.data.GoodsInfo;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
/**
23+
* Create by xingcc on 2018/8/9
24+
* main function: 购物车界面
25+
*
26+
* @author xingcc
27+
*/
28+
public class ShopCarActivity extends AppCompatActivity implements View.OnClickListener {
29+
30+
private Button backBtn;
31+
private RecyclerView shopListRv;
32+
private CheckBox allSelectCb;
33+
private TextView totalTv;
34+
private Button payMoneyBtn;
35+
private ArrayList<GoodsInfo> goodsInfos;
36+
private ShopCarListAdapter shopCarListAdapter;
37+
/**
38+
* 记录是否是全选状态
39+
*/
40+
private boolean isAllSelect = true;
41+
42+
@Override
43+
protected void onCreate(Bundle savedInstanceState) {
44+
super.onCreate(savedInstanceState);
45+
setContentView(R.layout.activity_shop_car);
46+
initView();
47+
initListener();
48+
initData();
49+
}
50+
51+
private void initView() {
52+
goodsInfos = new ArrayList<>();
53+
54+
backBtn = findViewById(R.id.back_btn);
55+
shopListRv = findViewById(R.id.select_shop_list_rv);
56+
allSelectCb = findViewById(R.id.all_select_cb);
57+
totalTv = findViewById(R.id.total_tv);
58+
payMoneyBtn = findViewById(R.id.pay_money_btn);
59+
60+
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
61+
shopListRv.setLayoutManager(layoutManager);
62+
shopCarListAdapter = new ShopCarListAdapter(getData());
63+
shopListRv.setAdapter(shopCarListAdapter);
64+
shopListRv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
65+
66+
//初始化先判断所有商品是否选中
67+
setAllSelectCbState();
68+
}
69+
70+
71+
private void initListener() {
72+
backBtn.setOnClickListener(this);
73+
payMoneyBtn.setOnClickListener(this);
74+
allSelectCb.setOnClickListener(this);
75+
shopCarListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
76+
@Override
77+
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
78+
GoodsInfo goodsInfo = shopCarListAdapter.getItem(position);
79+
if (goodsInfo == null) {
80+
return;
81+
}
82+
TextView countTv = (TextView) adapter.getViewByPosition(shopListRv, position, R.id.goods_count_et);
83+
switch (view.getId()) {
84+
case R.id.sub_goods_iv:
85+
if (goodsInfo.getCount() <= 1) {
86+
Toast.makeText(ShopCarActivity.this, "不能再少啦!", Toast.LENGTH_SHORT).show();
87+
return;
88+
}
89+
goodsInfo.setCount(goodsInfo.getCount() - 1);
90+
countTv.setText(goodsInfo.getCount() + "");
91+
if (goodsInfo.isSelect()) {
92+
refreshTotal();
93+
}
94+
break;
95+
case R.id.add_goods_iv:
96+
goodsInfo.setCount(goodsInfo.getCount() + 1);
97+
countTv.setText(goodsInfo.getCount() + "");
98+
if (goodsInfo.isSelect()) {
99+
refreshTotal();
100+
}
101+
break;
102+
case R.id.delete_btn:
103+
goodsInfos.remove(position);
104+
adapter.notifyItemRemoved(position);
105+
refreshTotal();
106+
break;
107+
case R.id.select_cb:
108+
goodsInfo.setSelect(!goodsInfo.isSelect());
109+
refreshTotal();
110+
setAllSelectCbState();
111+
break;
112+
default:
113+
}
114+
}
115+
});
116+
117+
// allSelectCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
118+
// @Override
119+
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
120+
// if (buttonView.isEnabled()) {
121+
// allSelectGoods(isChecked);
122+
// shopCarListAdapter.notifyDataSetChanged();
123+
// refreshTotal();
124+
// }
125+
// }
126+
// });
127+
}
128+
129+
private void initData() {
130+
refreshTotal();
131+
}
132+
133+
/**
134+
* 刷新总结
135+
*/
136+
private void refreshTotal() {
137+
getTotal();
138+
String content = "合计支付:<font color=\"#33000000\" ><big>" + getTotal() + "</big></font> ";
139+
totalTv.setText(Html.fromHtml(content));
140+
// totalTv.setText("" + getTotal());
141+
}
142+
143+
/**
144+
* 获取要支付的总价
145+
*/
146+
private double getTotal() {
147+
double total = 0;
148+
for (GoodsInfo goods : goodsInfos) {
149+
if (goods.isSelect()) {
150+
total = total + goods.getPrice() * goods.getCount();
151+
}
152+
}
153+
return total;
154+
}
155+
156+
@Override
157+
public void onClick(View v) {
158+
switch (v.getId()) {
159+
case R.id.back_btn:
160+
finish();
161+
break;
162+
case R.id.pay_money_btn:
163+
Toast.makeText(this, "您一共消费了" + getTotal() + "元!", Toast.LENGTH_SHORT).show();
164+
break;
165+
case R.id.all_select_cb:
166+
boolean isChecked = ((CheckBox) v).isChecked();
167+
allSelectGoods(isChecked);
168+
shopCarListAdapter.notifyDataSetChanged();
169+
refreshTotal();
170+
break;
171+
default:
172+
}
173+
}
174+
175+
/**
176+
* 判断是否所有商品全为选中
177+
*/
178+
private boolean allGoodsIsAllSelect() {
179+
isAllSelect = true;
180+
for (GoodsInfo goods : goodsInfos) {
181+
isAllSelect = isAllSelect && goods.isSelect();
182+
}
183+
return isAllSelect;
184+
}
185+
186+
/**
187+
* 设置全选按钮的状态
188+
*/
189+
private void setAllSelectCbState() {
190+
allGoodsIsAllSelect();
191+
allSelectCb.setChecked(isAllSelect);
192+
}
193+
194+
/**
195+
* 全选或者取消全选商品
196+
*/
197+
private void allSelectGoods(boolean isSelect) {
198+
for (GoodsInfo goods : goodsInfos) {
199+
goods.setSelect(isSelect);
200+
}
201+
}
202+
203+
private List<GoodsInfo> getData() {
204+
for (int i = 0; i < 5; i++) {
205+
GoodsInfo goodsInfo = new GoodsInfo();
206+
goodsInfo.setCount(1);
207+
goodsInfo.setName("这是最牛逼的手机 iPhone X");
208+
goodsInfo.setPrice(10);
209+
goodsInfo.setType("深空灰");
210+
goodsInfo.setSelect(false);
211+
goodsInfos.add(goodsInfo);
212+
}
213+
return goodsInfos;
214+
}
215+
216+
}

app/src/main/java/com/example/strivecheng/appdragview/adapter/AllAppItemAdapter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,20 @@ protected void convertHead(BaseViewHolder helper, AllAppSection item) {
4949
protected void convert(BaseViewHolder helper, AllAppSection item) {
5050
AppInfo appInfo = item.t;
5151
helper.setText(R.id.app_info_title_tv,appInfo.getTitle());
52-
helper.setImageResource(R.id.app_info_icon_iv,appInfo.getImage());
52+
helper.setImageResource(R.id.app_info_icon_iv,appInfo.getImage())
53+
.addOnClickListener(R.id.info_rl);
5354
if (isEditModel) {
5455
if (appInfo.isSelect()) {
5556
helper.setImageResource(R.id.small_delete_iv,R.mipmap.delete_icon);
5657
}else {
5758
helper.setImageResource(R.id.small_delete_iv,R.mipmap.add_icon);
5859
}
59-
helper.setBackgroundColor(R.id.info_rv,ActivityCompat.getColor(mContext,R.color.bgLightGray));
60+
helper.setBackgroundColor(R.id.info_rl,ActivityCompat.getColor(mContext,R.color.bgLightGray));
6061
helper.addOnClickListener(R.id.small_delete_iv);
6162
helper.setGone(R.id.small_delete_iv,true);
6263
}else {
6364
helper.setGone(R.id.small_delete_iv,false);
64-
helper.setBackgroundColor(R.id.info_rv,ActivityCompat.getColor(mContext,R.color.bgWhite));
65+
helper.setBackgroundColor(R.id.info_rl,ActivityCompat.getColor(mContext,R.color.bgWhite));
6566

6667
}
6768

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.example.strivecheng.appdragview.adapter;
2+
3+
import android.widget.CheckBox;
4+
5+
import com.chad.library.adapter.base.BaseQuickAdapter;
6+
import com.chad.library.adapter.base.BaseViewHolder;
7+
import com.example.strivecheng.appdragview.R;
8+
import com.example.strivecheng.appdragview.data.GoodsInfo;
9+
10+
import java.util.List;
11+
12+
/**
13+
* Created by xingcc on 2018/8/9.
14+
* main function
15+
*
16+
* @author strivecheng
17+
*/
18+
19+
public class ShopCarListAdapter extends BaseQuickAdapter<GoodsInfo,BaseViewHolder> {
20+
public ShopCarListAdapter(List<GoodsInfo> data) {
21+
super(R.layout.shop_car_list_item,data);
22+
}
23+
24+
@Override
25+
protected void convert(BaseViewHolder helper, final GoodsInfo item) {
26+
helper.setText(R.id.goods_name_tv,item.getName())
27+
.setText(R.id.goods_price_tv,"¥"+item.getPrice()+"")
28+
.setText(R.id.goods_count_et,item.getCount()+"")
29+
.setText(R.id.goods_type_tv,item.getType())
30+
.addOnClickListener(R.id.add_goods_iv)
31+
.addOnClickListener(R.id.sub_goods_iv)
32+
.addOnClickListener(R.id.delete_btn)
33+
.addOnClickListener(R.id.select_cb);
34+
35+
CheckBox checkBox = helper.getView(R.id.select_cb);
36+
checkBox.setChecked(item.isSelect());
37+
// .setOnCheckedChangeListener(R.id.select_cb, new CompoundButton.OnCheckedChangeListener() {
38+
// @Override
39+
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
40+
// buttonView.setChecked(!isChecked);
41+
// item.setSelect(!isChecked);
42+
// }
43+
// });
44+
45+
}
46+
47+
// public void refreshCountTv(int count){
48+
//
49+
// }
50+
}

0 commit comments

Comments
 (0)