安卓 仿餓了么購物車 電商

最近在做一個商城系統,因為要做購物車一個功能,然后還要和餓了么購物車一模一樣,我剛開始也是各種找資料,但是網上的都是死數據,用Map集合或者用SparrayMap存儲。現在我自己寫接口,自己用活數據填充。后臺接口我也是弄了半天,我先把接口顯示的接口數據展示一下:


kind是全部商品的種類,goods是商品集合。num是用戶已經采購的訂單中的數量,所以,我們進行json解析Format一下,生成實體類

我先上圖,我已經做好的界面:


左邊 的條目,右邊的商品,底部的購物車,然后還有彈窗出現,然后還有商品價格計算

網上大部分是用map集合,我現在用list集合

先展示adapter適配器代碼:

public class GoodsAdapterextends BaseAdapterimplements StickyListHeadersAdapter, GoodCarContract.View {

private ListdataList;

? ? private SortFragmentmContext;

? ? private NumberFormatnf;

? ? private LayoutInflatermInflater;

? ? private GoodCarPresentergoodCarPresenter =new GoodCarPresenter(this);

? ? private int layoutPosition;

? ? public GoodsAdapter(List dataList, SortFragment mContext) {

this.dataList = dataList;

? ? ? ? this.mContext = mContext;

? ? ? ? nf = NumberFormat.getCurrencyInstance();

? ? ? ? nf.setMaximumFractionDigits(2);

? ? ? ? mInflater = LayoutInflater.from(mContext.getContext());

? ? }

@Override

? ? public ViewgetHeaderView(int position, View convertView, ViewGroup parent) {

if (convertView ==null) {

convertView =mInflater.inflate(R.layout.item_header_view, parent, false);

? ? ? ? ? ? convertView.setVisibility(View.GONE);

? ? ? ? }

convertView.setVisibility(View.GONE);

? ? ? ? return convertView;

? ? }

@Override

? ? public long getHeaderId(int position) {

return dataList.get(position).getId();

? ? }

@Override

? ? public int getCount() {

if (dataList ==null) {

return 0;

? ? ? ? }

return dataList.size();

? ? }

@Override

? ? public ObjectgetItem(int position) {

return dataList.get(position);

? ? }

@Override

? ? public long getItemId(int position) {

return position;

? ? }

@Override

? ? public ViewgetView(int position, View convertView, ViewGroup parent) {

ItemViewHolder holder;

? ? ? ? if (convertView ==null) {

convertView =mInflater.inflate(R.layout.ry_goods_list_item, parent, false);

? ? ? ? ? ? holder =new ItemViewHolder(convertView);

? ? ? ? ? ? convertView.setTag(holder);

? ? ? ? }else {

holder = (ItemViewHolder) convertView.getTag();

? ? ? ? }

GoodGson.GoodsBean item =dataList.get(position);

? ? ? ? holder.bindData(item,position);

? ? ? ? return convertView;

? ? }

@Override

? ? public void addSuccess() {

}

@Override

? ? public void addFailed() {

}

@Override

? ? public void loadShopCarList(List goodsBeen) {

}

private class ItemViewHolder {

private TextViewname, price, tvCount;

? ? ? ? private GoodGson.GoodsBeanitem;

? ? ? ? private ImageViewtvAdd, tvMinus, ivCover;

? ? ? ? public ItemViewHolder(View itemView) {

name = (TextView) itemView.findViewById(R.id.tv_name);

? ? ? ? ? ? price = (TextView) itemView.findViewById(R.id.tv_price);

? ? ? ? ? ? tvCount = (TextView) itemView.findViewById(R.id.tv_count);

? ? ? ? ? ? tvMinus = (ImageView) itemView.findViewById(R.id.iv_reduce);

? ? ? ? ? ? tvAdd = (ImageView) itemView.findViewById(R.id.iv_add);

? ? ? ? ? ? ivCover = itemView.findViewById(R.id.iv_cover);

? ? ? ? }

public void bindData(final GoodGson.GoodsBean item, final int position) {

this.item = item;

? ? ? ? ? ? name.setText(item.getGoods_name());//商品名稱

? ? ? ? ? ? Glide.with(mContext).load(item.getGoods_pic()).into(ivCover);//商品圖片

? ? ? ? ? ? tvCount.setText(String.valueOf(item.getNum()));//商品數量

? ? ? ? ? ? price.setText(nf.format(item.getGoods_price()));//商品價格

? ? ? ? ? ? if (item.getNum() <1) {

tvCount.setVisibility(View.GONE);

? ? ? ? ? ? ? ? tvMinus.setVisibility(View.GONE);

? ? ? ? ? ? }else {

tvCount.setVisibility(View.VISIBLE);

? ? ? ? ? ? ? ? tvMinus.setVisibility(View.VISIBLE);

? ? ? ? ? ? }

tvAdd.setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? ? ? public void onClick(View v) {

SortFragment activity =mContext;

? ? ? ? ? ? ? ? ? ? int count =item.getNum();

? ? ? ? ? ? ? ? ? ? if (count <1) {

tvMinus.setAnimation(getShowAnimation());

? ? ? ? ? ? ? ? ? ? ? ? tvMinus.setVisibility(View.VISIBLE);

? ? ? ? ? ? ? ? ? ? ? ? tvCount.setVisibility(View.VISIBLE);

? ? ? ? ? ? ? ? ? ? }

activity.add(item,position);

? ? ? ? ? ? ? ? ? ? count++;

? ? ? ? ? ? ? ? ? ? tvCount.setText(String.valueOf(count));

? ? ? ? ? ? ? ? ? ? int[] loc =new int[2];

? ? ? ? ? ? ? ? ? ? v.getLocationInWindow(loc);

? ? ? ? ? ? ? ? ? ? activity.playAnimation(loc);

? ? ? ? ? ? ? ? }

});

? ? ? ? ? ? tvMinus.setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? ? ? public void onClick(View v) {

SortFragment activity =mContext;

? ? ? ? ? ? ? ? ? ? int count =item.getNum();

? ? ? ? ? ? ? ? ? ? if (count <2) {

tvMinus.setAnimation(getHiddenAnimation());

? ? ? ? ? ? ? ? ? ? ? ? tvMinus.setVisibility(View.GONE);

? ? ? ? ? ? ? ? ? ? ? ? tvCount.setVisibility(View.GONE);

? ? ? ? ? ? ? ? ? ? }

count--;

? ? ? ? ? ? ? ? ? ? activity.remove(item,position);//activity.getSelectedItemCountById(item.id)

? ? ? ? ? ? ? ? ? ? tvCount.setText(String.valueOf(count));

? ? ? ? ? ? ? ? }

});

