作者:墨镜小辣妹 | 来源:互联网 | 2023-09-09 10:57
1_商品总价格计算①在GovaffairPager类中设置adapternewGovaffairPagerAdapter(context,datas,checkbox_all
1_商品总价格计算
①在GovaffairPager类中设置
adapter = new GovaffairPagerAdapter(context,datas,checkbox_all,tv_total_price);
②GovaffairPagerAdapter
/**
* 显示总商品价格
*/
private void showTotalPrice(){
tv_total_price.setText("合计¥"+getTotalPrice());
}
/**
* 计算购物车里面所选商品总架构
* @return
*/
private double getTotalPrice(){
double num = 0;
if(datas != null && datas.size() >0){
for(int i=0;i datas) {
this.cOntext= context;
this.datas = datas;
cartProvider = new CartProvider(context);
showTotalPrice();
}
2_增加商品或者减少商品的时候计算总价格
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ShoppingCart cart = datas.get(position);
......
holder.numberAddSubView.setOnButtonClickListenter(new NumberAddSubView.OnButtonClickListenter() {
@Override
public void onButtonAddClick(View view, int value) {
cart.setCount(value);
cartProvider.update(cart);
showTotalPrice();
}
@Override
public void onButtonSubClick(View view, int value) {
cart.setCount(value);
cartProvider.update(cart);
showTotalPrice();
}
});
}
3_设置点击某一条item
1_先定义接口和调用
//设置点击某个item的监听
public interface OnItemClickListener{
void onItemClick(View view,int position);
}
private OnItemClickListener onItemClickListener;
/**
* 设置某条的监听
* @param onItemClickListener
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.OnItemClickListener= onItemClickListener;
}
2_调用接口
class ViewHolder extends RecyclerView.ViewHolder {
private CheckBox checkbox;
.................
public ViewHolder(View itemView) {
super(itemView);
checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
......
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListener != null){
onItemClickListener.onItemClick(v,getLayoutPosition());
}
}
});
}
}
3_在构造方法中设置监听
public GovaffairPagerAdapter(final Context context, final List datas) {
this.cOntext= context;
this.datas = datas;
cartProvider = new CartProvider(context);
showTotalPrice();
setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//1.设置点击item的状态
ShoppingCart ShoppingCart = datas.get(position);
shopingCart.setIsChecked(!shopingCart.isChecked());
notifyItemChanged(position);
//2.设置全选和非全选
checkListener();
//3.显示总价格
showTotalPrice();
// Toast.makeText(context,"选中了"+position,Toast.LENGTH_SHORT).show();
}
});
}
/**
* 全选的监听
*/
private void checkListener() {
int num = 0;
if(datas != null && datas.size()>0){
for(int i=0;i
4_全选和反选
//把CheckBox 和 TextView 传入到适配器中
private final CheckBox checkbox_all;
private final TextView tv_total_price;
private CartProvider cartProvider;
public GovaffairPagerAdapter(final Context context, final List datas, final CheckBox checkbox_all, TextView tv_total_price) {
this.cOntext= context;
this.datas = datas;
this.checkbox_all = checkbox_all;
this.tv_total_price = tv_total_price;
..........................
checkbox_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//全选和反选
checkAll_none(checkbox_all.isChecked());
showTotalPrice();
}
});
}
/**
* 全选和反选
* @param ischeck
*/
private void checkAll_none(boolean ischeck){
if(datas != null && datas.size() >0){
for(int i=0;i