android 实现仿美团点菜和京东分类导航

 欢迎大家进群:574605026   开启我们的开发之旅
  
废话不多说,看图说话


当看到标题的时候,每个人都想知道,到底是个什么样子呢?会不会和我想的不一样呢,我会不会有上当呢,看到图之后大家就知道,是实现的一个什么功能了。
一、首先给大家讲解下实现的方式,其实不是很难。
我先给大家看个我画的草图,画的不好,大家勿喷

这张草图是我在写这个的时候,简单的一个思路,看图,应该都能看懂
1、布局,左右分别是listview,我用left和right标识
2、事件,左边listview点击事件(onitemonclick)改变右边的listview的滚动位置,其次右边listview滚动事件(onscrollListener)改变左边listview的位置。
3、右边title的变化,点击左边listview改变,右边滚动到type的时候改变
4、点击加减号的抛物线动画  可以参考我的博客http://blog.csdn.net/mingzhnglei/article/details/54342644
5、购物车的时间,popuview。
6、数据在这里我是用的数据库存储

这么一分析,哎呀,其实也没有那么难吗?对不对。好吧,确实没有那么难
现在看代码
package com.sxwz.myapplication.fragment;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.sxwz.myapplication.R;
import com.sxwz.myapplication.adapter.LeftAdapter;
import com.sxwz.myapplication.entity.Cart;
import com.sxwz.myapplication.entity.Product;
import com.sxwz.myapplication.utils.Parser;
import com.sxwz.myapplication.utils.UIHelper;
import com.sxwz.myapplication.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/*****************************************************
 * author:      wz
 * email:       wangzhong0116@foxmail.com
 * version:     1.0
 * date:        2017/1/10 11:54
 * description:
 *****************************************************/

public class ProductFragment extends Fragmentimplements SectionIndexer, View.OnClickListener {private ListView rightListView;          //右侧商品listview
    private ListView leftListView;   //左侧--商品类型listview
    private RightAdapter rightAdapter;   //右侧adapter
    private LeftAdapter leftAdapter;  //左侧adapter
    private ImageView shopCart;//购物车
    private View cartFrame;
    private TextView buyNumView;//购物车上的数量标签
    private TextView mPriceSumView;
    //private View cartView;
    private TextView selectedView;
    private View titleLayout;
    private TextView title;
    private ListView popuListView;
    /**
     * 上次第一个可见元素,用于滚动时记录标识。
     */
    private int lastFirstVisibleItem = -1;
    private List<Product> mProductList;
    private List<Cart> mCartList = new ArrayList<Cart>();

    private ViewGroup anim_mask_layout;//动画层
    private int buyNum = 0;//购买数量

    private int count = 0;
    private double priceSum = 0.0;