? ? ? ? }

}

private AnimationgetShowAnimation() {

AnimationSet set =new AnimationSet(true);

? ? ? ? RotateAnimation rotate =new RotateAnimation(0, 720, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);

? ? ? ? set.addAnimation(rotate);

? ? ? ? TranslateAnimation translate =new TranslateAnimation(

TranslateAnimation.RELATIVE_TO_SELF, 2f

? ? ? ? ? ? ? ? , TranslateAnimation.RELATIVE_TO_SELF, 0

? ? ? ? ? ? ? ? , TranslateAnimation.RELATIVE_TO_SELF, 0

? ? ? ? ? ? ? ? , TranslateAnimation.RELATIVE_TO_SELF, 0);

? ? ? ? set.addAnimation(translate);

? ? ? ? AlphaAnimation alpha =new AlphaAnimation(0, 1);

? ? ? ? set.addAnimation(alpha);

? ? ? ? set.setDuration(500);

? ? ? ? return set;

? ? }

private AnimationgetHiddenAnimation() {

AnimationSet set =new AnimationSet(true);

? ? ? ? RotateAnimation rotate =new RotateAnimation(0, 720, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);

? ? ? ? set.addAnimation(rotate);

? ? ? ? TranslateAnimation translate =new TranslateAnimation(

TranslateAnimation.RELATIVE_TO_SELF, 0

? ? ? ? ? ? ? ? , TranslateAnimation.RELATIVE_TO_SELF, 2f

? ? ? ? ? ? ? ? , TranslateAnimation.RELATIVE_TO_SELF, 0

? ? ? ? ? ? ? ? , TranslateAnimation.RELATIVE_TO_SELF, 0);

? ? ? ? set.addAnimation(translate);

? ? ? ? AlphaAnimation alpha =new AlphaAnimation(1, 0);

? ? ? ? set.addAnimation(alpha);

? ? ? ? set.setDuration(500);

? ? ? ? return set;

? ? }

}

點擊事件中對item中num進行增加減少,邏輯簡單,一般大家都會碰到全部的條目同時添加減少,只要設置tag就好了



這段代碼主要是添加的時候

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容