day 27
实现购物车的物品删除
这里我是通过顶端toolbar的meun菜单设计点击事件完成的
tool.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()){ case R.id.action_kefu: Toast.makeText(mcontext,"客服",Toast.LENGTH_SHORT).show(); break; case R.id.action_write: Toast.makeText(mcontext,"编辑!",Toast.LENGTH_SHORT).show(); textView.setVisibility(View.GONE); button1.setVisibility(View.GONE); button2.setVisibility(View.VISIBLE); carAdapter.checkAll_None(false); checkBox.setChecked(false); break; case R.id.action_fenxiang: Toast.makeText(mcontext,"完成!",Toast.LENGTH_SHORT).show(); textView.setVisibility(View.VISIBLE); button1.setVisibility(View.VISIBLE); button2.setVisibility(View.GONE); carAdapter.checkAll_None(true); checkBox.setChecked(true); break; default: break; } return false; } });
这里我是通过布局代码把两个button设置在同一个位置并且把删除字样的button先设置为不可见
<Button android:id="@+id/btn_order" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:minHeight="60dp" android:minWidth="120dp" android:text="去结算" style="@style/bigRedButton"/> <Button android:id="@+id/btn_del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:visibility="gone" android:minHeight="60dp" android:minWidth="120dp" android:text="删除" style="@style/bigRedButton"/>
然后点击编辑后实现去结算按钮不可见,删除可见
功能删除键设置一个点击事件去调用Adapter里的declcart方法
button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { delcart(v); } });
public void delCart() { if(!isNull()) return ; for(Iterator iterator = carts.iterator(); iterator.hasNext();) { ShoppingCart cart = (ShoppingCart) iterator.next(); if(cart.isChecked()) { int position = carts.indexOf(cart); cartProvider.delete(cart); iterator.remove(); notifyItemRemoved(position); } } }