git代碼跳轉(zhuǎn) https://github.com/RadinLy/ShopCartdDmo
如何避免這兩種控件的復(fù)用讼庇,1灸拍,把Checkbox加入到Map集合中做祝,通過(guò)Map集合對(duì)控件進(jìn)行操作,2鸡岗,把每個(gè)條目布局中的商品數(shù)量加入到Bean類(lèi)中混槐。
checkbox加入到map中有何好處,首先轩性,你購(gòu)物車(chē)不可能是只是在adapter中記錄選中消息声登,你activity中可能會(huì)存在全選操作,那么,你在Activity中如何快速修改全部的選中狀態(tài)悯嗓,那么就直接循環(huán)一下map集合件舵,并刷新adapter,就可以完成。
條目布局中的數(shù)量加入到Bean類(lèi)脯厨,傳遞到下一個(gè)訂單支付界面铅祸,可以直接通過(guò)Bean類(lèi)傳遞一個(gè)序列化類(lèi),就不會(huì)做太多的循環(huán)查詢數(shù)據(jù)操作了合武。
接下來(lái)是
不詳細(xì)解釋原因临梗,場(chǎng)景: 購(gòu)物車(chē)
KXWH5TVFD11X@2Z27U0UCEL.png
復(fù)用的可能不止一個(gè)Checkbox,還有選擇商品數(shù)量的TextView稼跳,
具體如何避免盟庞,如下
1.給CheckBox創(chuàng)建一個(gè)Map集合,把商品數(shù)量放入Bean類(lèi)中
//創(chuàng)建Checkbox集合
private Map<Integer, Boolean> map = new HashMap<>();
2.創(chuàng)建子線程汤善,接收商品數(shù)量改變等操作
//創(chuàng)建子線程什猖,根據(jù)適配器中加減點(diǎn)擊按鈕傳遞給主界面
Handler mHandler = new Handler() {
public void handleMessage( Message msg) {
switch (msg.what) {
case 100://減 購(gòu)物車(chē)商品數(shù)量修改
// httpList();
CartLstBean cart = (CartLstBean) msg.obj;
httpChangeNum(cart.getGoodid(), cart.getType(), cart.getShop_id(), cart.getId(), cart.getNum());
break;
case 200://加 購(gòu)物車(chē)商品數(shù)量修改
CartLstBean carta = (CartLstBean) msg.obj;
httpChangeNum(carta.getGoodid(), carta.getType(), carta.getShop_id(), carta.getId(), carta.getNum());
break;
case 300:
ChangeMoney();
break;
case 400://購(gòu)物車(chē)商品刪除
final CartLstBean cartd = (CartLstBean) msg.obj;
new AlertStyleDialog(getActivity(), "", "確認(rèn)刪除該商品?", true, 0, new AlertStyleDialog.OnDialogButtonClickListener() {
@Override
public void onDialogButtonClick(int requestCode, boolean isPositive) {
if (isPositive) {
httpDelCart(cartd.getGoodid(), cartd.getType(), cartd.getShop_id(), cartd.getId());
}
}
}).show();
break;
}
}
};
//初始化適配器,根據(jù)自己創(chuàng)建適配器進(jìn)行相應(yīng)修改
AdapterUtil adapterUtil = new AdapterUtil<>();
mAdapter = adapterUtil.getCartLstAdapter(getActivity(), mList, mHandler, map);
lvDisplay.setAdapter(mAdapter);
3.接下來(lái)就是適配器中的相應(yīng)操作萎津,
/**
* 商品評(píng)論列表
*/
public QuickAdapter<T> getCartLstAdapter(final Activity activity, List<T> list, final Handler handler, final Map<Integer, Boolean> map) {
return new QuickAdapter<T>(activity, R.layout.item_cartlst, list) {
@Override
protected void convert(final BaseAdapterHelper helper, T item) {
final CartLstBean o = (CartLstBean) item;
//刪除商品按鈕
ImageView del = (ImageView) helper.getView().findViewById(R.id.cartlst_del);
del.setTag(helper.getPosition());//設(shè)置標(biāo)簽
//數(shù)量
final TextView tvHasNum = (TextView) helper.getView().findViewById(R.id.tv_has_num);
//減
final TextView tvSubtract = (TextView) helper.getView().findViewById(R.id.tv_subtract);
tvSubtract.setTag(helper.getPosition());//設(shè)置標(biāo)簽
//加
final TextView tvAdd = (TextView) helper.getView().findViewById(R.id.tv_add);
tvAdd.setTag(helper.getPosition());
//根據(jù)網(wǎng)絡(luò)請(qǐng)求獲取到商品數(shù)量卸伞,判斷減號(hào)按鈕是否顯示亮色
if (NumUtil.getInt(GetTextForViewUtil.getText(tvHasNum)) <= 1) {
tvHasNum.setText("1");
tvHasNum.setEnabled(false);
Drawable drawable = activity.getResources().getDrawable(R.mipmap.btn_subtract_normal);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //設(shè)置邊界
tvSubtract.setCompoundDrawables(null, null, drawable, null);//畫(huà)在右邊
tvSubtract.setPadding(0, 0, 14, 0);
} else {
Drawable drawable = activity.getResources().getDrawable(R.mipmap.btn_subtract_pressed);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //設(shè)置邊界
tvSubtract.setPadding(20, 0, 0, 0);
tvSubtract.setCompoundDrawables(drawable, null, null, null);//畫(huà)在左邊
}
//Checkbox按鈕
CheckBox choose = (CheckBox) helper.getView().findViewById(R.id.check_quanxuan);
choose.setTag(helper.getPosition());//設(shè)置標(biāo)簽
//根據(jù)傳遞進(jìn)來(lái)的map集合判斷當(dāng)前position的item是否為選中
boolean isCheck = map.get(helper.getPosition()) == null ? false : map.get(helper.getPosition());
choose.setChecked(isCheck);
//接下來(lái)就是Checkbox的選中事件了
choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//根據(jù)之前設(shè)置的標(biāo)簽,來(lái)獲取到點(diǎn)擊事件選中的是第幾個(gè)item
int pos = (int) v.getTag();
boolean isCheck;
isCheck = map.get(pos) == null ? false : map.get(pos);//獲取當(dāng)前item的選中狀態(tài)
if (!isCheck) {//如果是未選中锉屈,則在map集合中加入當(dāng)前position中的item為選中
map.put(pos, true);
} else {//如果是已選中荤傲,則去除map集合中的當(dāng)前位置選中狀態(tài)
map.remove(pos);
}
//像view界面?zhèn)鬟f消息,修改金額已經(jīng)刷新適配器
handler.sendEmptyMessageDelayed(300, 0);
notifyDataSetChanged();
}
});
tvSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message msg = new Message();
if (Integer.parseInt(v.getTag() + "") == helper.getPosition()) {
tvHasNum.setText((NumUtil.getInt(GetTextForViewUtil.getText(tvHasNum)) - 1) + "");
if (NumUtil.getInt(GetTextForViewUtil.getText(tvHasNum)) < 1) {
msg.what = 400;
tvHasNum.setText("1");
tvHasNum.setEnabled(false);
Drawable drawable = activity.getResources().getDrawable(R.mipmap.btn_subtract_normal);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //設(shè)置邊界
tvSubtract.setCompoundDrawables(null, null, drawable, null);//畫(huà)在右邊
tvSubtract.setPadding(0, 0, 14, 0);
} else {
Drawable drawable = activity.getResources().getDrawable(R.mipmap.btn_add_pressed);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //設(shè)置邊界
tvAdd.setCompoundDrawables(drawable, null, null, null);//畫(huà)在左邊
msg.what = 100;
}
}
//設(shè)置金額傳入bean類(lèi)中
o.setNum(GetTextForViewUtil.getText(tvHasNum));
msg.obj = o;
//點(diǎn)擊一次按鈕颈渊,發(fā)送一次消息給view遂黍,并刷新金額
handler.sendMessage(msg);
}
});
tvAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvHasNum.setText((NumUtil.getInt(GetTextForViewUtil.getText(tvHasNum)) + 1) + "");
if (NumUtil.getInt(GetTextForViewUtil.getText(tvHasNum)) > 1) {
tvHasNum.setEnabled(true);
Drawable drawable = activity.getResources().getDrawable(R.mipmap.btn_subtract_pressed);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //設(shè)置邊界
tvSubtract.setCompoundDrawables(null, null, drawable, null);//畫(huà)在右邊
tvSubtract.setPadding(0, 0, 14, 0);
}
o.setNum(GetTextForViewUtil.getText(tvHasNum));
Message msg = new Message();
msg.what = 200;
msg.obj = o;
handler.sendMessage(msg);
}
});
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Integer.parseInt(view.getTag() + "") == helper.getPosition()) {
Message msg = new Message();
msg.what = 400;
msg.obj = o;
handler.sendMessage(msg);
}
}
});
}
};
}
主要代碼就如上所示,解決如上問(wèn)題俊嗽,這些代碼也就足夠了雾家。
喜歡的話點(diǎn)個(gè)贊,謝謝