最近在做一个商城系统,因为要做购物车一个功能,然后还要和饿了么购物车一模一样,我刚开始也是各种找资料,但是网上的都是死数据,用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就好了
这段代码主要是添加的时候
网友评论