    /**
     * 在这里虚构个商家id,其主要目的是不想让大家走更多的弯路
     */
    private int businessId = 0;
    /**
     * 上次选中的左侧菜单
     */
    private View lastView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.product_list, null);
        initView(view);
        initClick();
        initData();
        return view;
    }private void initView(View view) {titleLayout = view.findViewById(R.id.title_layout);
        title = (TextView) view.findViewById(R.id.title_layout_catalog);
        rightListView = (ListView) view.findViewById(R.id.menu_lvmenu);
        shopCart = (ImageView) view.findViewById(R.id.iv_add_cart);//购物车
        cartFrame = view.findViewById(R.id.cart_frame);
        buyNumView = (TextView) view.findViewById(R.id.tv_count_price);//购物车上的数量标签
        mPriceSumView = (TextView) view.findViewById(R.id.price_sum_view);
        selectedView = (TextView) view.findViewById(R.id.selected_view);
        leftListView = (ListView) view.findViewById(R.id.side_menu_lv);
    }private void initClick() {cartFrame.setOnClickListener(this);
        selectedView.setOnClickListener(this);

        //左侧类型点击对应右侧商品
        leftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {if (lastView != null) {//上次选中的view变回灰色
                    lastView.setBackgroundColor(getResources().getColor(R.color.frament_tab_color));
                }//设置选中颜色为白色
                view.setBackgroundColor(getResources().getColor(R.color.white));
                //点击左侧,右侧滚动到对应的位置
                TextView section_tv = (TextView) view.findViewById(R.id.section_tv);
                int location = rightAdapter.getPositionForSection((Integer.parseInt(section_tv.getText().toString())));
                if (location != -1) {rightListView.setSelection(location);
                }lastView = view;
            }});
    }/**
     * 初始化数据
     */
    private void initData() {mProductList = Parser.getCateProductList(getActivity());
        rightAdapter = new RightAdapter(getActivity(), mProductList);
        leftAdapter = new LeftAdapter(getActivity(), mProductList);
        rightListView.setAdapter(rightAdapter);
        leftListView.setAdapter(leftAdapter);

        rightListView.setOnScrollListener(mOnScrollListener);
        //此处为判断是否为空
//        if (mProductList==null){
//
//        }
    }/**
     * 右侧商品滑动对于左侧类型
     */
    private AbsListView.OnScrollListener mOnScrollListener = new AbsListView.OnScrollListener() {@Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {}@Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {try {//获取屏幕第一个item的section
                int section = getSectionForPosition(firstVisibleItem);
                int nextSection = 0;
                if (mProductList.size() > 1) {nextSection = getSectionForPosition(firstVisibleItem + 1);
                }int nextSecPosition = getPositionForSection(+nextSection);
                if (firstVisibleItem != lastFirstVisibleItem) {ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) titleLayout
                            .getLayoutParams();
                    params.topMargin = 0;
                    titleLayout.setLayoutParams(params);
                    title.setText(mProductList.get(getPositionForSection(section)).getType());
                    if (lastView != null) {lastView.setBackgroundColor(getResources().getColor(R.color.frament_tab_color));
                    }lastView = leftListView.getChildAt(section);
                    lastView.setBackgroundColor(getResources().getColor(R.color.white));

                }if (nextSecPosition == firstVisibleItem + 1) {View childView = view.getChildAt(0);
                    if (childView != null) {int titleHeight = titleLayout.getHeight();
                        int bottom = childView.getBottom();
                        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) titleLayout
                                .getLayoutParams();
                        if (bottom < titleHeight) {float pushedDistance = bottom - titleHeight;
                            params.topMargin = (int) pushedDistance;
                            titleLayout.setLayoutParams(params);
                        } else {if (params.topMargin != 0) {params.topMargin = 0;
                                titleLayout.setLayoutParams(params);
                            }}}}lastFirstVisibleItem = firstVisibleItem;
            } catch (NullPointerException e) {e.printStackTrace();
            } catch (Exception e) {e.printStackTrace();
            }}};

    @Override
    public Object[] getSections() {return null;
    }@Override
    public int getPositionForSection(int sectionIndex) {for (int i = 0; i < mProductList.size(); i++) {int section = mProductList.get(i).getSeleteId();
            if (section == sectionIndex) {return i;
            }}return -1;
    }@Override
    public int getSectionForPosition(int i) {return mProductList.get(i).getSeleteId();
    }/**
     * 创建动画层
     * @return
     */
    private ViewGroup createAnimLayout() {ViewGroup rootView = (ViewGroup) getActivity().getWindow().getDecorView();
        LinearLayout animLayout = new LinearLayout(getActivity());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setId(Integer.MAX_VALUE - 1);
        animLayout.setBackgroundResource(android.R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
    }private View addViewToAnimLayout(final ViewGroup parent, final View view,
                                     int[] location) {int x = location[0];
        int y = location[1];
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = x;
        lp.topMargin = y;
        view.setLayoutParams(lp);
        return view;
    }public void setAnim(final View v, int[] startLocation) {anim_mask_layout = null;
        anim_mask_layout = createAnimLayout();
        anim_mask_layout.addView(v);//把动画小球添加到动画层
        final View view = addViewToAnimLayout(anim_mask_layout, v,
                startLocation);
        int[] endLocation = new int[2];// 存储动画结束位置的X、Y坐标
        //TODO 这里需要进行判断购物车是否存在
        shopCart.getLocationInWindow(endLocation);// shopCart是那个购物车

        // 计算位移
        int endX = 0 - startLocation[0] + 20;// 动画位移的X坐标
        int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标
        System.out.println("=====x==="+endX);
        System.out.println("=====y==="+endY);
        TranslateAnimation translateAnimationX = new TranslateAnimation(0,
                endX, 0, 0);
        translateAnimationX.setInterpolator(new LinearInterpolator()); //让动画已均匀的速度改变
        translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true); //执行完毕,利用视图setLayoutParams来重新定位

        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
                0, endY);
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        AnimationSet set = new AnimationSet(false);
        set.setFillAfter(false);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);
        set.setDuration(800);// 动画的执行时间
        view.startAnimation(set);
        // 动画监听事件
        set.setAnimationListener(new Animation.AnimationListener() {// 动画的开始
            @Override
            public void onAnimationStart(Animation animation) {v.setVisibility(View.VISIBLE);
            }@Override
            public void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub
            }// 动画的结束
            @Override
            public void onAnimationEnd(Animation animation) {v.setVisibility(View.GONE);
                addCart();
            }});

    }private void addCart() {buyNum++;//让购买数量+1
        showSeleted();
    }public void removeCart() {buyNum--;//让购买数量-1
        showSeleted();
    }private void showSeleted() {if (buyNum > 0) {buyNumView.setVisibility(View.VISIBLE);
            buyNumView.setText(buyNum + "");
            mPriceSumView.setText("共¥:" + convert(priceSum));
            mPriceSumView.setTextColor(getResources().getColor(R.color.big_red));
            if (priceSum > 12) {selectedView.setEnabled(true);
                selectedView.setText("选好了");
                selectedView.setBackgroundResource(R.drawable.bg_choice_press_round);
            } else {selectedView.setEnabled(false);
                selectedView.setText("¥15起送");
                selectedView.setBackgroundResource(R.drawable.bg_choice_round);
            }shopCart.setImageResource(R.mipmap.cart13);
            cartFrame.setEnabled(true);
            //selectedView.setVisibility(View.VISIBLE);
        } else {mPriceSumView.setText("请点餐~");
            mPriceSumView.setTextColor(getResources().getColor(R.color.cart_choice_color));
            selectedView.setEnabled(false);
            selectedView.setText("¥15起送");
            selectedView.setBackgroundResource(R.drawable.bg_choice_round);
            shopCart.setImageResource(R.mipmap.cart12);
            buyNumView.setVisibility(View.GONE);
            if (popupWindow != null) {popupWindow.dismiss();
            }cartFrame.setEnabled(false);
            //selectedView.setVisibility(View.GONE);
        }}@Override
    public void onClick(View v) {switch (v.getId()) {case R.id.selected_view:break;
            case R.id.iv_add_cart:initPopWindow();
                break;
            case R.id.cart_frame:initPopWindow();
                break;
            case R.id.cart_clear_view://clear cart  === hide cart list

                break;
            default:break;
        }}popuwindow part
    private PopupWindow popupWindow;

    private void initPopWindow() {View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popu_cart, null);
        int height = Utils.getScreenHeight(getActivity()) - (cartFrame.getHeight()*2) + 10;
        popupWindow = new PopupWindow(contentView, Utils.getScreenWidth(getActivity()),
                height);
        popupWindow.setContentView(contentView);

        //cart view init
        View clearView = contentView.findViewById(R.id.cart_clear_view);
        clearView.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {UIHelper.clearCart(getActivity(), businessId);
                cartFrame.setEnabled(false);
                popupWindow.dismiss();
                rightAdapter.notifyDataSetChanged();
                leftAdapter.notifyDataSetChanged();
                priceSum = 0.0;
                buyNum = 0;
                showSeleted();
            }});
        popuListView = (ListView) contentView.findViewById(R.id.cart_list);
        getCart(popuListView);
        popupWindow.setFocusable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {@Override
            public boolean onTouch(View v, MotionEvent event) {return false;
            }});

        View empView = contentView.findViewById(R.id.content_view);
        empView.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {popupWindow.dismiss();
            }});
