Activity:
public class ShopCarActivity extends AppCompatActivity implements IView, View.OnClickListener {
private ShopAdapter mShopAdapter;
private CheckBox mIvCircle;
private List<ShopBean.DataBean> mList = new ArrayList<>();
private TextView mAllPriceTxt, nSumPrice;
private IPresenterImpl mIPresenterImpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
mIPresenterImpl = new IPresenterImpl(this);
initView();
getData();
}
@Override
protected void onDestroy() {
super.onDestroy();
mIPresenterImpl.onDetach();
}
private void initView() {
mIvCircle = (CheckBox) findViewById(R.id.iv_cricle);
mAllPriceTxt = (TextView) findViewById(R.id.all_price);
nSumPrice = (TextView) findViewById(R.id.sum_price_txt);
mIvCircle.setOnClickListener(this);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
mShopAdapter = new ShopAdapter(this);
mRecyclerView.setAdapter(mShopAdapter);
mShopAdapter.setListener(new ShopAdapter.ShopCallBackListener() {
@Override
public void callBack(List<ShopBean.DataBean> list) {
//在这里重新遍历已经改状态后的数据,
// 这里不能break跳出,因为还需要计算后面点击商品的价格和数目,所以必须跑完整个循环
double totalPrice = 0;
//勾选商品的数量,不是该商品购买的数量
int num = 0;
//所有商品总数,和上面的数量做比对,如果两者相等,则说明全选
int totalNum = 0;
for (int a = 0; a < list.size(); a++) {
//获取商家里商品
List<ShopBean.DataBean.ListBean> listAll = list.get(a).getList();
for (int i = 0; i < listAll.size(); i++) {
totalNum = totalNum + listAll.get(i).getNum();
//取选中的状态
if (listAll.get(i).isCheck()) {
totalPrice = totalPrice + (listAll.get(i).getPrice() * listAll.get(i).getNum());
num = num + listAll.get(i).getNum();
}
}
}
if (num < totalNum) {
//不是全部选中
mIvCircle.setChecked(false);
} else {
//是全部选中
mIvCircle.setChecked(true);
}
mAllPriceTxt.setText("合计:" + totalPrice);
nSumPrice.setText("去结算(" + num + ")");
}
});
}
private void getData() {
Map<String, String> map = new HashMap<>();
map.put(Constants.MAP_KEY_GET_PRODUCT_UID, "71");
mIPresenterImpl.startRequest(Apis.URL_GET_SHOP_CAR_INFO, map, ShopBean.class);
}
@Override
public void showResponseData(Object data) {
if (data instanceof ShopBean) {
ShopBean shopBean = (ShopBean) data;
mList = shopBean.getData();
if (mList != null) {
mList.remove(0);
mShopAdapter.setList(mList);
}
}
}
@Override
public void showResponseFail(Object data) {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_cricle:
checkSeller(mIvCircle.isChecked());
mShopAdapter.notifyDataSetChanged();
break;
default:
}
}
/**
* 修改选中状态,获取价格和数量
*/
private void checkSeller(boolean bool) {
double totalPrice = 0;
int num = 0;
for (int a = 0; a < mList.size(); a++) {
//遍历商家,改变状态
ShopBean.DataBean dataBean = mList.get(a);
dataBean.setCheck(bool);
List<ShopBean.DataBean.ListBean> listAll = mList.get(a).getList();
for (int i = 0; i < listAll.size(); i++) {
//遍历商品,改变状态
listAll.get(i).setCheck(bool);
totalPrice = totalPrice + (listAll.get(i).getPrice() * listAll.get(i).getNum());
num = num + listAll.get(i).getNum();
}
}
if (bool) {
mAllPriceTxt.setText("合计:" + totalPrice);
nSumPrice.setText("去结算(" + num + ")");
} else {
mAllPriceTxt.setText("合计:0.00");
nSumPrice.setText("去结算(0)");
}
}
}
商家适配器:
ShopAdapter:
public class JiaAdapter extends RecyclerView.Adapter<JiaAdapter.ViewHolder> {
private List<ShopBean.DataBean> list;
private Context context;
public JiaAdapter(Context context) {
this.context = context;
//初始化
list=new ArrayList<>();
}
//加载数据
public void setList(List<ShopBean.DataBean> list) {
this.list = list;
notifyDataSetChanged();
}
public ShopBean.DataBean getitem(int position){
return list.get(position);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_shangjia, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.getdata(getitem(i),context,i);
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private CheckBox jia_box;
private TextView jia_name;
private RecyclerView jia_recy;
private PinAdapter pinAdapter;
public ViewHolder(@NonNull View itemView) {
super(itemView);
jia_box=itemView.findViewById(R.id.item_jia_box);
jia_name=itemView.findViewById(R.id.item_jia_name);
jia_recy=itemView.findViewById(R.id.item_jia_recy);
}
public void getdata(ShopBean.DataBean getitem, Context context, int i) {
//设置商家的名字
jia_name.setText(getitem.getSellerName());
//设置商家的复选框
jia_box.setChecked(getitem.isIscheck());
//商品的adapter
initRecy(getitem);
initData(getitem);
}
private void initData(final ShopBean.DataBean getitem) {
pinAdapter.setListener(new PinAdapter.ShopCallBackListener() {
@Override
public void callBack() {
//从商品适配里回调回来,回给activity
if (mShopCallBackListener!=null){
mShopCallBackListener.callBack(list);
}
List<ShopBean.DataBean.ListBean> mlist = getitem.getList();
//创建一个临时的标志位,用来记录心在的点击状态
boolean isAllChecked=true;
for (ShopBean.DataBean.ListBean bean:mlist){
if (!bean.isCheck()){
//只要有一个商品未选中,标志位设置成false 并跳出循环
isAllChecked=false;
break;
}
}
//刷新商家的状态
jia_box.setChecked(isAllChecked);
getitem.setIscheck(isAllChecked);
}
});
//监听checkbox的点击事件
//目的是改变旗下所有商品的选中状态
jia_box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//首先改变自己的标志位
getitem.setIscheck(jia_box.isChecked());
//调用产品adapter的方法,用来全选和反选
pinAdapter.selectOrRemoveAll(jia_box.isChecked());
}
});
}
private void initRecy(final ShopBean.DataBean getitem) {
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
jia_recy.setLayoutManager(linearLayoutManager);
pinAdapter= new PinAdapter(context);
pinAdapter.setList(getitem.getList());
jia_recy.setAdapter(pinAdapter);
}
}
public ShopCallBackListener mShopCallBackListener;
public void setListener(ShopCallBackListener listener){
this.mShopCallBackListener=listener;
}
public interface ShopCallBackListener{
void callBack(List<ShopBean.DataBean> list);
}
}
商品的适配器:
ProductsAdapter:
/**
* 展示商家里的商品
*/
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.MyViewHolder> {
private Context mContext;
private List<ShopBean.DataBean.ListBean> mList = new ArrayList<>();
public ProductsAdapter(Context context, List<ShopBean.DataBean.ListBean> list) {
this.mContext = context;
this.mList = list;
}
@NonNull
@Override
public ProductsAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext, R.layout.shop_car_adapter, null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull ProductsAdapter.MyViewHolder myViewHolder, final int i) {
String url = mList.get(i).getImages().split("\\|")[0].replace("https", "http");
Glide.with(mContext).load(url).into(myViewHolder.mImage);
myViewHolder.mTitle.setText(mList.get(i).getTitle());
myViewHolder.mPrice.setText(mList.get(i).getPrice() + "");
//根据我记录的状态,改变勾选
myViewHolder.mCheckBox.setChecked(mList.get(i).isCheck());
//商品的跟商家的有所不同,商品添加了选中改变的监听
myViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//优先改变自己的状态
mList.get(i).setCheck(isChecked);
//回调,目的是告诉activity,有人选中状态被改变
if (mShopCallBackListener != null) {
mShopCallBackListener.callBack();
}
}
});
//设置自定义View里的Edit
myViewHolder.mCustomShopCarPrice.setData(this, mList, i);
myViewHolder.mCustomShopCarPrice.setOnCallBack(new CustomCounterView.CallBackListener() {
@Override
public void callBack() {
if (mShopCallBackListener != null) {
mShopCallBackListener.callBack();
}
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
CustomCounterView mCustomShopCarPrice;
TextView mTitle, mPrice;
ImageView mImage;
CheckBox mCheckBox;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (ImageView) itemView.findViewById(R.id.iv_product);
mTitle = (TextView) itemView.findViewById(R.id.tv_product_title);
mPrice = (TextView) itemView.findViewById(R.id.tv_product_price);
mCheckBox = (CheckBox) itemView.findViewById(R.id.check_product);
mCustomShopCarPrice = (CustomCounterView) itemView.findViewById(R.id.custom_product_counter);
}
}
/**
* 在我们子商品的adapter中,修改子商品的全选和反选
*
* @param isSelectAll
*/
public void selectOrRemoveAll(boolean isSelectAll) {
for (ShopBean.DataBean.ListBean listBean : mList) {
listBean.setCheck(isSelectAll);
}
notifyDataSetChanged();
}
private ShopCallBackListener mShopCallBackListener;
public void setListener(ShopCallBackListener listener) {
this.mShopCallBackListener = listener;
}
public interface ShopCallBackListener {
void callBack();
}
}
自定义的view:
/**
* 加减数量
*/
public class CustomCounterView extends RelativeLayout implements View.OnClickListener {
private EditText editCar;
public CustomCounterView(Context context) {
super(context);
init(context);
}
public CustomCounterView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomCounterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private Context context;
private void init(Context context) {
this.context = context;
View view = View.inflate(context, R.layout.shop_car_price_layout, null);
ImageView addIamge = (ImageView) view.findViewById(R.id.add_car);
ImageView jianIamge = (ImageView) view.findViewById(R.id.jian_car);
editCar = (EditText) view.findViewById(R.id.edit_shop_car);
addIamge.setOnClickListener(this);
jianIamge.setOnClickListener(this);
addView(view);
editCar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//TODO:改变数量
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private int num;
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.add_car:
//改变数量,设置数量,改变对象内容,回调,局部刷新
num++;
editCar.setText(num + "");
list.get(position).setNum(num);
listener.callBack();
productsAdapter.notifyItemChanged(position);
break;
case R.id.jian_car:
if (num > 1) {
num--;
} else {
toast("我是有底线的啊");
}
editCar.setText(num + "");
list.get(position).setNum(num);
listener.callBack();
productsAdapter.notifyItemChanged(position);
break;
default:
break;
}
}
private void toast(String msg) {
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
//传递的数据
private List<ShopBean.DataBean.ListBean> list = new ArrayList<>();
private int position;
private ProductsAdapter productsAdapter;
public void setData(ProductsAdapter productsAdapter, List<ShopBean.DataBean.ListBean> list, int i) {
this.list = list;
this.productsAdapter = productsAdapter;
position = i;
num = list.get(i).getNum();
editCar.setText(num + "");
}
private CallBackListener listener;
public void setOnCallBack(CallBackListener listener) {
this.listener = listener;
}
public interface CallBackListener {
void callBack();
}
}
自定义view:
CustomCounterView:
/**
* 加减数量
*/
public class CustomCounterView extends RelativeLayout implements View.OnClickListener {
private EditText editCar;
public CustomCounterView(Context context) {
super(context);
init(context);
}
public CustomCounterView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomCounterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private Context context;
private void init(Context context) {
this.context = context;
View view = View.inflate(context, R.layout.shop_car_price_layout, null);
ImageView addIamge = (ImageView) view.findViewById(R.id.add_car);
ImageView jianIamge = (ImageView) view.findViewById(R.id.jian_car);
editCar = (EditText) view.findViewById(R.id.edit_shop_car);
addIamge.setOnClickListener(this);
jianIamge.setOnClickListener(this);
addView(view);
editCar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//TODO:改变数量
String s1 = s.toString();
try {
num = Integer.valueOf(s1);
}catch (Exception e){
num = 1;
// editText.setText(num+"");
}finally {
getitem.setNum(num);
if (mCustomClick!=null){
mCustomClick.cust();
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private int num;
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.add_car:
//改变数量,设置数量,改变对象内容,回调,局部刷新
num++;
editCar.setText(num + "");
list.get(position).setNum(num);
listener.callBack();
productsAdapter.notifyItemChanged(position);
break;
case R.id.jian_car:
if (num > 1) {
num--;
} else {
toast("我是有底线的啊");
}
editCar.setText(num + "");
list.get(position).setNum(num);
listener.callBack();
productsAdapter.notifyItemChanged(position);
break;
default:
break;
}
}
private void toast(String msg) {
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
//传递的数据
private List<ShopBean.DataBean.ListBean> list = new ArrayList<>();
private int position;
private ProductsAdapter productsAdapter;
public void setData(ProductsAdapter productsAdapter, List<ShopBean.DataBean.ListBean> list, int i) {
this.list = list;
this.productsAdapter = productsAdapter;
position = i;
num = list.get(i).getNum();
editCar.setText(num + "");
}
private CallBackListener listener;
public void setOnCallBack(CallBackListener listener) {
this.listener = listener;
}
public interface CallBackListener {
void callBack();
}
}
加减控件的布局:
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="match_parent">
<ImageView
android:id="@+id/num_image_minus"
android:layout_width="@dimen/dp_20"
android:layout_height="@dimen/dp_20"
android:src="@mipmap/jian"
android:layout_marginTop="@dimen/dp_3"
/>
<EditText
android:id="@+id/num_edit_price"
android:layout_width="@dimen/dp_31"
android:layout_height="@dimen/dp_20"
android:layout_marginLeft="@dimen/dp_5"
android:background="@drawable/shop_price_background"
android:gravity="center"
android:inputType="number"
android:textSize="@dimen/sp_12"
android:textColor="#666666"
android:layout_marginTop="@dimen/dp_5"
/>
<ImageView
android:id="@+id/num_image_add"
android:layout_width="@dimen/dp_20"
android:layout_height="@dimen/dp_20"
android:src="@mipmap/add"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginTop="@dimen/dp_3"
/>
</LinearLayout>
第二种加减的自定义view
public class CustomRelalayout extends RelativeLayout implements View.OnClickListener {
private EditText num_price;
private int num;
private SelectShopBean.ResultBean getitem;
public CustomRelalayout(Context context) {
super(context);
init(context);
}
public CustomRelalayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomRelalayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private Context context;
private void init(Context context) {
this.context = context;
View view = View.inflate(context, R.layout.shop_custom_price_num, null);
ImageView num_add = (ImageView) view.findViewById(R.id.num_image_add);
ImageView num_minus = (ImageView) view.findViewById(R.id.num_image_minus);
num_price = (EditText) view.findViewById(R.id.num_edit_price);
num_add.setOnClickListener(this);
num_minus.setOnClickListener(this);
addView(view);
num_price.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//TODO:改变数量
String s1 = s.toString();
try {
num = Integer.valueOf(s1);
}catch (Exception e){
num = 1;
}finally {
getitem.setCount(num);
if (listener!=null){
listener.callBack();
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public void onClick(View view) {
//改变数量,设置数量,改变对象内容,回调,局部刷新
int id = view.getId();
switch (id) {
case R.id.num_image_minus:
if (num > 1) {
num--;
num_price.setText(num + "");
getitem.setCount(num);
if (listener != null) {
listener.callBack();
}
} else {
Toast.makeText(context, "最后一个了,舍得吗", Toast.LENGTH_SHORT).show();
break;
}
break;
case R.id.num_image_add:
num++;
num_price.setText(num + "");
getitem.setCount(num);
if (listener != null) {
listener.callBack();
}
break;
default:
break;
}
}
//第二种传递数据
public void setnum(SelectShopBean.ResultBean getitem){
this.getitem=getitem;
num = getitem.getCount();
num_price.setText(num+"");
};
private CallBackListener listener;
public void setOnCallBack(CallBackListener listener) {
this.listener = listener;
}
public interface CallBackListener {
void callBack();
}
}