oooo
购物车 主布局
<com.jcodecraeer.xrecyclerview.XRecyclerView
Android:id="@+id/xrv_cart"
android:layout_width=“match_parent”
android:layout_height=“0dp”
android:layout_weight=“9”
></com.jcodecraeer.xrecyclerview.XRecyclerView>
adapter适配器
public class CartAdapter extends XRecyclerView.Adapter<CartAdapter.ViewHolder> {
private context context;
private List<Cart.ResultBean> list;
private List checkBoxs;
public CartAdapter(Context context) {
this.context = context;
list = new ArrayList<>();
checkBoxs = new ArrayList<>();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.cart_recy_item, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
Uri uri = Uri.parse(list.get(i).getPic());
viewHolder.cartSdvPic.setImageURI(uri);
viewHolder.cartTxtCommodityName.setText(list.get(i).getCommodityName());
viewHolder.cartTxtPrice.setText(""+list.get(i).getPrice());
viewHolder.cartAddCount.setNumber(list.get(i).getCount());
/**
* 点击商品时回调
*/
viewHolder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCartlistener !=null){
boolean cbChecked = viewHolder.cb.isChecked();
checkBoxs.get(i).setCk(cbChecked);
mCartlistener.GoodsChange();
}
}
});
viewHolder.cartAddCount.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {
@Override
public void onNumberChange(int num) {
if (mCartlistener !=null){
mCartlistener.NumChange(i,num);
}
}
});
}
@Override
public int getItemCount() {
if(list.size()>0){
for (int i = 0; i <list.size(); i++) {
checkBoxs.add(new CheckBoxs(false));
if(checkBoxs.size()==list.size()){
break;
}
}
}
return list.size();
}
public void setData(List<Cart.ResultBean> result) {
list = result;
notifydatasetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final SimpleDraweeView cartSdvPic;
private final TextView cartTxtCommodityName;
private final TextView cartTxtPrice;
private final MyAddSubView cartAddCount;
private final CheckBox cb;
public ViewHolder(@NonNull View itemView) {
super(itemView);
cb = itemView.findViewById(R.id.cb);
cartSdvPic = itemView.findViewById(R.id.cart_sdv_pic);
cartTxtCommodityName = itemView.findViewById(R.id.cart_txt_commodityName);
cartTxtPrice = itemView.findViewById(R.id.cart_txt_price);
cartAddCount = itemView.findViewById(R.id.cart_add_count);
}
}
/**
* 定义接口回调
*/
public interface Cartlistener{
void GoodsChange();
void NumChange(int index,int num);
}
private Cartlistener mCartlistener;
public void setCartlistener(Cartlistener cartlistener){
mCartlistener = cartlistener;
}
/**
* 计算商品总价
* @return
*/
public float setsumprice(){
float num = 0.0f;
for (int i = 0; i < list.size(); i++) {
if (checkBoxs.get(i).isCkeck() ==true){
int count = list.get(i).getCount();
int price = list.get(i).getPrice();
num+=count*price;
}
}
return num;
}
/**
* 改变商品数量
* @param index
* @param num
*/
public void changeshopnum(int index,int num){
list.get(index).setCount(num);
}
public void setAllshopselected(boolean b){
for (int i = 0; i < checkBoxs.size(); i++) {
checkBoxs.get(i).setCk(b?true:false);
}
}
/**
* 所有商品是否都选中
* @return
*/
public boolean isAllshopselected(){
for (int i = 0; i < list.size(); i++) {
if (checkBoxs.get(i).isCkeck() ==false){
return false;
}
}
return true;
}
/**
* 是否所有商品被选中
* @return
*/
public boolean isAllshopselecteds(){
mCartlistener.GoodsChange();
for (int i = 0; i < list.size(); i++) {
if (checkBoxs.get(i).isCkeck() == false){
return false;
}
}
return true;
}
public int setshopNum(){
int shopnum = 0;
for (int i = 0; i < list.size(); i++) {
if (checkBoxs.get(i).isCkeck() ==true){
shopnum += list.get(i).getCount();
}
}
return shopnum;
}
Adapter里的布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginleft="10dp"
android:orientation="horizontal"
android:padding="5dp">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/cart_sdv_pic"
android:layout_width="150dp"
android:layout_height="150dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/cart_txt_commodityName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margintop="70dp"
android:orientation="horizontal">
<TextView
android:id="@+id/cart_txt_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp" />
<com.bw.sellgoods.MyAddSubView
android:id="@+id/cart_add_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"></com.bw.sellgoods.MyAddSubView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
MyAddSubView
public class MyAddSubView extends LinearLayout implements View.OnClickListener {
private int number = 1;
private TextView sub_tv;
private TextView product_number_tv;
private TextView add_tv;
public MyAddSubView(Context context) {
this(context, null);
}
public MyAddSubView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyAddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = inflate(context, R.layout.cart_add_remove_view_layout, this);
sub_tv = view.findViewById(R.id.sub_tv);
product_number_tv = view.findViewById(R.id.product_number_tv);
add_tv = view.findViewById(R.id.add_tv);
sub_tv.setOnClickListener(this);
add_tv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sub_tv:
if (number > 1) {
--number;
product_number_tv.setText(number + "");
if (onNumberChangeListener != null) {
onNumberChangeListener.onNumberChange(number);
}
} else {
toast.maketext(getContext(), "不能再少了", Toast.LENGTH_SHORT).show();
}
break;
case R.id.add_tv:
++number;
product_number_tv.setText(number + "");
if (onNumberChangeListener != null) {
onNumberChangeListener.onNumberChange(number);
}
break;
}
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
product_number_tv.setText(number + "");
}
OnNumberChangeListener onNumberChangeListener;
public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
this.onNumberChangeListener = onNumberChangeListener;
}
public interface OnNumberChangeListener {
void onNumberChange(int num);
}
自定义加减器
<TextView
android:text="1"
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/product_number_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<TextView
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/add_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="+"
android:textSize="16sp" />
CheckBoxs
public class CheckBoxs {
boolean isCkeck;
public CheckBoxs(boolean isCkeck) {
this.isCkeck = isCkeck;
}
public boolean isCkeck() {
return isCkeck;
}
public void setCk(boolean ck) {
isCkeck = ck;
}
}