//        popupWindow.setOnDismissListener();

        ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.ban_tou));
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.getBackground().setAlpha(100);
        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        popupWindow.showAsDropDown(cartFrame);
    }private CartAdapter cartAdapter = null;

    private void getCart(ListView listView) {mCartList = UIHelper.getCartList(getActivity(), businessId);
        cartAdapter = new CartAdapter(getActivity(), mCartList);
        listView.setAdapter(cartAdapter);
    }private void updateCart(Product mProduct) {//TODO 这里需要进行判断购物车是否存在 如果存在   则显示动画,否则第一次不显示动画
        int productId = mProduct.getProductId();
        String name = mProduct.getFoodName();
        double price = mProduct.getFoodPrice();
        Cart cart = new Cart();
        cart.setName(name);
        cart.setProductId(productId);
        cart.setSaleCount(count);
        cart.setPrice(price);
        UIHelper.getQueryIdCart(getActivity(), cart, productId,businessId);
    }private void deleteProductId(int productId) {UIHelper.deleteCart(getActivity(), productId);
    }/product adapter

    class RightAdapter extends BaseAdapter implements SectionIndexer {private List<Product> list = null;
        private Context mContext;

        public RightAdapter(Context mContext, List<Product> list) {this.mContext = mContext;
            this.list = list;
        }@Override
        public int getCount() {// TODO Auto-generated method stub
            return this.list.size();
        }@Override
        public Object getItem(int position) {// TODO Auto-generated method stub
            return list.get(position);
        }@Override
        public long getItemId(int position) {// TODO Auto-generated method stub
            return position;
        }@Override
        public View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub
            final ViewProductHolder viewProductHolder;
            final Product mProduct = list.get(position);
            if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.product_item, null);
                TextView tvTag = (TextView) convertView.findViewById(R.id.cate_name);
                TextView tvSalecount = (TextView) convertView.findViewById(R.id.product_salecount_view);
                TextView tvTitle = (TextView) convertView.findViewById(R.id.product_name_view);
                ImageView productImage = (ImageView) convertView.findViewById(R.id.product_imageView);
                TextView countView = (TextView) convertView.findViewById(R.id.add_count_view);
                TextView tvPrice = (TextView) convertView.findViewById(R.id.product_price_view);
                ImageView removeProductImage = (ImageView) convertView.findViewById(R.id.delete_product_view);
                ImageView addProductImage = (ImageView) convertView.findViewById(R.id.add_product_view);
                viewProductHolder = new ViewProductHolder(tvTag, tvSalecount, tvTitle, productImage, countView, tvPrice, removeProductImage, addProductImage);

                convertView.setTag(viewProductHolder);
            } else {viewProductHolder = (ViewProductHolder) convertView.getTag();
            }//如果有该类型,则隐藏
            int section = getSectionForPosition(position);
            if (position == getPositionForSection(section)) {viewProductHolder.tvTag.setVisibility(View.VISIBLE);
                viewProductHolder.tvTag.setText(mProduct.getType());
            } else {viewProductHolder.tvTag.setVisibility(View.GONE);
            }viewProductHolder.tvTitle.setText(mProduct.getFoodName());
            viewProductHolder.tvPrice.setText("¥" + mProduct.getFoodPrice());
            viewProductHolder.tvSalecount.setText("已售" + mProduct.getSalesCount() + "份");

            int salecount = UIHelper.saleCount(getActivity(), mProduct.getProductId(), businessId);
            if (salecount > 0) {viewProductHolder.countView.setText(salecount + "");
                viewProductHolder.countView.setVisibility(View.VISIBLE);
                viewProductHolder.removeProductImage.setVisibility(View.VISIBLE);
            } else {viewProductHolder.countView.setText(0 + "");
                viewProductHolder.countView.setVisibility(View.GONE);
                viewProductHolder.removeProductImage.setVisibility(View.GONE);
            }//此处可以设置商品的图片========请在使用的过程中自行替换
            String url = mProduct.getImageUrl();
            viewProductHolder.productImage.setImageResource(R.mipmap.default_icon);

            viewProductHolder.removeProductImage.setOnClickListener(new View.OnClickListener() {@Override
                public void onClick(View v) {count = Integer.parseInt(viewProductHolder.countView.getText().toString());
                    count--;
                    priceSum = priceSum - mProduct.getFoodPrice();
                    if (count < 1) {viewProductHolder.removeProductImage.setVisibility(View.GONE);
                        viewProductHolder.countView.setVisibility(View.GONE);
                    }viewProductHolder.countView.setText(count + "");
                    mProduct.setCount(count);
                    if (count == 0) {deleteProductId(mProduct.getProductId());
                    } else {updateCart(mProduct);
                    }removeCart();
                    leftAdapter.notifyDataSetChanged();
                    //cartTotal(priceSum);
                }});

            viewProductHolder.addProductImage.setOnClickListener(new View.OnClickListener() {@Override
                public void onClick(View v) {count = Integer.parseInt(viewProductHolder.countView.getText().toString());
                    count++;
                    priceSum = priceSum + mProduct.getFoodPrice();
                    viewProductHolder.countView.setVisibility(View.VISIBLE);
                    viewProductHolder.removeProductImage.setVisibility(View.VISIBLE);
                    viewProductHolder.countView.setText(count + "");
                    mProduct.setCount(count);
                    int[] startLocation = new int[2];// 一个整型数组,用来存储按钮的在屏幕的X、Y坐标
                    v.getLocationInWindow(startLocation);// 这是获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标)
                    ImageView ball = new ImageView(mContext);// buyImg是动画的图片,我的是一个小球
                    ball.setImageResource(R.mipmap.icon_fansnum_like_on);// 设置buyImg的图片
                    updateCart(mProduct);
                    setAnim(ball, startLocation);
                    leftAdapter.notifyDataSetChanged();
                    //cartTotal(priceSum);
                }});
            return convertView;
        }private class ViewProductHolder {TextView tvTag, tvSalecount, tvTitle;//,tvPrice,countView
            ImageView productImage;//,removeProductImage,addProductImage
            private final TextView countView;
            private final TextView tvPrice;
            private final ImageView removeProductImage;
            private final ImageView addProductImage;

            private ViewProductHolder(TextView tvTag, TextView tvSalecount, TextView tvTitle, ImageView productImage, TextView countView, TextView tvPrice, ImageView removeProductImage, ImageView addProductImage) {this.tvTag = tvTag;
                this.tvSalecount = tvSalecount;
                this.tvTitle = tvTitle;
                this.productImage = productImage;
                this.countView = countView;
                this.tvPrice = tvPrice;
                this.removeProductImage = removeProductImage;
                this.addProductImage = addProductImage;
            }}@Override
        public int getSectionForPosition(int position) {return list.get(position).getSeleteId();
        }/**
         * 查询api可以得知
         * 根据分类列的索引号获得该序列的首个位置
         * @param sectionIndex
         * @return
         */
        @Override
        public int getPositionForSection(int sectionIndex) {for (int i = 0; i < getCount(); i++) {int section = list.get(i).getSeleteId();
                if (section == sectionIndex) {return i;
                }}return -1;
        }@Override
        public Object[] getSections() {// TODO 自动生成的方法存根
            return null;
        }}/cart adapter

    class CartAdapter extends BaseAdapter {private List<Cart> list = null;
        private Context mContext;

        public CartAdapter(Context mContext, List<Cart> list) {this.mContext = mContext;
            this.list = list;
        }@Override
        public int getCount() {// TODO Auto-generated method stub
            return this.list.size();
        }@Override
        public Object getItem(int position) {// TODO Auto-generated method stub
            return list.get(position);
        }@Override
        public long getItemId(int position) {// TODO Auto-generated method stub
            return position;
        }@Override
        public View getView(final int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub
            final ViewHolder viewHolder;
            final Cart mCart = list.get(position);
            if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.cart_item, null);
                TextView tvname = (TextView) convertView.findViewById(R.id.product_name_view);
                TextView countView = (TextView) convertView.findViewById(R.id.add_count_view);
                TextView totalPrice = (TextView) convertView.findViewById(R.id.product_totle_price_view);
                ImageView removeProductImage = (ImageView) convertView.findViewById(R.id.remove_product_view);
                ImageView addProductImage = (ImageView) convertView.findViewById(R.id.add_product_view);
                viewHolder = new ViewHolder(tvname, countView, totalPrice, removeProductImage, addProductImage);
                convertView.setTag(viewHolder);
            } else {viewHolder = (ViewHolder) convertView.getTag();
            }viewHolder.tvname.setText(mCart.getName());
            viewHolder.totalPrice.setText("¥" + mCart.getPrice());
            viewHolder.countView.setText(String.valueOf(mCart.getSaleCount()));
            if (Integer.parseInt(viewHolder.countView.getText().toString()) < 1) {viewHolder.removeProductImage.setVisibility(View.GONE);
                viewHolder.countView.setVisibility(View.GONE);
            } else {viewHolder.removeProductImage.setVisibility(View.VISIBLE);
                viewHolder.countView.setVisibility(View.VISIBLE);
            }viewHolder.removeProductImage.setOnClickListener(new View.OnClickListener() {@Override
                public void onClick(View v) {count = Integer.parseInt(viewHolder.countView.getText().toString());
                    count--;
                    priceSum = priceSum - mCart.getPrice();
                    if (count < 1) {viewHolder.removeProductImage.setVisibility(View.GONE);
                        viewHolder.countView.setVisibility(View.GONE);
                    }viewHolder.countView.setText(count + "");

                    removeCart();
                    //cartTotal(priceSum);

                    if (count < 1) {deleteProductId(mCart.getProductId());
                        deleteData();
                    } else {mCartList.get(position).setSaleCount(count);
                        updateCart(mCart);
                        rightAdapter.notifyDataSetChanged();
                    }leftAdapter.notifyDataSetChanged();

                }});

            viewHolder.addProductImage.setOnClickListener(new View.OnClickListener() {@Override
                public void onClick(View v) {count = Integer.parseInt(viewHolder.countView.getText().toString());
                    count++;
                    priceSum = priceSum + mCart.getPrice();
                    viewHolder.countView.setVisibility(View.VISIBLE);
                    viewHolder.removeProductImage.setVisibility(View.VISIBLE);
                    viewHolder.countView.setText(count + "");
                    //TODO 这里需要进行判断购物车是否存在 如果存在   则显示动画,否则第一次不显示动画
                    updateCart(mCart);
                    addCart();
                    //cartTotal(priceSum);
                    mCartList.get(position).setSaleCount(count);
                    rightAdapter.notifyDataSetChanged();
                    leftAdapter.notifyDataSetChanged();
                }});

            return convertView;
        }private void deleteData() {try {mCartList = UIHelper.getCartList(getActivity(), businessId);
                popuListView.setAdapter(new CartAdapter(getActivity(), mCartList));
                rightAdapter.notifyDataSetChanged();
            } catch (Exception e) {e.printStackTrace();
            }}private void updateCart(Cart mCart) {int productId = mCart.getProductId();
            String name = mCart.getName();
            double price = mCart.getPrice();
            Cart cart = new Cart();
            cart.setProductId(productId);
            cart.setName(name);
            cart.setSaleCount(count);
            cart.setPrice(price);
            cart.setBusinessId(businessId);
            UIHelper.getQueryIdCart(getActivity(), cart, productId,businessId);
        }private class ViewHolder {TextView tvname;
            private final TextView countView;
            private final ImageView removeProductImage;
            private final ImageView addProductImage;
            private final TextView totalPrice;

            private ViewHolder(TextView tvname, TextView countView, TextView totalPrice, ImageView removeProductImage, ImageView addProductImage) {this.tvname = tvname;
                this.countView = countView;
                this.totalPrice = totalPrice;
                this.removeProductImage = removeProductImage;
                this.addProductImage = addProductImage;
            }}}static double convert(double value) {long l1 = Math.round(value * 100); //四舍五入
        double ret = l1 / 100.0; //注意:使用 100.0 而不是 100
        return ret;
    }@Override
    public void onDestroy() {super.onDestroy();
        UIHelper.clearCart(getActivity(), businessId);
    }
}

