1.自定义加减器
布局
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="30dp"
android:gravity="center_vertical">
<TextView
android:background="@drawable/circle_border"
android:layout_weight="1"
android:id="@+id/sub_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="-"
android:textSize="16sp" />
<TextView
android:text="1"
android:layout_marginLeft="2dp"
android:background="@drawable/circle_border"
android:layout_weight="1"
android:textColor="#7716a4"
android:id="@+id/product_number_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
/>
<TextView
android:layout_marginLeft="2dp"
android:background="@drawable/circle_border"
android:layout_weight="1"
android:id="@+id/add_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="+"
android:textSize="16sp" />
</LinearLayout>
自定义加减器主页代码
public class MyAddSub extends LinearLayout implements View.OnClickListener {
private int number = 1;
private TextView sub_tv;
private TextView product_number_tv;
private TextView add_tv;
public MyAddSub(Context context) {
this(context, null);
}
public MyAddSub(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyAddSub(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = inflate(context, R.layout.my_addsub_item, 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);
}
}
2.首页布局
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.shopcart.ShopcartFragment"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/shoppingfragment_cartrecycler"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:layout_weight="9"></android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:layout_weight="1"
>
<CheckBox
android:id="@+id/shoppingfragment_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_10"
android:layout_gravity="center_vertical"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/dp_10"
android:textSize="@dimen/sp_12"
android:textColor="#666666"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="合计:"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/dp_20"
android:textSize="@dimen/sp_12"
android:textColor="#666666"
/>
<TextView
android:id="@+id/shoppingfragment_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥:6666"
android:layout_gravity="center_vertical"
android:textSize="@dimen/sp_16"
android:textColor="#f00"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/shoppingfragment_button"
android:layout_width="@dimen/dp_118"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#ff5e70"
android:textSize="@dimen/sp_20"
android:text="去结算"
android:textColor="#fff"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
主页面逻辑代码
public class ShopcartFragment extends Fragment implements View.OnClickListener {
private RecyclerView shoppingfragment_cartrecycler;
private CheckBox shoppingfragment_checkbox;
private TextView shoppingfragment_price;
private Button shoppingfragment_button;
private List<MyShopCartBean.ResultBean> mResult;
private MyShopCartAdapter mMyShopCartAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment_shopcart, null);
initView(view);
initData();
return view;
}
private void initData() {
Util_OkHttpUtil2.setok().doGet(Constance.MY_SHOPCART, new Util_OkHttpUtil2.OkHttpCall() {
@Override
public void onFailure(Exception failureInfo) {
}
@Override
public void onResponse(String jsonString) {
MyShopCartBean myShopCartBean = new Gson().fromJson(jsonString, MyShopCartBean.class);
mResult = myShopCartBean.getResult();
//适配器
mMyShopCartAdapter = new MyShopCartAdapter(getActivity(), mResult);
//添加到适配器
shoppingfragment_cartrecycler.setAdapter(mMyShopCartAdapter);
isCheckBoxandnumber(mResult);
}
});
}
private void isCheckBoxandnumber(List<MyShopCartBean.ResultBean> result) {
//调用adapter的接口中的两个方法
mMyShopCartAdapter.setMyShopCartAdapterInterface(new MyShopCartAdapter.MyShopCartAdapterInterface() {
@Override
public void onCheckBoxChange(Boolean flag) {
//改变多选框状态
shoppingfragment_checkbox.setChecked(flag);
//更新价格
isCheckBoxAndNum();
}
@Override
public void onPruductNumberChange(int posiion, int number) {
//加减器
mMyShopCartAdapter.changeProductNumber(posiion,number);
//更新价格
isCheckBoxAndNum();
}
});
//设置全选的按钮
shoppingfragment_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//获取adapter的list集合的get方法
List<MyShopCartBean.ResultBean> list = mMyShopCartAdapter.getList();
for (int i = 0; i < list.size(); i++) {
list.get(i).setCheck(shoppingfragment_checkbox.isChecked());
}
isCheckBoxAndNum();
//更新一下适配器
mMyShopCartAdapter.notifyDataSetChanged();
}
});
}
private void isCheckBoxAndNum() {
//调用适配器的计算所有商品的价格
float allProduct = mMyShopCartAdapter.allProduct();
//把价格设置上去
shoppingfragment_price.setText("¥:"+allProduct);
}
private void initView(View view) {
shoppingfragment_cartrecycler = (RecyclerView) view.findViewById(R.id.shoppingfragment_cartrecycler);
shoppingfragment_checkbox = (CheckBox) view.findViewById(R.id.shoppingfragment_checkbox);
shoppingfragment_price = (TextView) view.findViewById(R.id.shoppingfragment_price);
shoppingfragment_button = (Button) view.findViewById(R.id.shoppingfragment_button);
shoppingfragment_button.setOnClickListener(this);
//设置布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
shoppingfragment_cartrecycler.setLayoutManager(linearLayoutManager);
}
/**
* 去付款按钮
* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.shoppingfragment_button:
break;
}
}
}
3.适配器
适配器的布局
适配器的布局代码
<?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="horizontal">
<CheckBox
android:id="@+id/shoppingfragment_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10"
android:focusable="false" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/shoppingfragment_item_simpledraweeview"
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_100"
android:layout_margin="10dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_100"
android:orientation="vertical"
android:layout_marginTop="@dimen/dp_4"
android:layout_marginBottom="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_10">
<TextView
android:id="@+id/shoppingfragment_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/shoppingfragment_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/sp_16"
android:textColor="#f00"
android:text="66666"/>
<com.bawie.shopmkk.fragment.shopcart.MyAddSub
android:id="@+id/shoppingfragment_item_shopnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/dp_10"
></com.bawie.shopmkk.fragment.shopcart.MyAddSub>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
适配器的主代码
public class MyShopCartAdapter extends RecyclerView.Adapter<MyShopCartAdapter.ViewHolder> {
private Context mContext;
private List<MyShopCartBean.ResultBean> mList;
public List<MyShopCartBean.ResultBean> getList() {
return mList;
}
public MyShopCartAdapter(Context context, List<MyShopCartBean.ResultBean> list) {
mContext = context;
mList = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext, R.layout.myshopbean_item,null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
//获取下标
final MyShopCartBean.ResultBean resultBean = mList.get(i);
viewHolder.shoppingfragment_item_simpledraweeview.setImageURI(resultBean.getPic());
viewHolder.shoppingfragment_item_title.setText(resultBean.getCommodityName());
viewHolder.shoppingfragment_item_price.setText("¥"+resultBean.getPrice());
viewHolder.shoppingfragment_item_shopnum.setNumber(resultBean.getCount());
viewHolder.shoppingfragment_item_checkbox.setChecked(resultBean.isCheck());
//设置值 方法:设置自定义组件的接口回调
viewHolder.shoppingfragment_item_shopnum.setOnNumberChangeListener(new MyAddSub.OnNumberChangeListener() {
@Override
public void onNumberChange(int num) {
//改变多选框的接口回调
if (mMyShopCartAdapterInterface != null){
mMyShopCartAdapterInterface.onPruductNumberChange(i,num);
}
}
});
//设置加减器
viewHolder.shoppingfragment_item_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//如果选中为真
resultBean.setCheck(true);
}else {
//没有选中为false
resultBean.setCheck(false);
}
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final CheckBox shoppingfragment_item_checkbox;
private final SimpleDraweeView shoppingfragment_item_simpledraweeview;
private final TextView shoppingfragment_item_title;
private final TextView shoppingfragment_item_price;
private final MyAddSub shoppingfragment_item_shopnum;
public ViewHolder(@NonNull View itemView) {
super(itemView);
shoppingfragment_item_checkbox = itemView.findViewById(R.id.shoppingfragment_item_checkbox);
shoppingfragment_item_simpledraweeview = itemView.findViewById(R.id.shoppingfragment_item_simpledraweeview);
shoppingfragment_item_title = itemView.findViewById(R.id.shoppingfragment_item_title);
shoppingfragment_item_price = itemView.findViewById(R.id.shoppingfragment_item_price);
shoppingfragment_item_shopnum = itemView.findViewById(R.id.shoppingfragment_item_shopnum);
}
}
///购物车的操作
/**
* 判断购物车是否全选择
*/
public void isCheckAll(){
//循环总的数组的长度
for (int i = 0; i < mList.size(); i++) {
//看看他当前是否都选中
boolean flag = mList.get(i).isCheck();
if (flag == false){
//如果都没选中 设置为false
mMyShopCartAdapterInterface.onCheckBoxChange(false);
}
}
//否则为true
mMyShopCartAdapterInterface.onCheckBoxChange(true);
}
/**
* 当加减器点击切换数字的时候 下标 数量
*/
public void changeProductNumber(int position,int number){
MyShopCartBean.ResultBean resultBean = mList.get(position);
resultBean.setCount(number);
}
/**
* 计算和总价
*/
public float allProduct(){
float num = 0;
for (int i = 0; i < mList.size(); i++) {
MyShopCartBean.ResultBean resultBean = mList.get(i);
//只算选中的
boolean check = resultBean.isCheck();
if (check == true){
int count = resultBean.getCount();
int price = resultBean.getPrice();
num = count * price;
}
}
return num;
}
///接口回调
public interface MyShopCartAdapterInterface{
/**
* 改变多选框
* @param flag
*/
void onCheckBoxChange(Boolean flag);
/**
* 加减器
* @param posiion
*/
void onPruductNumberChange(int posiion, int number);
}
private MyShopCartAdapterInterface mMyShopCartAdapterInterface;
public void setMyShopCartAdapterInterface(MyShopCartAdapterInterface myShopCartAdapterInterface){
mMyShopCartAdapterInterface = myShopCartAdapterInterface;
}
}
4.Bean类
public class MyShopCartBean {
/**
* result : [{"commodityId":5,"commodityName":"双头两用修容笔","count":3,"pic":"http://172.17.8.100/images/small/commodity/mzhf/cz/3/1.jpg","price":39},{"commodityId":6,"commodityName":"轻柔系自然裸妆假睫毛","count":4,"pic":"http://172.17.8.100/images/small/commodity/mzhf/cz/4/1.jpg","price":39}]
* message : 查询成功
* status : 0000
*/
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
/**
* commodityId : 5
* commodityName : 双头两用修容笔
* count : 3
* pic : http://172.17.8.100/images/small/commodity/mzhf/cz/3/1.jpg
* price : 39
*/
private int commodityId;
private String commodityName;
private int count;
private String pic;
private int price;
private boolean isCheck = false;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
public ResultBean(boolean isCheck) {
this.isCheck = isCheck;
}
public int getCommodityId() {
return commodityId;
}
public void setCommodityId(int commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
}
5.网址
//购物车查询
public static final String MY_SHOPCART = “http://172.17.8.100/small/order/verify/v1/findShoppingCart”; 公共参数