安卓 仿餓了么購物車 電商

最近在做一個(gè)商城系統(tǒng),因?yàn)橐鲑徫镘囈粋€(gè)功能,然后還要和餓了么購物車一模一樣,我剛開始也是各種找資料,但是網(wǎng)上的都是死數(shù)據(jù),用Map集合或者用SparrayMap存儲(chǔ)?,F(xiàn)在我自己寫接口,自己用活數(shù)據(jù)填充。后臺(tái)接口我也是弄了半天,我先把接口顯示的接口數(shù)據(jù)展示一下:


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

我先上圖,我已經(jīng)做好的界面:


左邊 的條目,右邊的商品,底部的購物車,然后還有彈窗出現(xiàn),然后還有商品價(jià)格計(jì)算

網(wǎng)上大部分是用map集合,我現(xiàn)在用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()));//商品數(shù)量

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

? ? ? ? ? ? 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;

? ? }

}

點(diǎn)擊事件中對(duì)item中num進(jìn)行增加減少,邏輯簡單,一般大家都會(huì)碰到全部的條目同時(shí)添加減少,只要設(shè)置tag就好了



這段代碼主要是添加的時(shí)候

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容