1. 程式人生 > >仿京東購物車

仿京東購物車

public interface CartView {
    void success(CartBean cartBean);
    void failure(String msg);
}
public interface CartAllCheckLinstener {
    void notifyAllCheckboxStatus();
}
<?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="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ccc"
        android:gravity="center">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:gravity="center_horizontal"
            android:text="購物車"
            android:textSize="25dp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="編輯"
            android:textSize="18dp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#909090" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/carGV"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8" />

    <RelativeLayout
        android:id="@+id/cart_bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.75"
        android:background="#CCCCCC"
        android:padding="5dp"
        android:textColor="#C0C0C0">

        <CheckBox
            android:id="@+id/allCheckbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/totalpriceTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/allCheckbox"
            android:text="總價:¥0.0" />

        <Button
            android:id="@+id/buy"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="#f70404"
            android:gravity="center"
            android:text="去結算"
            android:textColor="#fff"
            android:textSize="20dp" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#CCCCCC" />


</LinearLayout>
cartfragment_layout
<?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="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ccc"
        android:gravity="center">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:gravity="center_horizontal"
            android:text="購物車"
            android:textSize="25dp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="編輯"
            android:textSize="18dp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#909090" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/carGV"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8" />

    <RelativeLayout
        android:id="@+id/cart_bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.75"
        android:background="#CCCCCC"
        android:padding="5dp"
        android:textColor="#C0C0C0">

        <CheckBox
            android:id="@+id/allCheckbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/totalpriceTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/allCheckbox"
            android:text="總價:¥0.0" />

        <Button
            android:id="@+id/buy"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="#f70404"
            android:gravity="center"
            android:text="去結算"
            android:textColor="#fff"
            android:textSize="20dp" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#CCCCCC" />


