概述
公司有个项目需要做购物车功能,于是实现了一下,另加了个浮动title ,侧滑删除,示例图片如下
//侧滑删除
compile 'com.github.anzaizai:EasySwipeMenuLayout:1.1.2'
实现方式 adapter用的是封装的
CommonAdapter
代码如下
package com.loocan.shpping.viewholder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* adapter基类
*
* @param <T>
*/
public abstract class CommonAdapter<T> extends BaseAdapter {
protected Context mContext;
protected List<T> mDatas;
protected LayoutInflater mInflater;
private int layoutId;
private int displayCount;
public CommonAdapter(Context context, List<T> datas) {
this.mContext = context;
this.mDatas = datas;
mInflater = LayoutInflater.from(context);
}
public CommonAdapter(Context context, List<T> datas, int layoutId) {
this.mContext = context;
this.mDatas = datas;
this.layoutId = layoutId;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
if (displayCount != 0) {
return this.displayCount;
}
return mDatas.size();
}
public void setDisplayCount(int displayCount) {
this.displayCount = displayCount;
}
@Override
public T getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = ViewHolder.get(mContext, convertView, parent,
layoutId, position);
convert(holder, getItem(position));
return holder.getConvertView();
}
public List<T> getDatas() {
return mDatas;
}
public void setDatas(List<T> datas) {
mDatas = datas;
}
public abstract void convert(ViewHolder holder, T t);
}
package com.loocan.shpping.viewholder;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.util.Linkify;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
/**
* 以前的做法是这样子的
* viewHolder = new ViewHolder();
* convertView = layoutInflater.inflate(R.layout.people_list_item, null, false);
* viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
* viewHolder.name = (TextView)convertView.findViewById(R.id.name);
* 缺点,如果我们换了一个Item,我们就需要在ViewHolder中从新添加对应空间的变量
* 也就需要重写viewHolder,
* 因此这里用了一个通用的方法,就是在viewHolder中用一个类似于map的键值来保存findViewById对象,
* 这种比先前还有一个好处就是,不管多少条Item,Item中的每个控件只会被findViewById一次
* 以前的方法,当前页面缓存的个数4个,然后内存中对每个控件都是对应4个,也就是findViewById4次,如果屏幕较大就会越来越多
* 以后一个项目几十个Adapter一个ViewHolder直接hold住全场
*
* @ Author: qiyue (ustory)
* @ Email: qiyuekoon@foxmail.com
* @ Data:2016/3/6
*/
public class ViewHolder {
private SparseArray<View> mViews;
private int mPosition;
private View mConvertView;
private Context mContext;
private int mLayoutId;
public ViewHolder(Context context, ViewGroup parent, int layoutId,
int position) {
mContext = context;
mLayoutId = layoutId;
this.mPosition = position;
this.mViews = new SparseArray<View>();
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
false);
mConvertView.setTag(this);
}
public static ViewHolder get(Context context, View convertView,
ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new ViewHolder(context, parent, layoutId, position);
} else {
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.mPosition = position;
return holder;
}
}
public int getPosition() {
return mPosition;
}
public int getLayoutId() {
return mLayoutId;
}
/**
* 通过viewId获取控件
*
* @param viewId
* @return
*/
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public View getConvertView() {
return mConvertView;
}
/**
* 设置TextView的�??
*
* @param viewId
* @param text
* @return
*/
public ViewHolder setText(int viewId, String text) {
TextView tv = getView(viewId);
tv.setText(text);
return this;
}
public ViewHolder setImageResource(int viewId, int resId) {
ImageView view = getView(viewId);
view.setImageResource(resId);
return this;
}
public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
ImageView view = getView(viewId);
view.setImageBitmap(bitmap);
return this;
}
public ViewHolder setImageDrawable(int viewId, Drawable drawable) {
ImageView view = getView(viewId);
view.setImageDrawable(drawable);
return this;
}
public ViewHolder setBackgroundColor(int viewId, int color) {
View view = getView(viewId);
view.setBackgroundColor(color);
return this;
}
public ViewHolder setBackgroundRes(int viewId, int backgroundRes) {
View view = getView(viewId);
view.setBackgroundResource(backgroundRes);
return this;
}
public ViewHolder setTextColor(int viewId, int textColor) {
TextView view = getView(viewId);
view.setTextColor(textColor);
return this;
}
public ViewHolder setTextColorRes(int viewId, int textColorRes) {
TextView view = getView(viewId);
view.setTextColor(mContext.getResources().getColor(textColorRes));
return this;
}
@SuppressLint("NewApi")
public ViewHolder setAlpha(int viewId, float value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getView(viewId).setAlpha(value);
} else {
// Pre-honeycomb hack to set Alpha value
AlphaAnimation alpha = new AlphaAnimation(value, value);
alpha.setDuration(0);
alpha.setFillAfter(true);
getView(viewId).startAnimation(alpha);
}
return this;
}
public ViewHolder setVisible(int viewId, boolean visible) {
View view = getView(viewId);
view.setVisibility(visible ? View.VISIBLE : View.GONE);
return this;
}
public ViewHolder linkify(int viewId) {
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
public ViewHolder setTypeface(Typeface typeface, int... viewIds) {
for (int viewId : viewIds) {
TextView view = getView(viewId);
view.setTypeface(typeface);
view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
return this;
}
public ViewHolder setProgress(int viewId, int progress) {
ProgressBar view = getView(viewId);
view.setProgress(progress);
return this;
}
public ViewHolder setProgress(int viewId, int progress, int max) {
ProgressBar view = getView(viewId);
view.setMax(max);
view.setProgress(progress);
return this;
}
public ViewHolder setMax(int viewId, int max) {
ProgressBar view = getView(viewId);
view.setMax(max);
return this;
}
public ViewHolder setRating(int viewId, float rating) {
RatingBar view = getView(viewId);
view.setRating(rating);
return this;
}
public ViewHolder setRating(int viewId, float rating, int max) {
RatingBar view = getView(viewId);
view.setMax(max);
view.setRating(rating);
return this;
}
public ViewHolder setTag(int viewId, Object tag) {
View view = getView(viewId);
view.setTag(tag);
return this;
}
public ViewHolder setTag(int viewId, int key, Object tag) {
View view = getView(viewId);
view.setTag(key, tag);
return this;
}
public ViewHolder setChecked(int viewId, boolean checked) {
Checkable view = (Checkable) getView(viewId);
view.setChecked(checked);
return this;
}
/**
* 关于事件
*/
public ViewHolder setOnClickListener(int viewId,
View.OnClickListener listener) {
View view = getView(viewId);
view.setOnClickListener(listener);
return this;
}
public ViewHolder setOnTouchListener(int viewId,
View.OnTouchListener listener) {
View view = getView(viewId);
view.setOnTouchListener(listener);
return this;
}
public ViewHolder setOnLongClickListener(int viewId,
View.OnLongClickListener listener) {
View view = getView(viewId);
view.setOnLongClickListener(listener);
return this;
}
}
Activity 代码如下
package com.loocan.shpping;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity implements CartAdapter.ItemClickListener {
@BindView(R.id.tv_back)
TextView mTvBack;
@BindView(R.id.tv_title)
TextView mTvTitle;
@BindView(R.id.tv_right)
TextView mTvRight;
@BindView(R.id.rlt_base)
RelativeLayout mRltBase;
@BindView(R.id.listView)
ListView mListView;
@BindView(R.id.all_chekbox)
CheckBox mAllChekbox;
@BindView(R.id.tv_total_price)
TextView mTvTotalPrice;
@BindView(R.id.tv_go_to_pay)
TextView mTvGoToPay;
private double totalPrice = 0.00;
private int totalCount = 0;
private List<HashMap<String, String>> goodsList;
private CartAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mTvTitle.setText("购物车");
//模拟一些数据
initDate();
initView();
}
private void initDate() {
goodsList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
HashMap<String, String> map = new HashMap<>();
map.put("id", "0");
map.put("name", "购物车里的第" + (i + 1) + "件商品");
map.put("type", (i + 20) + "码");
map.put("price", (new Random().nextInt(100) % (100 - 29 + 29) + 29) + "");
map.put("count", (new Random().nextInt(10) % (10 - 1 + 1) + 1) + "");
goodsList.add(map);
}
}
private void initView() {
adapter = new CartAdapter(this, goodsList, R.layout.item_cehua);
adapter.setOnItemClickListener(this);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@OnClick({R.id.all_chekbox, R.id.tv_go_to_pay})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.all_chekbox:
AllTheSelected(true);
break;
case R.id.tv_go_to_pay:
if (totalCount <= 0) {
Toast.makeText(this, "请选择要付款的商品~", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "钱就是另一回事了~", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void ItemClickListener(View view, int position) {
HashMap<String, String> hashMap = goodsList.get(position);
if (((CheckBox) view).isChecked()) {
hashMap.put("id", "1");
} else {
hashMap.put("id", "0");
}
goodsList.set(position, hashMap);
AllTheSelected(false);
}
//删除
@Override
public void ItemDeleteClickListener(View view, int position) {
goodsList.remove(position);
AllTheSelected(false);
}
//增加
@Override
public void ItemAddClickListener(View view, int position) {
//当低于目标值是 可以做删除操作
HashMap<String, String> hashMap = goodsList.get(position);
int currentCount = Integer.valueOf(hashMap.get("count"));
if (currentCount + 1 > 15) {
Toast.makeText(this, "亲,已达库存上限~", Toast.LENGTH_SHORT).show();
} else {
hashMap.put("count", String.valueOf(currentCount + 1));
goodsList.set(position, hashMap);
}
AllTheSelected(false);
}
//减
@Override
public void ItemReduceClickListener(View view, int position) {
HashMap<String, String> hashMap = goodsList.get(position);
int currentCount = Integer.valueOf(hashMap.get("count"));
if (currentCount - 1 < 1) {
Toast.makeText(this, "受不了了,宝贝不能再减少了哦~", Toast.LENGTH_SHORT).show();
} else {
hashMap.put("count", String.valueOf(currentCount - 1));
goodsList.set(position, hashMap);
}
AllTheSelected(false);
}
//控制价格展示
private void priceContro() {
totalCount = 0;
totalPrice = 0.00;
for (int i = 0; i < goodsList.size(); i++) {
if (goodsList.get(i).get("id").equals("1")) {
totalCount = totalCount + Integer.valueOf(goodsList.get(i).get("count"));
double goodsPrice = Integer.valueOf(goodsList.get(i).get("count")) * Double.valueOf(goodsList.get(i).get("price"));
totalPrice = totalPrice + goodsPrice;
}
}
mTvTotalPrice.setText("¥ " + totalPrice);
mTvGoToPay.setText("付款(" + totalCount + ")");
}
/**
* 部分选取 做全选 全部选择做反选
*/
private void AllTheSelected(Boolean aBoolean) {
int number = 0;
for (int j = 0; j < goodsList.size(); j++) {
if (goodsList.get(j).get("id").equals("1")) {
number++;
}
}
if (aBoolean) {
//全部选择 反选
if (number == goodsList.size()) {
for (int i = 0; i < goodsList.size(); i++) {
goodsList.get(i).put("id", "0");
}
mAllChekbox.setChecked(false);
//全部未选
} else if (number == 0) {
for (int i = 0; i < goodsList.size(); i++) {
goodsList.get(i).put("id", "1");
}
mAllChekbox.setChecked(true);
//部分选择
} else {
for (int i = 0; i < goodsList.size(); i++) {
goodsList.get(i).put("id", "1");
}
mAllChekbox.setChecked(true);
}
} else {
if (number == goodsList.size()) {
mAllChekbox.setChecked(true);
} else {
mAllChekbox.setChecked(false);
}
}
adapter.notifyDataSetChanged();
priceContro();
}
}
adapter如下
package com.loocan.shpping;
import android.content.Context;
import android.view.View;
import com.guanaj.easyswipemenulibrary.EasySwipeMenuLayout;
import com.loocan.shpping.viewholder.CommonAdapter;
import com.loocan.shpping.viewholder.ViewHolder;
import java.util.HashMap;
import java.util.List;
/**
* File descripition:
*
* @author lp
* @date 2018/9/28
*/
public class CartAdapter extends CommonAdapter<HashMap<String, String>> {
private ItemClickListener listener;
public CartAdapter(Context context, List<HashMap<String, String>> datas, int layoutId) {
super(context, datas, layoutId);
}
@Override
public void convert(final ViewHolder holder, HashMap<String, String> map) {
if (map.get("id").equals("0")) {
holder.setChecked(R.id.check_box, false);
} else {
holder.setChecked(R.id.check_box, true);
}
holder.setText(R.id.tv_goods_name, map.get("name"));
holder.setText(R.id.tv_num, map.get("count"));
holder.setText(R.id.tv_type_size, map.get("type"));
holder.setText(R.id.tv_goods_price, "¥ " + (Double.valueOf(map.get("price")) * Integer.valueOf(map.get("count"))));
final EasySwipeMenuLayout easySwipeMenuLayout = holder.getView(R.id.action_bar);
holder.setOnClickListener(R.id.check_box, new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.ItemClickListener(v, holder.getPosition());
}
});
//删除
holder.setOnClickListener(R.id.right, new View.OnClickListener() {
@Override
public void onClick(View v) {
//侧滑初始化
easySwipeMenuLayout.resetStatus();
listener.ItemDeleteClickListener(v, holder.getPosition());
}
});
//减
holder.setOnClickListener(R.id.tv_reduce, new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.ItemReduceClickListener(v, holder.getPosition());
}
});
//加
holder.setOnClickListener(R.id.tv_add, new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.ItemAddClickListener(v, holder.getPosition());
}
});
}
public void setOnItemClickListener(ItemClickListener listener) {
this.listener = listener;
}
public interface ItemClickListener {
void ItemClickListener(View view, int position);
void ItemDeleteClickListener(View view, int position);
void ItemAddClickListener(View view, int position);
void ItemReduceClickListener(View view, int position);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.loocan.shpping.MainActivity">
<include layout="@layout/app_title" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.5"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/all_chekbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="4dp"
android:button="@drawable/check_box_bg"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:minHeight="64dp"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="合计:"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥0.00"
android:textColor="@color/colorBlack"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/tv_go_to_pay"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#E24146"
android:clickable="true"
android:gravity="center"
android:text="付款(0)"
android:textColor="#FAFAFA" />
</LinearLayout>
</LinearLayout>
item_cehua.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.guanaj.easyswipemenulibrary.EasySwipeMenuLayout
android:id="@+id/action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentView="@+id/content"
app:rightMenuView="@+id/right">
<LinearLayout
android:id="@+id/left"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:orientation="horizontal"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享" />
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="114dp"
android:background="#cccccc"
android:orientation="vertical">
<include layout="@layout/item_shopcart_product" />
</LinearLayout>
<RelativeLayout
android:id="@+id/right"
android:layout_width="60dp"
android:layout_height="114dp"
android:background="@android:color/holo_red_light"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="删除"
android:textColor="@color/colorBlack" />
</RelativeLayout>
</com.guanaj.easyswipemenulibrary.EasySwipeMenuLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#CCCCCC" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorF5F5F5"
android:orientation="horizontal">
<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="4dp"
android:button="@drawable/check_box_bg"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:minHeight="64dp"
android:minWidth="32dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="visible" />
<ImageView
android:id="@+id/iv_adapter_list_pic"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="13dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="13dp"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/tv_goods_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/colorBlack"
android:textSize="14sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_goods_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:textColor="@color/colorBlack"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_type_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/tv_goods_price"
android:singleLine="true"
android:textColor="@color/colorBlack"
android:textSize="10sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_reduce"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/text_angle_gray"
android:gravity="center"
android:text="一"
android:textColor="@color/colorBlack"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_num"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/text_angle"
android:gravity="center"
android:singleLine="true"
android:text="1"
android:textColor="@color/colorBlack"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_add"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/text_angle_right"
android:gravity="center"
android:text="+"
android:textColor="@color/colorBlack"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
demo链接地址:https://download.csdn.net/download/loocanp/10693697
最后
以上就是清爽硬币为你收集整理的Android商城购物车页面实现的全部内容,希望文章能够帮你解决Android商城购物车页面实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复