左边liewview的adapter

package com.sxwz.myapplication.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sxwz.myapplication.R;
import com.sxwz.myapplication.entity.Product;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/*****************************************************
 * author:      wz
 * email:       wangzhong0116@foxmail.com
 * version:     1.0
 * date:        2017/1/10 17:50
 * description: 左侧商品类型适配器
 *****************************************************/
public class LeftAdapter extends BaseAdapter {private List<MenuTagSection> list = null;
    private Context mContext;

    public LeftAdapter(Context mContext, List<Product> list) {this.mContext = mContext;
        this.list = getTagSections(list);
    }@Override
    public int getCount() {return this.list.size();
    }@Override
    public Object getItem(int position) {return list.get(position);
    }@Override
    public long getItemId(int position) {return position;
    }@Override
    public View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder = null;
        final MenuTagSection mContent = list.get(position);
        if (convertView == null) {viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.product_menu_item, null);
            viewHolder.tvTag = (TextView) convertView.findViewById(R.id.menu_tag_tv);
            viewHolder.tvSection = (TextView) convertView.findViewById(R.id.section_tv);
            convertView.setTag(viewHolder);

        } else {viewHolder = (ViewHolder) convertView.getTag();
        }Log.i("fuck_getview", "success");
        viewHolder.tvSection.setText(String.valueOf(mContent.getMenuSection()));

        viewHolder.tvTag.setText(mContent.getMenuTag());

        return convertView;
    }final static class ViewHolder {TextView tvTag;
        TextView tvSection;
    }final static class MenuTagSection {String menuTag;
        int menuSection;

        public String getMenuTag() {return menuTag;
        }public int getMenuSection() {return menuSection;
        }public MenuTagSection(String menuTag, int menuSection) {this.menuTag = menuTag;
            this.menuSection = menuSection;
        }@Override
        public boolean equals(Object o) {MenuTagSection mts = (MenuTagSection) o;
            return menuTag.equals(mts.menuTag) && (menuSection == mts.menuSection);
        }@Override
        public int hashCode() {String in = menuSection + menuTag;
            return in.hashCode();
        }}/**
     * 此处遍历商品去掉相同的商品类型
     * 显示商品类型
     * 左边菜单-----类型
     * @param list
     * @return
     */
    private List<MenuTagSection> getTagSections(List<Product> list) {List<MenuTagSection> mtsList = new ArrayList<MenuTagSection>();
        for (int i = 0; i < list.size(); i++) {if (!mtsList.contains(new MenuTagSection(list.get(i).getType(), list.get(i).getSeleteId()))) {mtsList.add(new MenuTagSection(list.get(i).getType(), list.get(i).getSeleteId()));
            }}//去除重复
        Set<MenuTagSection> ts = new HashSet<MenuTagSection>();
        List<MenuTagSection> newMtsList = new ArrayList<MenuTagSection>();
        for (Iterator<MenuTagSection> iter = mtsList.iterator(); iter.hasNext(); ) {MenuTagSection ele = iter.next();
            if (ts.add(ele)) {newMtsList.add(ele);
            }}return newMtsList;
    }}