</LinearLayout>
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> implements CheckListener, ItemTouchHelperAdapter {

    Context context;
    List<CartBean.DataBean> list;
    CheckListener checkListener;
    CartAllCheckLinstener cartAllCheckLinstener;
    private List<CartBean.DataBean.ListBean> listBeans;

    public void setCartAllCheckLinstener(CartAllCheckLinstener cartAllCheckLinstener) {
        this.cartAllCheckLinstener = cartAllCheckLinstener;
    }

    public CartAdapter(Context context, List<CartBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.cart_itemlayout, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final CartBean.DataBean dataBean = list.get(position);
        holder.sellerName.setText(list.get(position).getSellerName());
        holder.checkBox.setChecked(dataBean.isSelected());

        for (int i = 0; i < dataBean.getList().size(); i++) {
            if (!dataBean.getList().get(i).isSelected()) {
                holder.checkBox.setChecked(false);
                break;
            } else {
                holder.checkBox.setChecked(true);
            }
        }
        listBeans = this.list.get(position).getList();
        CartRevAdapter adapter = new CartRevAdapter(context, this.list.get(position).getList());
        holder.recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new CartRevAdapter.OnItemClickListener() {
            @Override
            public void onItemClicklistener(int ItemPosition) {
                boolean inCheck = listBeans.get(ItemPosition).isSelected();
                if (!inCheck){
                    list.get(position).setSelected(false);
                }else {
                    for (int i = 0; i < listBeans.size(); i++) {
                        if (listBeans.get(i).isSelected()){
                            list.get(position).setSelected(true);
                        }else {
                            list.get(position).setSelected(false);
                        }
                    }
                }
                notifyItemChanged(position);

            }
        });

        for (int i = 0; i < dataBean.getList().size(); i++) {
            if (!dataBean.getList().get(i).isSelected()) {
                holder.checkBox.setChecked(false);
                break;
            } else {
                holder.checkBox.setChecked(true);
            }
        }

        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.checkBox.isChecked()) {
                    dataBean.setSelected(true);
                    for (int i = 0; i < dataBean.getList().size(); i++) {
                        dataBean.getList().get(i).setSelected(true);
                    }
                } else {
                    dataBean.setSelected(false);
                    for (int i = 0; i < dataBean.getList().size(); i++) {
                        dataBean.getList().get(i).setSelected(false);
                    }
                }
                notifyDataSetChanged();
                if (cartAllCheckLinstener != null) {
                    cartAllCheckLinstener.notifyAllCheckboxStatus();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void notifyParpen() {
        notifyDataSetChanged();
        if (cartAllCheckLinstener != null) {
            cartAllCheckLinstener.notifyAllCheckboxStatus();
        }
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(list, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onItemDismiss(int position) {
        list.remove(position);
        notifyItemChanged(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        CheckBox checkBox;
        RecyclerView recyclerView;
        TextView sellerName;

        public ViewHolder(View itemView) {
            super(itemView);

            sellerName = itemView.findViewById(R.id.sellerName);
            checkBox = itemView.findViewById(R.id.checkBox);
            recyclerView = itemView.findViewById(R.id.cartYiRev);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        }
    }

    public List<CartBean.DataBean> getDate() {
        return list;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/jia_jian_bg"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/jian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="-"
        android:textSize="25sp" />

    <View
        android:layout_width="1px"
        android:layout_height="match_parent"
        android:background="#999999" />

    <EditText
        android:id="@+id/num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#00000000"
        android:gravity="center"
        android:text="1" />

    <View
        android:layout_width="1px"
        android:layout_height="match_parent"
        android:background="#999999" />

    <TextView
        android:id="@+id/jia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="+"
        android:textSize="25sp" />
</LinearLayout>





public class MyJiaJianView extends LinearLayout {

    private TextView jiaTv,jiantv;
    private EditText numEt;
    private int num = 1;
    public MyJiaJianView(Context context) {
        this(context,null);
    }

    public MyJiaJianView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyJiaJianView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    //初始化自定義的佈局
    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.my_jiajian,this,true);
        jiantv = view.findViewById(R.id.jian);
        jiaTv = view.findViewById(R.id.jia);
        numEt = view.findViewById(R.id.num);
        numEt.setText(num+"");

        jiaTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                num++;
                numEt.setText(num+"");
                if (jiaJianListener!=null){
                    jiaJianListener.getNum(num);
                }
            }
        });
        jiantv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                num--;
                if (num<=0){
                    Toast.makeText(getContext(), "數量不能小於1", Toast.LENGTH_SHORT).show();
                    num = 1;
                }
                numEt.setText(num+"");

                if (jiaJianListener!=null){
                    jiaJianListener.getNum(num);
                }
            }
        });
    }
    public void setNumEt(int n) {
        numEt.setText(n+"");
        num = Integer.parseInt(numEt.getText().toString());
    }
    private JiaJianListener jiaJianListener;

    public void setJiaJianListener(JiaJianListener jiaJianListener) {
        this.jiaJianListener = jiaJianListener;
    }
    public interface JiaJianListener{
        void getNum(int num);
    }
}

cart_rev_item
<?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:layout_gravity="center_horizontal"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <CheckBox
        android:id="@+id/Rev_item_chekBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/Rev_item_sim"
        android:layout_width="80dp"
        android:background="@mipmap/ic_launcher"
        android:layout_marginLeft="15dp"
        android:layout_height="80dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/Rev_item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/app_name" />

        <RelativeLayout
            android:layout_marginTop="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/Rev_item_product"
                android:layout_alignParentLeft="true"
                android:text="優惠價:¥99.99"
                android:textColor="@android:color/holo_red_light"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <com.example.jingd.app.MyJiaJianView
                android:id="@+id/jiajianqi"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </RelativeLayout>
    </LinearLayout>


</LinearLayout>


public class CartRevAdapter extends RecyclerView.Adapter<CartRevAdapter.ViewHolder> {

    Context context;
    List<CartBean.DataBean.ListBean> list;
    CheckListener checkListener;
    CartAllCheckLinstener cartAllCheckLinstener;
    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setCheckListener(CheckListener checkListener) {
        this.checkListener = checkListener;
    }

    public void setCartAllCheckLinstener(CartAllCheckLinstener cartAllCheckLinstener) {
        this.cartAllCheckLinstener = cartAllCheckLinstener;
    }

