自定義加減視圖
public class AddDelView extends LinearLayout {
private OnNumChangedListener onNumChangedListener;
private View rootView;
private ImageView iv_add;
private ImageView iv_del;
private TextView tv_num;
public AddDelView(Context context) {
this(context,null);
}
public AddDelView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,-1);
}
public AddDelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
initListener();
}
private void initView(Context context) {
rootView = LayoutInflater.from(context).inflate(R.layout.add_del_butn, this);
iv_add =rootView.findViewById(R.id.iv_add);
iv_del = rootView.findViewById(R.id.iv_del);
tv_num = rootView.findViewById(R.id.tv_num_text);
tv_num.setText("1");
}
private void initListener() {
iv_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
add();
}
});
iv_del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sub();
}
});
}
private void add() {
String cu = tv_num.getText().toString();
int parseInt = Integer.parseInt(cu);
parseInt++;
setCurentCount(parseInt);
getTotal();
}
private void sub() {
String cu = tv_num.getText().toString();
int parseInt = Integer.parseInt(cu);
if (parseInt > 1) {
parseInt--;
setCurentCount(parseInt);
} else {
Toast.makeText(getContext(),"不能再少了親",Toast.LENGTH_SHORT).show();
}
getTotal();
}
public int getCurentCount() {
return Integer.parseInt(tv_num.getText().toString());
}
public void setOnNumChangedListener(OnNumChangedListener onNumChangedListener) {
this.onNumChangedListener = onNumChangedListener;
}
public void setCurentCount(int num) {
tv_num.setText(num + "");
if (onNumChangedListener != null) {
onNumChangedListener.onNumChanged(this, num);
}
}
public interface OnNumChangedListener {
void onNumChanged(View view, int curNum);
}
}
自定義加減布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/minus_circle" />
<TextView
android:id="@+id/tv_num_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/shopcart_add_btn"
android:paddingBottom="2dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="2dp"
android:text="1" />
<ImageView
android:id="@+id/iv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:src="@drawable/plus_circle" />
</LinearLayout>
自定義加減的調用
private AddDelView.OnNumChangedListener onNumChangedListener;
public void setOnNumChangedListener(AddDelView.OnNumChangedListener onNumChangedListener) {
this.onNumChangedListener = onNumChangedListener;
}
myViewHolder.addDelView.setOnNumChangedListener(new AddDelView.OnNumChangedListener() {
@Override
public void onNumChanged(View view, int curNum) {
listData.get(i).setNum(curNum);
if (onNumChangedListener != null) {
onNumChangedListener.onNumChanged(view, curNum);
}
}
});
提供為加減的計算的方法
public static void getTotal() {
int num = 0 ;
double total = 0;
for (int i = 0; i < data.size(); i++) {
List<GoodsDataBean.DataBean.ListBean> listBeans = data.get(i).getList();
for (int j = 0; j < listBeans.size(); j++) {
GoodsDataBean.DataBean.ListBean listchild = listBeans.get(j);
if (listchild.getSelected2() == 1) {
double price = listchild.getPrice();
total += price * listchild.getNum();
num +=listchild.getNum();
}
}
}
mTvPrice.setText(total+"剛剛添加的");
mTvNum.setText(num+"");
}
通過選中方法來實作加減的操作
- 首先改變全選的計算(抽出checked方法)
b為第一層的選中,p為子資訊的選中
private void checked(boolean b, int p) {
List<GoodsDataBean.DataBean> list = goodsHomeAdapter.getList();
for (int i = 0; i <list.size() ; i++) {
GoodsDataBean.DataBean dataBean = list.get(i);
dataBean.setSelected(b);
List<GoodsDataBean.DataBean.ListBean> listBeans = dataBean.getList();
for (int j = 0; j < listBeans.size(); j++) {
GoodsDataBean.DataBean.ListBean bean = listBeans.get(j);
bean.setSelected2(p);
}
}
}
改變選中狀态以此來實作計算的狀态
mCheckbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checked(true,1);
getTotal();
}else{
checked(false, 0);
mTvNum.setText("結算(" + 0 + ")");
mTvPrice.setText(0 + "");
}
goodsHomeAdapter.notifyDataSetChanged();
}
});
針對對于第一層的選中狀态監聽
myViewHodler.checkBox.setChecked(dataBean.isSelected());
myViewHodler.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
List<GoodsDataBean.DataBean.ListBean> listData = myViewHodler.goodsChildAdapter.getListData();
for (int j = 0; j < listData.size(); j++) {
GoodsDataBean.DataBean.ListBean bean = listData.get(j);
bean.setSelected2(1);
}
getTotal();
}else{
List<GoodsDataBean.DataBean.ListBean> listData = myViewHodler.goodsChildAdapter.getListData();
for (int j = 0; j < listData.size(); j++) {
GoodsDataBean.DataBean.ListBean bean = listData.get(j);
bean.setSelected2(0);
}
getTotal();
}
myViewHodler.goodsChildAdapter.notifyDataSetChanged();
}
});
設定子類視圖複選框的選中狀态
checkBox.setChecked(listData.get(position).getSelected2()==1);
設定改變子類視圖的複選框監聽狀态
myViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
listData.get(i).setSelected2(1);
getTotal();
}else {
listData.get(i).setSelected2(0);
getTotal();
}
}
});
至此關于RecyclerView嵌套RecyclerView的購物車功能實作,便簡易設定完成了