唉。。。。。

感觉这么一一的贴代码也不是个事,万一少贴了一点,其不是要报错。我相信绝大部分的童鞋都不希望这样,都希望直接给个demo下载地址多好呀。
你们的想法实现了,我确实也不想一一贴了,还是直接来下载地址吧 http://download.csdn.net/detail/mingzhnglei/9734768
声明下在我在demo里使用的是我的一个开源库
https://github.com/rocky0116/orderDishes
https://github.com/rocky0116/QuickDevelopApp 欢迎大家fork or star
compile 'com.sxwz.qcodelib:qcodelib:1.0.1'
有兴趣的朋友可以去看看,记得关注我
http://blog.csdn.net/mingzhnglei/article/details/54133673

android 实现仿美团点菜和京东分类导航相关推荐

  1. 基于Android的仿美团外卖系统设计与实现 文档+源码+视频

    基于Android的仿美团外卖系统设计与实现 演示视频 摘 要 为了巩固所学 Android 基础知识,要开发一款仿美团外卖的项目,该项目与我们平常看到的美团外卖项目界面比较类似,展示的内容包括店铺. ...

  2. android仿美团论文,毕业设计(论文)-基于Android的仿美团系统.docx

    全套设计加扣 3012250582 PAGE \* MERGEFORMAT- 1 - 全套设计加扣 3012250582 湖南软件职业学院 毕业设计 毕业选题 : 基于Android的仿美团系统 指导 ...

  3. android高仿美团筛选控件,Android高仿美团首页分类按钮

    惯例,先上GIF 栗子.gif更新v1.1版本 2017-6-2 11:55:30 详见github 一.使用姿势 1.引入(使用Gradle或者Maven) 1)Gradleallprojects  ...

  4. Android 高仿美团外卖详情页

    目录 1.需求分析 2.具体实现 2.1效果展示 2.2布局分析 2.3代码分析 2.3.1自定义 CoordinatorLayout.Behavior 2.3.2自定义 RecyclerView.I ...

  5. HTML+CSS 仿写淘宝商城分类导航

    效果图 学会淘宝商城的分类导航以后,会发现大多数的电商网站都是这样的效果,在做其他网站时具体分析即可. 首先我们要构思整个大框架的结构:左边是全部商品分类,在这个分类中主要是用了导航列表(ul li) ...

  6. Android:实现仿 美团/淘宝 多级分类菜单效果

    本例要实现的是诸如美团/淘宝/百度糯米 多级分类菜单效果.当分类数量非常多时可以考虑采用两级分类,而诸如美团这种表现方式是一个不错的选择. 首先上效果图:      主要代码: 1. PopupWin ...

  7. android 高仿美团,Android 仿美团、大众点评团购详情UI

    在scrollview 上滑固定某一控件(美团团购详情UI)文中介绍了怎么用touchlistener实现类似上滑停住的效果,但是这种方法存在一个明显的bug,就是在内容比较多的时候, 大部分人都是以 ...

  8. Android自己仿美团做的外卖软件(巴哥外卖),新手进

    先附上GitHub:https://github.com/LiuJingyingdev/Food 再附几张效果图: 主要用到的知识点,RecyclerView显示列表,ScollView,Materi ...

  9. Android 高仿华为手机Tab页滑动导航效果

    首先带大家看一下实现效果,用了两种实现方式: 1.基于LinearLayout实现,导航栏不可响应手指滑动 2.基于HorizontalScrollView实现,导航栏可响应手指滑动 实现方式虽然不一 ...

  10. vue仿美团饿了么--底部导航公共组件

    底部导航栏,是每一个页面都需要用的,所以用的公共组件,在app.vue中引入 import common from "./components/footer/common.vue" ...