    public CartRevAdapter(Context context, List<CartBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.cart_rev_item, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final CartBean.DataBean.ListBean listBean = list.get(position);
        holder.product.setText(list.get(position).getPrice() + "");
        holder.title.setText(list.get(position).getTitle());
        Uri uri = Uri.parse(list.get(position).getImages().split("\\|")[0]);
        holder.simpleDraweeView.setImageURI(uri);
        holder.checkBox.setChecked(list.get(position).isSelected());

        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.checkBox.isChecked()) {
                    list.get(position).setSelected(true);
                } else {
                    list.get(position).setSelected(false);
                }
                notifyItemChanged(position);
                onItemClickListener.onItemClicklistener(position);
            }
        });

        holder.myJiaJianView.setNumEt(list.get(position).getNum());
        holder.myJiaJianView.setJiaJianListener(new MyJiaJianView.JiaJianListener() {
            @Override
            public void getNum(int num) {
                listBean.setNum(num);
                if (checkListener != null) {
                    checkListener.notifyParpen();
                }
            }
        });
        holder.myJiaJianView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (holder.checkBox.isChecked()) {
                    listBean.setSelected(true);
                } else {
                    listBean.setSelected(false);
                }
                if (checkListener != null) {
                    checkListener.notifyParpen();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView product, title;
        CheckBox checkBox;
        SimpleDraweeView simpleDraweeView;
        MyJiaJianView myJiaJianView;

        public ViewHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.Rev_item_chekBox);
            simpleDraweeView = itemView.findViewById(R.id.Rev_item_sim);
            title = itemView.findViewById(R.id.Rev_item_title);
            product = itemView.findViewById(R.id.Rev_item_product);
            myJiaJianView = itemView.findViewById(R.id.jiajianqi);
        }
    }

    interface OnItemClickListener {
        void onItemClicklistener(int position);
    }
}


public class CartFragment extends Fragment implements CartView, CartAllCheckLinstener {

    @BindView(R.id.carGV)
    RecyclerView carGV;
    @BindView(R.id.allCheckbox)
    CheckBox allCheckbox;
    @BindView(R.id.totalpriceTv)
    TextView totalpriceTv;
    @BindView(R.id.buy)
    Button buy;
    Unbinder unbinder;
    private CartAdapter adapter;
    private List<CartBean.DataBean> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.cartfragment_layout, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        CartPresenter cartPresenter = new CartPresenter(this);
        cartPresenter.getCartData(71);
        initView();
        initData();
        return inflate;
    }

    private void initData() {
        list = new ArrayList<>();
        allCheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (allCheckbox.isChecked()) {
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setSelected(true);
                            for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
                                list.get(i).getList().get(i1).setSelected(true);
                            }
                        }
                    }
                } else {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setSelected(false);
                        for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
                            list.get(i).getList().get(i1).setSelected(false);
                        }
                    }
                }
                adapter.notifyDataSetChanged();
                numm();
            }
        });
    }

    private void numm() {
        double product = 0;
        for (int i = 0; i < adapter.getDate().size(); i++) {
            for (int i1 = 0; i1 < adapter.getDate().get(i).getList().size(); i1++) {
                if (adapter.getDate().get(i).getList().get(i1).isSelected()) {
                    CartBean.DataBean.ListBean listBean = adapter.getDate().get(i).getList().get(i1);
                    product += listBean.getBargainPrice () * listBean.getTotalNum();
                }
            }
            totalpriceTv.setText("總價:¥" + product);

        }
    }

    private void initView() {
        carGV.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void success(CartBean cartBean) {
        list = cartBean.getData();
        adapter = new CartAdapter(getActivity(), cartBean.getData());
        carGV.setAdapter(adapter);
        adapter.setCartAllCheckLinstener(this);

    }

    @Override
    public void failure(String msg) {

    }

    @Override
    public void notifyAllCheckboxStatus() {
        StringBuilder stringBuilder = new StringBuilder();
        if (adapter != null) {
            for (int i = 0; i < adapter.getDate().size(); i++) {
                stringBuilder.append(adapter.getDate().get(i).isSelected());
                for (int i1 = 0; i1 < adapter.getDate().get(i).getList().size(); i1++) {
                    stringBuilder.append(adapter.getDate().get(i).getList().get(i1).isSelected());
                }
            }
        }
        if (stringBuilder.toString().contains("false")) {
            allCheckbox.setChecked(false);
        } else {
            allCheckbox.setChecked(true);
        }
        numm();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}