implementation 'com.android.support:recyclerview-v7:27.+'
implementation 'com.github.bumptech.glide:glide:4.8.0'
适配器
public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartHolder>{
private Context context;
private List<ShopCarBean.CartlistBean> data;
public ShopCartAdapter(Context context, List<ShopCarBean.CartlistBean> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public ShopCartHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ShopCartHolder(LayoutInflater.from(context).inflate(R.layout.shop_cart_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ShopCartHolder holder, final int position) {
/* 商品图片 */
Glide.with(context).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1537181886761&di=576079fc797f5c11e552783c84a49b6c&imgtype=0&src=http%3A%2F%2Fpic22.nipic.com%2F20120702%2F2129594_171228709393_2.jpg").into(holder.ivShopCartClothPic);
/* 商品的基本信息 */
holder.tvShopCartClothColor.setText("颜色:" + data.get(position).getColor());
holder.tvShopCartClothSize.setText("尺寸:" + data.get(position).getSize());
holder.tvShopCartClothName.setText(data.get(position).getProductName());
holder.tvShopCartShopName.setText(data.get(position).getShopName());
holder.tvShopCartClothPrice.setText("¥" + data.get(position).getPrice());
holder.etShopCartClothNum.setText(data.get(position).getCount() + "");
/* 显示前面的选中状态 */
if (data.get(position).getIsSelect()){
holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));
}else {
holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));
}
if (data.get(position).getIsShopSelect()){
holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected));
}else {
holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected));
}
/* 判断是否显示商铺 */
if (position>0){
/* 判断是否是同一个商铺的商品 */
if (data.get(position).getShopId() == data.get(position - 1).getShopId()){
holder.llShopCartHeader.setVisibility(View.GONE);
}else {
holder.llShopCartHeader.setVisibility(View.VISIBLE);
}
}else {
holder.llShopCartHeader.setVisibility(View.VISIBLE);
}
/* 判断是否全选并计算 */
if (mOnResfreshListener != null){
boolean isSelect = false;
for (int i=0;i<data.size();i++){
if (!data.get(i).getIsSelect()){
isSelect = false;
break;
}else {
isSelect = true;
}
}
mOnResfreshListener.onResfresh(isSelect);
}
/* 商品数量加 */
holder.ivShopCartClothAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.get(position).setCount(data.get(position).getCount()+1);
notifyDataSetChanged();
}
});
/* 商品数量减 */
holder.ivShopCartClothMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (data.get(position).getCount()>1){
data.get(position).setCount(data.get(position).getCount()-1);
notifyDataSetChanged();
}
}
});
/* 删除操作 */
holder.ivShopCartClothDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.remove(position);
//重新排序,标记所有商品不同商铺第一个的商品位置
MainActivity.isSelectFirst(data);
notifyDataSetChanged();
}
});
/* 单个商品 选中状态 */
holder.ivShopCartClothSel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.get(position).setSelect(!data.get(position).getIsSelect());
//通过循环找出不同商铺的第一个商品的位置
for (int i = 0; i < data.size(); i++){
if (data.get(i).isFirst()){
//遍历去找出同一家商铺的所有商品的勾选情况
for (int j = 0; j < data.size(); j++){
//如果是同一家商铺的商品,并且其中一个商品是未选中,那么商铺的全选勾选取消
if (data.get(j).getShopId() == data.get(i).getShopId() && !data.get(j).getIsSelect()){
data.get(i).setShopSelect(false);
break;
}else {
//如果是同一家商铺的商品,并且所有商品是选中,那么商铺的选中全选勾选
data.get(i).setShopSelect(true);
}
}
}
}
notifyDataSetChanged();
}
});
/* 商铺选中状态 */
holder.ivShopCartShopSel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (data.get(position).isFirst()){
// 商铺选中状态执反
data.get(position).setShopSelect(!data.get(position).getIsShopSelect());
// 改变商品的选中状态和商铺一样
for (int i = 0; i < data.size(); i++){
if (data.get(i).getShopId() == data.get(position).getShopId()){
data.get(i).setSelect(data.get(position).getIsShopSelect());
}
}
notifyDataSetChanged();
}
}
});
}
@Override
public int getItemCount() {
return data == null?0:data.size();
}
//刷新的接口
private OnResFreshListener mOnResfreshListener;
public void setmOnResfreshListener(OnResFreshListener mOnResfreshListener) {
this.mOnResfreshListener = mOnResfreshListener;
}
}
适配器
OnResFreshListener//event
public interface OnResFreshListener {
void onResfresh(boolean isSelect);
}
OnResFreshListener
ShopCartHolder
public class ShopCartHolder extends RecyclerView.ViewHolder{
public ImageView ivShopCartShopSel;
public TextView tvShopCartShopName;
public TextView tvShopCartClothName;
public TextView tvShopCartClothPrice;
public TextView etShopCartClothNum;
public TextView tvShopCartClothColor;
public TextView tvShopCartClothSize;
public ImageView ivShopCartClothSel;
public ImageView ivShopCartClothMinus;
public ImageView ivShopCartClothAdd;
public ImageView ivShopCartClothDelete;
public ImageView ivShopCartClothPic;
public LinearLayout llShopCartHeader;
public ShopCartHolder(View itemView) {
super(itemView);
llShopCartHeader = itemView.findViewById(R.id.ll_shopcart_header);
ivShopCartShopSel = itemView.findViewById(R.id.iv_item_shopcart_shopselect);
tvShopCartShopName = itemView.findViewById(R.id.tv_item_shopcart_shopname);
tvShopCartClothName = itemView.findViewById(R.id.tv_item_shopcart_clothname);
tvShopCartClothPrice = itemView.findViewById(R.id.tv_item_shopcart_cloth_price);
etShopCartClothNum = itemView.findViewById(R.id.et_item_shopcart_cloth_num);
tvShopCartClothColor = itemView.findViewById(R.id.tv_item_shopcart_cloth_color);
tvShopCartClothSize = itemView.findViewById(R.id.tv_item_shopcart_cloth_size);
ivShopCartClothSel = itemView.findViewById(R.id.tv_item_shopcart_clothselect);
ivShopCartClothMinus = itemView.findViewById(R.id.iv_item_shopcart_cloth_minus);
ivShopCartClothAdd = itemView.findViewById(R.id.iv_item_shopcart_cloth_add);
ivShopCartClothPic = itemView.findViewById(R.id.iv_item_shopcart_cloth_pic);
ivShopCartClothDelete = itemView.findViewById(R.id.iv_item_shopcart_cloth_delete);
}
}
ShopCartHolder
mainActivity
public class MainActivity extends AppCompatActivity {
private TextView tvShopCartSubmit, tvShopCartSelect, tvShopCartTotalNum;
private RecyclerView rlvShopCart, rlvHotProducts;
private ShopCartAdapter mShopCartAdapter;
private LinearLayout llPay;
private RelativeLayout rlHaveProduct;
private List<ShopCarBean.CartlistBean> mAllOrderList = new ArrayList<>();
private ArrayList<ShopCarBean.CartlistBean> mGoPayList = new ArrayList<>();
private List<String> mHotProductsList = new ArrayList<>();
private TextView tvShopCartTotalPrice;
private int mCount, mPosition;
private float mTotalPrice1;
private boolean mSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvShopCartSelect = findViewById(R.id.tv_shopcart_addselect);
tvShopCartTotalPrice = findViewById(R.id.tv_shopcart_totalprice);
tvShopCartTotalNum = findViewById(R.id.tv_shopcart_totalnum);
rlHaveProduct = findViewById(R.id.rl_shopcart_have);
rlvShopCart = findViewById(R.id.rlv_shopcart);
llPay = findViewById(R.id.ll_pay);
tvShopCartSubmit = findViewById(R.id.tv_shopcart_submit);
rlvShopCart.setLayoutManager(new LinearLayoutManager(this));
mShopCartAdapter = new ShopCartAdapter(this, mAllOrderList);
rlvShopCart.setAdapter(mShopCartAdapter);
//实时监控全选按钮
mShopCartAdapter.setmOnResfreshListener(new OnResFreshListener() {
@Override
public void onResfresh(boolean isSelect) {
mSelect= isSelect;
if (isSelect){
Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
}else {
Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
}
// 计算总价
float mTotalPrice = 0;
int mTotalNum = 0;
mTotalPrice1 = 0;
mGoPayList.clear();
// 遍历所有商品 计算总价
for (int i = 0; i < mAllOrderList.size(); i++){
if (mAllOrderList.get(i).getIsSelect()){
mTotalPrice += Float.parseFloat(mAllOrderList.get(i).getPrice()) * mAllOrderList.get(i).getCount();
mTotalNum += 1;
mGoPayList.add(mAllOrderList.get(i));
}
mTotalPrice1 = mTotalPrice;
tvShopCartTotalPrice.setText("总价:" + mTotalPrice);
tvShopCartTotalNum.setText("共" + mTotalNum + "件商品");
}
}
});
//全选
tvShopCartSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSelect = !mSelect;
if (mSelect){
/* 全选 */
Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
for (int i = 0; i < mAllOrderList.size(); i++){
mAllOrderList.get(i).setSelect(true);
mAllOrderList.get(i).setShopSelect(true);
}
}else {
/* 全不选 */
Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected);
tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
for (int i = 0; i < mAllOrderList.size(); i++){
mAllOrderList.get(i).setSelect(false);
mAllOrderList.get(i).setShopSelect(false);
}
}
mShopCartAdapter.notifyDataSetChanged();
}
});
initData();
mShopCartAdapter.notifyDataSetChanged();
}
private void initData() {
for (int i = 0; i < 3; i++){
ShopCarBean.CartlistBean sb = new ShopCarBean.CartlistBean();
sb.setShopId(1);
sb.setPrice("1999.0");
sb.setDefaultPic("http://img2.3lian.com/2014/c7/25/d/40.jpg");
sb.setProductName("小米MIX手机");
sb.setShopName("小米旗舰店");
sb.setColor("玫瑰金");
sb.setCount(1);
mAllOrderList.add(sb);
}
for (int i = 0; i < 2; i++){
ShopCarBean.CartlistBean sb = new ShopCarBean.CartlistBean();
sb.setShopId(2);
sb.setPrice("2999.0");
sb.setDefaultPic("http://img2.3lian.com/2014/c7/25/d/40.jpg");
sb.setProductName("三星note6");
sb.setShopName("三星旗舰店");
sb.setColor("玫瑰金");
sb.setCount(1);
mAllOrderList.add(sb);
}
isSelectFirst(mAllOrderList);
}
public static void isSelectFirst(List<ShopCarBean.CartlistBean> list) {
// 1. 判断是否有商品 有商品 根据商品是否是第一个显示商铺
if (list.size() > 0){
//头个商品一定属于它所在商铺的第一个位置,isFirst标记为1.
list.get(0).setFirst(true);
for (int i = 1; i < list.size(); i++){
//每个商品跟它前一个商品比较,如果Shopid相同isFirst则标记为2,
//如果Shopid不同,isFirst标记为1.
if (list.get(i).getShopId() == list.get(i - 1).getShopId()){
list.get(i).setFirst(false);
}else {
list.get(i).setFirst(true);
}
}
}
}
}
mainActivity