购物车页面的实现

购物车页面要实现的功能:用RecyclerView实现shoppingCart的显示;选择商品后购物车下方的价格总和会改变;实现编辑页面和完成页面的切换。
①布局页面实现
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

android:id="@id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="购物车"
app:rightButtonText="编辑"
android:layout_alignParentTop="true"
/>


android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
android:layout_marginBottom="50dp">


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#802f4f4f"
android:gravity="center_vertical"
>

android:id="@+id/checkbox_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:checked="true"
android:text="全选"
style="@style/customCheckbox"/>

android:id="@+id/txt_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkbox_all"
android:layout_marginLeft="40dp"
android:layout_centerVertical="true"
android:textSize="30dp"
android:text="合计"/>

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"/>

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"/>



②实现编辑页面和完成页面的切换
public void changeToolbar(){
mToolbar.hideSearchView();
mToolbar.showTitleView();
mToolbar.setTitle(R.string.cart);
mToolbar.getRightButton().setVisibility(View.VISIBLE);
mToolbar.setRightButtonText("编辑");
mToolbar.getRightButton().setOnClickListener(this);
mToolbar.getRightButton().setTag(ACTION_EDIT);
}
private void showDelControl(){
mToolbar.getRightButton().setText("完成");
mTextTotal.setVisibility(View.GONE);
mBtnOrder.setVisibility(View.GONE);
mBtnDel.setVisibility(View.VISIBLE);
mToolbar.getRightButton().setTag(ACTION_CAMPLATE);
mAdapter.checkAll_None(false);
mCheckBox.setChecked(false);
}
private voidhideDelControl(){
mTextTotal.setVisibility(View.VISIBLE);
mBtnOrder.setVisibility(View.VISIBLE);
mBtnDel.setVisibility(View.GONE);
mToolbar.setRightButtonText("编辑");
mToolbar.getRightButton().setTag(ACTION_EDIT);
mAdapter.checkAll_None(true);
mAdapter.showTotalPrice();
mCheckBox.setChecked(true);
}
@Override
public void onClick(View v) {
int action = (int) v.getTag();
if(ACTION_EDIT == action){
showDelControl();
}else if(ACTION_CAMPLATE == action){
hideDelControl();
}
}

③实例化CartProvider,显示购物车数据
cartProvider = new CartProvider(getActivity());

④得到数据源
List carts = cartProvider.getAll();

⑤实现Adapter
布局文件:
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_list_item"
android:padding="5dp"
android:gravity="center_vertical"
>

android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
style="@style/customCheckbox"/>

android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp">

android:id="@+id/drawee_view"
android:layout_width="180dp"
android:layout_height="180dp"
/>

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
>

android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="@color/gray"
android:textSize="16sp"
android:maxLines="3"
/>

android:id="@+id/text_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/crimson"
android:layout_gravity="right"
/>

android:id="@+id/num_control"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:minValue="https://www.it610.com/article/1"
/>




【购物车页面的实现】
CartAdapter:
public class CartAdapter extends SimpleAdapter implements BaseAdapter.OnItemClickListener {


public static final String TAG="CartAdapter";

private CheckBox checkBox;
private TextView textView;
private CartProvider cartProvider;

public CartAdapter(Context context, List datas, final CheckBox checkBox,TextView tv) {
super(context, R.layout.template_cart, datas);
setCheckBox(checkBox);
setTextView(tv);
cartProvider = new CartProvider(context);
setOnItemClickListener(this);
showTotalPrice();
}

@Override
protected void convert(BaseViewHolder viewHoder, final ShoppingCart item) {
viewHoder.getTextView(R.id.text_title).setText(item.getName());
viewHoder.getTextView(R.id.text_price).setText("¥"+item.getPrice());
SimpleDraweeView draweeView = (SimpleDraweeView) viewHoder.getView(R.id.drawee_view);
draweeView.setImageURI(Uri.parse(item.getImgUrl()));

CheckBox checkBox = (CheckBox) viewHoder.getView(R.id.checkbox);
checkBox.setChecked(item.isChecked());

NumberAddSubView numberAddSubView = (NumberAddSubView) viewHoder.getView(R.id.num_control);
numberAddSubView.setValue(item.getCount());
numberAddSubView.setOnButtonClickListener(new NumberAddSubView.OnButtonClickListener() {
@Override
public void onButtonAddClick(View view, int value) {
item.setCount(value);
cartProvider.update(item);
showTotalPrice();
}
@Override
public void onButtonSubClick(View view, int value) {
item.setCount(value);
cartProvider.update(item);
showTotalPrice();
}
});
}
privatefloat getTotalPrice(){
float sum=0;
if(!isNull())
return sum;
for (ShoppingCart cart: datas) {
if(cart.isChecked())
sum += cart.getCount()*cart.getPrice();
}
return sum;
}

public void showTotalPrice(){
float total = getTotalPrice();
textView.setText(Html.fromHtml("合计 ¥" + total + ""), TextView.BufferType.SPANNABLE);
}

private boolean isNull(){
return (datas !=null && datas.size()>0);
}
@Override
public void onItemClick(View view, int position) {
ShoppingCart cart =getItem(position);
cart.setIsChecked(!cart.isChecked());
notifyItemChanged(position);
checkListen();
showTotalPrice();
}

private void checkListen() {
int count = 0;
int checkNum = 0;
if (datas != null) {
count = datas.size();
for (ShoppingCart cart : datas) {
if (!cart.isChecked()) {
checkBox.setChecked(false);
break;
} else {
checkNum = checkNum + 1;
}
}
if (count == checkNum) {
checkBox.setChecked(true);
}
}
}

public void checkAll_None(boolean isChecked){
if(!isNull())
return ;
int i=0;
for (ShoppingCart cart :datas){
cart.setIsChecked(isChecked);
notifyItemChanged(i);
i++;
}
}

public void delCart(){
if(!isNull())
return ;
for(Iterator iterator = datas.iterator(); iterator.hasNext(); ){
ShoppingCart cart = (ShoppingCart) iterator.next();
if(cart.isChecked()){
int position = datas.indexOf(cart);
cartProvider.delete(cart);
iterator.remove();
notifyItemRemoved(position);
}
}
}
public void setTextView(TextView textview){
this.textView = textview;
}
public void setCheckBox(CheckBox ck){
this.checkBox = ck;
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAll_None(checkBox.isChecked());
showTotalPrice();
}
});
}
}

⑥将数据显示在RecyclerView中
private void showData(){
List carts = cartProvider.getAll();
mAdapter = new CartAdapter(getActivity(),carts,mCheckBox,mTextTotal);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL_LIST));
}

    推荐阅读