这是效果图
上代码
主activity代码
——————————————我是分割线————————————————————
package activitytest.example.com.shopcar; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.TextView; import com.baoyz.swipemenulistview.SwipeMenu; import com.baoyz.swipemenulistview.SwipeMenuCreator; import com.baoyz.swipemenulistview.SwipeMenuItem; import com.baoyz.swipemenulistview.SwipeMenuListView; import java.math.BigDecimal; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { // 全选商品 private CheckBox mAllCkeck; // 总价钱 private TextView mAllMoney; private SwipeMenuListView mList; private ArrayList<GoodBean> mData; private MyAdapter mAdapter; //总价 private double allMoney = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mData = new ArrayList<>(); // 初始化数据 for (int i = 0; i < 30; i++) { mData.add(new GoodBean(false, R.mipmap.ic_launcher, "name" + (i + 1), "guige" + (i + 1), 5.21 + i, i + 1)); } mAllCkeck = (CheckBox) findViewById(R.id.allCkeck); mAllMoney = (TextView) findViewById(R.id.allMoney); mList = (SwipeMenuListView) findViewById(R.id.allList); // 设置侧滑菜单 SwipeMenuCreator creator = new SwipeMenuCreator() { @Override public void create(SwipeMenu menu) { SwipeMenuItem openItem = new SwipeMenuItem( getApplicationContext()); // 设置菜单的背景 openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE))); // 宽度 菜单的宽度是一定要有的,否则不显示菜单,笔者就吃了这样的亏 openItem.setWidth(180); // 菜单标题 openItem.setTitle("删除"); // 标题大小 openItem.setTitleSize(18); // 标题的颜色 openItem.setTitleColor(Color.WHITE); // 添加到menu menu.addMenuItem(openItem); } }; mList.setMenuCreator(creator); mAdapter = new MyAdapter(); mList.setAdapter(mAdapter); // 设置侧滑菜单点击事件 mList.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(int position, SwipeMenu menu, int index) { // 点击后删除商品 mData.remove(position); // 删除商品后刷新数据 mAdapter.notifyDataSetChanged(); return false; } }); mAllCkeck.setOnCheckedChangeListener(this); mAllMoney.setText("总价" + initNumber(allMoney)); } //全选操作处理 @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { allMoney = 0; if (isChecked) { // 全选后商品总价为每个商品价钱X每个商品个数的总和 for (int i = 0; i < mData.size(); i++) { mData.get(i).setCheckState(true); // 计算总价 allMoney += (mData.get(i).getGoodPrice() * mData.get(i).getGoodNumber()); } } else { //没有全选则总价为0 for (int i = 0; i < mData.size(); i++) { mData.get(i).setCheckState(false); } } mAllMoney.setText("总价" + initNumber(allMoney)); mAdapter.notifyDataSetChanged(); } //适配器 class MyAdapter extends BaseAdapter { @Override public int getCount() { return mData != null ? mData.size() : 0; } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.shop_item, null, false); viewHolder.mItemCheck = (ImageView) convertView.findViewById(R.id.itemCheck); viewHolder.mItemImage = (ImageView) convertView.findViewById(R.id.itemImage); viewHolder.mItemName = (TextView) convertView.findViewById(R.id.itemName); viewHolder.mItemGuige = (TextView) convertView.findViewById(R.id.itemGuige); viewHolder.mItemPrice = (TextView) convertView.findViewById(R.id.itemPrice); viewHolder.mItemMove = (TextView) convertView.findViewById(R.id.itemMove); viewHolder.mItemNumber = (TextView) convertView.findViewById(R.id.itemNumber); viewHolder.mItemAdd = (TextView) convertView.findViewById(R.id.itemAdd); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.mItemCheck.setImageResource(mData.get(position).isCheckState() ? R.mipmap.ic_checked : R.mipmap.ic_uncheck); viewHolder.mItemImage.setImageResource(mData.get(position).getImageId()); viewHolder.mItemName.setText(mData.get(position).getGoodName()); viewHolder.mItemGuige.setText(mData.get(position).getGoodGuige()); viewHolder.mItemPrice.setText(mData.get(position).getGoodPrice() + "元"); viewHolder.mItemNumber.setText(mData.get(position).getGoodNumber() + ""); //处理item 的选中和未选中 viewHolder.mItemCheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mData.get(position).isCheckState()) { // 由选中变成未选中 从总价中减去价格 mData.get(position).setCheckState(false); allMoney -= (mData.get(position).getGoodPrice() * mData.get(position).getGoodNumber()); } else { // 由未选中变成选中 总价中加上价格 mData.get(position).setCheckState(true); allMoney += (mData.get(position).getGoodPrice() * mData.get(position).getGoodNumber()); } mAllMoney.setText("总价" + initNumber(allMoney)); mAdapter.notifyDataSetChanged(); } }); // 处理添加个数 viewHolder.mItemAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 如果选中 增加后要增加总价钱 if (mData.get(position).isCheckState()) { allMoney += mData.get(position).getGoodPrice(); mAllMoney.setText("总价" + initNumber(allMoney)); } mData.get(position).setGoodNumber(mData.get(position).getGoodNumber() + 1); mAdapter.notifyDataSetChanged(); } }); // 处理减少个数 viewHolder.mItemMove.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 如果选中 减少后要减少总价格 if (mData.get(position).isCheckState()) { allMoney -= mData.get(position).getGoodPrice(); mAllMoney.setText("总价" + initNumber(allMoney)); } if (mData.get(position).getGoodNumber() - 1 > 0) { mData.get(position).setGoodNumber(mData.get(position).getGoodNumber() - 1); } else { mData.remove(position); } mAdapter.notifyDataSetChanged(); } }); return convertView; } class ViewHolder { ImageView mItemCheck; ImageView mItemImage; TextView mItemName; TextView mItemGuige; TextView mItemPrice; TextView mItemMove; TextView mItemNumber; TextView mItemAdd; } } //处理double数据 四舍五入保留两位小数 public static double initNumber(double oldNum) { return new BigDecimal(oldNum).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); } }
——————————————我是分割线——————————————————
这是主activity的布局
———————————————我是分割线———————————————————
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="activitytest.example.com.shopcar.MainActivity"> <com.baoyz.swipemenulistview.SwipeMenuListView android:id="@+id/allList" android:layout_width="match_parent" android:layout_height="match_parent"> </com.baoyz.swipemenulistview.SwipeMenuListView> <LinearLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_alignParentBottom="true" android:background="#808080" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/allCkeck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选" /> <TextView android:id="@+id/allMoney" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="总价:" /> </LinearLayout> </RelativeLayout>
———————————————我是分割线—————————————————————————
这是数据JavaBean
———————————我是分割线———————————————
package activitytest.example.com.shopcar; import java.math.BigDecimal; /** * Created by 金金 on 2016/12/13. */ public class GoodBean { private boolean checkState; private int imageId; private String goodName; private String goodGuige; private double goodPrice; private int goodNumber; public boolean isCheckState() { return checkState; } public void setCheckState(boolean checkState) { this.checkState = checkState; } public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; } public String getGoodName() { return goodName; } public void setGoodName(String goodName) { this.goodName = goodName; } public String getGoodGuige() { return goodGuige; } public void setGoodGuige(String goodGuige) { this.goodGuige = goodGuige; } public double getGoodPrice() { return goodPrice; } public void setGoodPrice(double goodPrice) { this.goodPrice = new BigDecimal(goodPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); } public int getGoodNumber() { return goodNumber; } public void setGoodNumber(int goodNumber) { this.goodNumber = goodNumber; } public GoodBean(boolean checkState, int imageId, String goodName, String goodGuige, double goodPrice, int goodNumber) { this.checkState = checkState; this.imageId = imageId; this.goodName = goodName; this.goodGuige = goodGuige; this.goodPrice = new BigDecimal(goodPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); this.goodNumber = goodNumber; } }
————————————我是分割线————————————————————
这是ListView 的item布局
——————————————我是分割线————————————————————
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:background="#8a8a8a" android:padding="8dp"> <ImageView android:id="@+id/itemCheck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:src="@mipmap/ic_uncheck" /> <ImageView android:id="@+id/itemImage" android:layout_width="80dp" android:layout_height="80dp" android:layout_toRightOf="@+id/itemCheck" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_toRightOf="@+id/itemImage" android:orientation="vertical" android:weightSum="3"> <TextView android:id="@+id/itemName" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:text="商品名" /> <TextView android:id="@+id/itemGuige" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:text="商品规格" /> <TextView android:id="@+id/itemPrice" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:text="商品价格" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true"> <TextView android:id="@+id/itemMove" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_move" /> <TextView android:id="@+id/itemNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="100" /> <TextView android:id="@+id/itemAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_add" /> </LinearLayout> </RelativeLayout>
——————————————————我是分割线————————————————————————————
剩下的就是些图片了
SwipeMenuListView开源项目地址
https://github.com/baoyongzhang/SwipeMenuListView
源码在这
http://download.csdn.net/detail/rosener/9710694
完了