最新文章

  1. JS数组方法汇总 array数组元素的添加和删除
  2. python下载不了-python3下载不了
  3. IEEE R10 SAC Special Call for Proposals
  4. Oracle 原理: 物化视图,快照,实体化视图。
  5. opencv学习笔记1:图片读入,显示与保存(有代码)
  6. 2014 网选 5024 Wang Xifeng's Little Plot
  7. linux程序库设置错误,Citrix在Arch Linux中无效 – 库错误
  8. ibtais中把clob数据类型转换成string并插入到数据库中
  9. 通过pgpool-II实现PostgreSQL数据库服务高可用
  10. 微软云架构服务器,微软云存储架构(Azure Cloud Storage)
  11. HDU 5820 Lights(扫描线+zkw线段树)
  12. Oracle数据库中的索引
  13. 数字证书是什么,主要应用于哪些方面?
  14. 代理ip填写格式有什么要求?
  15. tkm批量转mp3工具使用教程
  16. App测试从入门到精通之功能测试
  17. linux下arm架构中,触摸屏下没有鼠标事件,采用qtouchevent事件来代替,左右滑动显示完文本
  18. Simplicity Studio V5 建立一个空工程后如何添加外设驱动
  19. 群晖硬盘已损毁 Linux 修复,今天群晖存储空间损毁,起死回生
  20. python开头注释

热门文章

  1. python 视频剪辑软件_一个Python写的视频剪辑软件,好用!
  2. 杰理AD14N/AD15N---Timer定时器问题
  3. ABB机器人GSD文件获取的几种方法
  4. 数值计算之 插值法(2)多项式插值——牛顿插值法
  5. 第一个Django项目----一小时写出账号密码管理系统
  6. 简单的Django项目
  7. php实现简易聊天室
  8. Win10 Word背景默认是绿色的怎么取消?
  9. 【SAP】实施方法论-ASAP
  10. select获取下拉框的值 下拉框默认选中