仿饿了么购物车下单效果
前一段由于新项目需要,开发一个类似饿了么购物车下单效果,电商类、外卖类、点餐类项目都可以用的上,废话不多说请看效果。 效果图如下:

主要的功能: 就是左侧展示分类,右侧展示分类下商品的,点击右侧分类下的商品,如果商品是套餐类型的话,点击可以看套餐详情,下单选择完商品后,可以在购物车里面添加或减少商品数量。
主要功能实现: 1:分类及商品及购物车里面商品数量的联动效果 2:底部购物车商品列表 3:选择左侧分类效果 4:添加商品是有0到1,减少商品有1到0动画效果 5:下单动画
1:分类及商品及购物车里面商品数量的联动效果
商品的分类和商品展示分别是两个列表,每一个商品的数量取商品的number、商品分类是取每一个分类下的商品list里面遍历商品取商品数量之和。然后商品列表的 GoodsAdapter里面有一个参数传的是商品分类的CatograyAdapter,当下单时减少或则添加商品时,都会调 MainActivity的handlerCarNum方法。 减少时type传0,添加时传1。通过一个SparseArray,key是商品的product_id,value为商品的下单个数。1:当添加商品都会传入一个商品对象goodsBean,通过product_id取到这个商品对象,如果商品对象为空时,给传入的对象下单数量goodsBean.setNum(1),否则goodsBean.setNum(++i)。2:当减少商品时,通过product_id取到这个商品对象,通过对象取对象的下单数量如果小与2的时候goodsBean.setNum(0),并将该对象从SparseArray中remove否goodsBean.setNum(--i)。最后调update方法更新GoodsAdapter,CatograyAdapter.
1 public void handlerCarNum(int type, GoodsBean goodsBean, boolean refreshGoodList){
2 if (type == 0) {
3 GoodsBean temp = selectedList.get(goodsBean.getProduct_id());
4 if(temp!=null){
5 if(temp.getNum()<2){
6 goodsBean.setNum(0);
7 selectedList.remove(goodsBean.getProduct_id());
8 }else{
9 int i = goodsBean.getNum();
10 goodsBean.setNum(--i);
11 }
12 }
13 } else if (type == 1) {
14 GoodsBean temp = selectedList.get(goodsBean.getProduct_id());
15 if(temp==null){
16 goodsBean.setNum(1);
17 selectedList.append(goodsBean.getProduct_id(), goodsBean);
18 }else{
19 int i= goodsBean.getNum();
20 goodsBean.setNum(++i);
21 }
22 }
23 update(refreshGoodList);
24 }
1 //刷新布局 总价、购买数量等
2 private void update(boolean refreshGoodList){
3 int size = selectedList.size();
4 int count =0;
5 for(int i=0;i<size;i++){
6 GoodsBean item = selectedList.valueAt(i);
7 count += item.getNum();
8 totleMoney += item.getNum()*Double.parseDouble(item.getPrice());
9 }
10 tv_totle_money.setText("¥"+String.valueOf(df.format(totleMoney)));
11 totleMoney = 0.00;
12 if(count<1){
13 bv_unm.setVisibility(View.GONE);
14 }else{
15 bv_unm.setVisibility(View.VISIBLE);
16 }
17
18 bv_unm.setText(String.valueOf(count));
19
20 if(productAdapter!=null){
21 productAdapter.notifyDataSetChanged();
22 }
23
24 if(goodsAdapter!=null){
25 goodsAdapter.notifyDataSetChanged();
26 }
27
28 if(catograyAdapter!=null){
29 catograyAdapter.notifyDataSetChanged();
30 }
31
32 if(bottomSheetLayout.isSheetShowing() && selectedList.size()<1){
33 bottomSheetLayout.dismissSheet();
34 }
35 }
2:底部购物车商品列表
点击购物车查看购物车列表时,就是给含有下单商品的HashMap作为参数传入ProductAdapter中,在购物车中添加或减少商品数量同样是调 MainActivity的handlerCarNum方法。 除此之外,购物车列表弹起的效果可以用一个bottomsheet效果。as直接集成 compile'com.flipboard:bottomsheet-core:1.5.1'即可。
3:选择左侧分类效果
由于选择分类时,既要改变分类的背景色也要改变选择分类文字的颜色,就不能直接在xml里面设置了,需要在getview里面根据外面传进来选择的selection与position比较后设置。
1 public void setSelection(int selection) {
2 this.selection = selection;
3 }
1 if (position == selection) {
2 viewholder.tv_catogray.setBackgroundResource(R.drawable.rec_red_left_stroke);
3 viewholder.tv_catogray.setTextColor(context.getResources().getColor(R.color.black));
4 } else {
5 viewholder.tv_catogray.setBackgroundResource(R.drawable.empty);
6 viewholder.tv_catogray.setTextColor(context.getResources().getColor(R.color.gray));
7 }
4:添加商品是有0到1,减少商品有1到0动画效果
其实动画效果做起来也不是特别复杂,首先要搞清android的几种动画效果,分为在Android3.0(即API Level11)以前,Android仅支持2种动画:分别是Frame Animation(逐帧动画)和Tween Animation(补间动画),在3.0之后Android支持了一种新的动画系统,称为:Property Animation(属性动画)。 最常用的补间动画最常用的4中效果如下: 渐变透明度动画效果 渐变尺寸伸缩动画效果 画面转换位置移动动画效果 画面转移旋转动画效果
添加商品是有0到1,减少商品有1到0动画效果其实就是利用了translate、rotate水平位移和旋转动画效果。
1 //显示减号的动画
2 private Animation getShowAnimation(){
3 AnimationSet set = new AnimationSet(true);
4 RotateAnimation rotate = new RotateAnimation(0,720,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
5 set.addAnimation(rotate);
6 TranslateAnimation translate = new TranslateAnimation(
7 TranslateAnimation.RELATIVE_TO_SELF,2f
8 ,TranslateAnimation.RELATIVE_TO_SELF,0
9 ,TranslateAnimation.RELATIVE_TO_SELF,0
10 ,TranslateAnimation.RELATIVE_TO_SELF,0);
11 set.addAnimation(translate);
12 AlphaAnimation alpha = new AlphaAnimation(0,1);
13 set.addAnimation(alpha);
14 set.setDuration(500);
15 return set;
16 }
17 //隐藏减号的动画
18 private Animation getHiddenAnimation(){
19 AnimationSet set = new AnimationSet(true);
20 RotateAnimation rotate = new RotateAnimation(0,720,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
21 set.addAnimation(rotate);
22 TranslateAnimation translate = new TranslateAnimation(
23 TranslateAnimation.RELATIVE_TO_SELF,0
24 ,TranslateAnimation.RELATIVE_TO_SELF,2f
25 ,TranslateAnimation.RELATIVE_TO_SELF,0
26 ,TranslateAnimation.RELATIVE_TO_SELF,0);
27 set.addAnimation(translate);
28 AlphaAnimation alpha = new AlphaAnimation(1,0);
29 set.addAnimation(alpha);
30 set.setDuration(500);
31 return set;
32 }
5:下单动画
下单动画首先要获取添加商品时的初始位置坐标,通过getLocationInWindow方法获取初始位置的心x,y坐标,然后获取添加商品要消失的地方的坐标,一般都选择购物车logo的坐标。然后计算x,y的位移值后,通过TranslateAnimation转换移动效果,x,y同时移动就实现了抛物线的效果。最后不要忘记设置当动画结束的时候隐藏抛过来的小图标。
1 public void setAnim(final View v, int[] startLocation) {
2 anim_mask_layout = null;
3 anim_mask_layout = createAnimLayout();
4 anim_mask_layout.addView(v);//把动画小球添加到动画层
5 final View view = addViewToAnimLayout(anim_mask_layout, v, startLocation);
6 int[] endLocation = new int[2];// 存储动画结束位置的X、Y坐标
7 tv_car.getLocationInWindow(endLocation);
8 // 计算位移
9 int endX = 0 - startLocation[0] + 40;// 动画位移的X坐标
10 int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标
11
12 TranslateAnimation translateAnimationX = new TranslateAnimation(0,endX, 0, 0);
13 translateAnimationX.setInterpolator(new LinearInterpolator());
14 translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
15 translateAnimationX.setFillAfter(true);
16
17 TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
18 translateAnimationY.setInterpolator(new AccelerateInterpolator());
19 translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
20 translateAnimationY.setFillAfter(true);
21
22 AnimationSet set = new AnimationSet(false);
23 set.setFillAfter(false);
24 set.addAnimation(translateAnimationY);
25 set.addAnimation(translateAnimationX);
26 set.setDuration(800);// 动画的执行时间
27 view.startAnimation(set);
28 // 动画监听事件
29 set.setAnimationListener(new Animation.AnimationListener() {
30 // 动画的开始
31 @Override
32 public void onAnimationStart(Animation animation) {
33 v.setVisibility(View.VISIBLE);
34 }
35
36 @Override
37 public void onAnimationRepeat(Animation animation) {
38 // TODO Auto-generated method stub
39 }
40
41 // 动画的结束
42 @Override
43 public void onAnimationEnd(Animation animation) {
44 v.setVisibility(View.GONE);
45 }
46 });
47
48 }
最后肯定少不了源码下载地址:shopcar
转载于:https://www.cnblogs.com/huolongluo/p/6214284.html