1.Addsum
public class Addsum extends LinearLayout {
private View view;
private TextView shop_recycler_jian;
private TextView shop_recycler_number;
private TextView shop_recycler_add;
public Addsum(Context context) {
super(context, null);
}
public Addsum(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public Addsum(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
initListener();
}
private void initListener() {
shop_recycler_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
add();
}
});
shop_recycler_jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
jian();
}
});
}
private void jian() {
String s = shop_recycler_number.getText().toString();
int parseInt = Integer.parseInt(s);
if (parseInt>0){
parseInt--;
setcurnum(parseInt);
}
}
private void add() {
String s = shop_recycler_number.getText().toString();
int parseInt = Integer.parseInt(s);
parseInt++;
setcurnum(parseInt);
}
private void initView(Context context) {
view = View.inflate(context, R.layout.add_sum, this);
shop_recycler_jian = view.findViewById(R.id.shop_recycler_jian);
shop_recycler_add = view.findViewById(R.id.shop_recycler_add);
shop_recycler_number = view.findViewById(R.id.shop_recycler_number);
shop_recycler_number.setText("0");
}
public void setcurnum(int parseInt) {
shop_recycler_number.setText(parseInt+"");
if (onNumCHangeLitener!=null){
onNumCHangeLitener.OnitemCLick(this,parseInt);
}
}
public interface OnNumCHangeLitener{
void OnitemCLick(View view,int sum);
}
OnNumCHangeLitener onNumCHangeLitener;
public void setOnNumCHangeLitener(OnNumCHangeLitener onNumCHangeLitener) {
this.onNumCHangeLitener = onNumCHangeLitener;
}
}
2.add_sum.xml
<?xml version="1.0" encoding="utf-8"?><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/shop_recycler_jian"
android:text="--"
android:padding="10dp"
android:textColor="#666666"
android:textSize="14sp"/>
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="@+id/shop_recycler_number"
android:gravity="center"
android:text="1231"
android:padding="10dp"
android:textColor="#666666"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/shop_recycler_add"
android:text="+"
android:padding="10dp"
android:textColor="#666666"
android:textSize="14sp"/>
3.佈局
<CheckBox
android:id="@+id/checkboxz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="13dp"
android:layout_marginLeft="13dp"
android:layout_marginTop="1dp"
android:focusable="false"
android:theme="@style/My_CheckBox" />
<ImageView
android:id="@+id/imagez"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
android:src="@mipmap/ic_launcher" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textz1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:maxLines="2"
android:text="sdfas"
android:textSize="16sp" />
<TextView
android:id="@+id/textz2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:text="sdfa" />
<bw.com.king.shopcar.Addsum
android:id="@+id/addsum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp">
</bw.com.king.shopcar.Addsum>
</RelativeLayout>
4.Adapter
public class ShopCarAdapter extends RecyclerView.Adapter {
private Context context;
private ShopCarBean shopCarBean;
private Addsum.OnNumCHangeLitener onNumCHangeLitener;
public ShopCarAdapter(Context context, ShopCarBean shopCarBean) {
this.context = context;
this.shopCarBean = shopCarBean;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.shopcar_item, viewGroup, false);
Holder holder = new Holder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
final Holder holder = (Holder) viewHolder;
Glide.with(context).load(shopCarBean.getResult().get(i).getPic()).into(holder.imageView);
holder.textView.setText(shopCarBean.getResult().get(i).getCommodityName());
holder.textView1.setText("¥"+shopCarBean.getResult().get(i).getPrice()+"");
holder.addsum.setcurnum(shopCarBean.getResult().get(i).getCount());
holder.addsum.setOnNumCHangeLitener(new Addsum.OnNumCHangeLitener() {
@Override
public void OnitemCLick(View view, int sum) {
shopCarBean.getResult().get(i).setCount(sum);
if (onNumCHangeLitener!=null){
onNumCHangeLitener.OnitemCLick(view,sum);
}
}
});
//全選
if (shopCarBean.getResult().get(i).getCheckbox()==1){
holder.checkBox1.setChecked(true);
}else {
holder.checkBox1.setChecked(false);
}
//單選
holder.checkBox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (shopCarBean.getResult().get(i).getCheckbox()==1){
onItemclick.onclick(i,0);
}else {
onItemclick.onclick(i,1);
}
}
});
}
public void setOnNumCHangeLitener(Addsum.OnNumCHangeLitener onNumCHangeLitener) {
this.onNumCHangeLitener = onNumCHangeLitener;
}
@Override
public int getItemCount() {
return shopCarBean.getResult().size();
}
public class Holder extends RecyclerView.ViewHolder {
private final CheckBox checkBox1;
private final ImageView imageView;
private final TextView textView;
private final TextView textView1;
private final Addsum addsum;
public Holder(@NonNull View itemView) {
super(itemView);
checkBox1 = itemView.findViewById(R.id.checkboxz);
imageView = itemView.findViewById(R.id.imagez);
textView = itemView.findViewById(R.id.textz1);
textView1 = itemView.findViewById(R.id.textz2);
addsum = itemView.findViewById(R.id.addsum);
}
}
public interface onItemclick{
void onclick(int i,int b);
}
onItemclick onItemclick;
public void setOnItemclick(ShopCarAdapter.onItemclick onItemclick) {
this.onItemclick = onItemclick;
}
}
5.Fragment
public class ThreeFragment extends Fragment implements ShopCarView {
private ShopCarPresenter shopCarPresenter = new ShopCarPresenter(this);
private RecyclerView shopcar_rec;
private CheckBox qx;
private TextView tvSum;
private Button jiesuan;
private ShopCarAdapter shopCarAdapter;
private Boolean checked;
private List<ShopCarBean.ResultBean> shopCarBeanResult;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
shopCarPresenter.showShopCar();
initView(view);
return view;
}
private void initView(View view) {
shopcar_rec = (RecyclerView) view.findViewById(R.id.shopcar_rec);
qx = (CheckBox) view.findViewById(R.id.qx);
tvSum = (TextView) view.findViewById(R.id.tvSum);
jiesuan = (Button) view.findViewById(R.id.jiesuan);
}
@Override
public void onSuccessShopCar(final ShopCarBean shopCarBean) {
shopCarBeanResult = shopCarBean.getResult();
//全選、反選
qx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
for (int i = 0; i <shopCarBean.getResult().size() ; i++) {
shopCarBean.getResult().get(i).setCheckbox(1);
}
}else {
for (int i = 0; i <shopCarBean.getResult().size() ; i++) {
shopCarBean.getResult().get(i).setCheckbox(0);
}
}
getTotal();
shopCarAdapter.notifyDataSetChanged();
}
});
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
shopcar_rec.setLayoutManager(linearLayoutManager);
shopCarAdapter = new ShopCarAdapter(getActivity(),shopCarBean);
shopcar_rec.setAdapter(shopCarAdapter);
shopCarAdapter.setOnItemclick(new ShopCarAdapter.onItemclick() {
@Override
public void onclick(int i, int b) {
shopCarBean.getResult().get(i).setCheckbox(b);
getTotal();
}
});
shopCarAdapter.setOnNumCHangeLitener(new Addsum.OnNumCHangeLitener() {
@Override
public void OnitemCLick(View view, int sum) {
getTotal();
}
});
}
private void getTotal(){
int sum=0;
for (int i = 0; i <shopCarBeanResult.size() ; i++) {
if (shopCarBeanResult.get(i).getCheckbox()==1){
int price = shopCarBeanResult.get(i).getPrice();
int count = shopCarBeanResult.get(i).getCount();
sum+=price*count;
}
}
tvSum.setText("合計:"+sum+"");
}
@Override
public void onFailerShopCar(String error) {
}
}
6.Bean
public class ShopCarBean {
/**
* 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 int checkbox ;
public ResultBean(int checkbox) {
this.checkbox = checkbox;
}
public int getCheckbox() {
return checkbox;
}
public void setCheckbox(int checkbox) {
this.checkbox = checkbox;
}
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;
}
}
}