編輯:關於android開發
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.size();i++){ ShoppingCart cart = datas.get(i); //判斷是否選中的商品 if(cart.isChecked()){ num = num + cart.getPrice()*cart.getCount(); } } } return num; } //在構造方法中調用 public GovaffairPagerAdapter(final Context context, final List<ShopingCart> 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<ShopingCart> 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<datas.size();i++){ ShoppingCart cart = datas.get(i); //只要有一個沒有被選中就把全選設置為未勾選 if(!cart.isChecked()){ checkbox_all.setChecked(false); }else{ num += 1; } } if(datas.size()==num){ checkbox_all.setChecked(true); } } }
4_全選和反選
//把CheckBox 和 TextView 傳入到適配器中 private final CheckBox checkbox_all; private final TextView tv_total_price; private CartProvider cartProvider; public GovaffairPagerAdapter(final Context context, final List<ShopingCart> 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<datas.size();i++){ ShoppingCart cart = datas.get(i); cart.setIsChecked(ischeck); notifyItemChanged(i); } } } //在GovaffairPager類中設置 adapter = new GovaffairPagerAdapter(context,datas,checkbox_all,tv_total_price);
Android進階——聲波振幅顯示,android進階振幅最近博主想做一個app,中間有一個是錄音的功能。於是博主想把UI做的好看一些,想仿照微信或者QQ語音輸入時,能夠
上一節中講了Fragment概述及用法,本節繼續講解Fragment的創建和生
使用數據源碼解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
GsonFormat插件從配置到使用,gsonformat插件配置說明:目前多數服務器端都以json格式返回,那麼相對應的解析時建立的實體類如果你還在自己挨個寫的話,那就