bean類
public class ShopCarBean {
private List<DataBean> data;
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public class DataBean{
private List<ListBean> list;
private String sellerName;
private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public List<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public class ListBean{
private String images;
private String title;
private float price;
private int num;
private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
}
}
主頁面`
public class FragMentShou extends Fragment implements ShopView {
private TextView mallprice;
private RecyclerView mRecyclerx;
private TextView mMoney;
private ShopAdapter shopAdapter;
private ShopPresenterIml shopPresenterIml;
private ShopCarBean shopCarBean;
private CheckBox mcheeckboxs;
private List<ShopCarBean.DataBean> datas;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shoufragment,container,false);
mRecyclerx = view.findViewById(R.id.recyclerx);
mallprice = view.findViewById(R.id.tv_allprice);
mMoney = view.findViewById(R.id.tv_money);
mcheeckboxs = view.findViewById(R.id.checkbox_all);
//設置佈局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerx.setLayoutManager(linearLayoutManager);
//設置適配器
shopAdapter = new ShopAdapter(getActivity());
mRecyclerx.setAdapter(shopAdapter);
//關聯p層
shopPresenterIml = new ShopPresenterIml(new ShopModelml(), this);
shopPresenterIml.doShopCar(“http://172.17.8.100/ks/product/getCarts?uid=51”);
//全選
//全選 全部的
mcheeckboxs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = mcheeckboxs.isChecked();
float price = 0;
int num = 0;
for (int i = 0; i < datas.size(); i++) {
//商家是否選中
datas.get(i).setChecked(checked);
List<ShopCarBean.DataBean.ListBean> beanList = datas.get(i).getList();
for (int j = 0; j < beanList.size(); j++) {
beanList.get(j).setChecked(checked);
if (checked) {
float prices = beanList.get(j).getPrice();
int nums = beanList.get(j).getNum();
price = prices + price * nums;
num = num + nums;
} else {
price = 0;
num = 0;
}
}
}
//設置總價 合計
mallprice.setText(price + "");
//數量
mMoney.setText("去結算(" + num + ")");
//重新賦值
shopAdapter.setdata(datas);
}
});
shopAdapter.setOnCallbacks(new ShopAdapter.OnCallbacks() {
@Override
public void changeData(List<ShopCarBean.DataBean> list) {
float price = 0;
int num = 0;
int ii = 0;
for (int i = 0; i < list.size(); i++) {
boolean checked = list.get(i).isChecked();
if (checked) {
ii++;
List<ShopCarBean.DataBean.ListBean> listBeans = list.get(i).getList();
for (int j = 0; j < listBeans.size(); j++) {
float prices = listBeans.get(j).getPrice();
int nums = listBeans.get(j).getNum();
price = prices + price * nums;
num = num + nums;
}
} else {
List<ShopCarBean.DataBean.ListBean> listBeans = list.get(i).getList();
for (int j = 0; j < listBeans.size(); j++) {
if (listBeans.get(j).isChecked()) {
float prices = listBeans.get(j).getPrice();
int nums = listBeans.get(j).getNum();
price = prices + price * nums;
num = num + nums;
}
}
}
}
if (ii==list.size()){
mcheeckboxs.setChecked(true);
}else{
mcheeckboxs.setChecked(false);
}
//設置總價 合計
mallprice.setText(price + "");
//數量
mallprice.setText("去結算(" + num + ")");
}
});
return view;
}
@Override
public void success(String data) {
shopCarBean = new Gson().fromJson(data, ShopCarBean.class);
datas = shopCarBean.getData();
datas.remove(0);
shopAdapter.setdata(datas);
}
@Override
public void fail(String error) {
}
}
主佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--最外層的列表 用來展示商家-->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/layout" />
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<CheckBox
android:id="@+id/checkbox_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="全選" />
<TextView
android:id="@+id/tv_allprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkbox_all"
android:text="合計:" />
<RelativeLayout
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:background="#d43c3c">
<TextView
android:id="@+id/tv_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="去結算"
android:textColor="#ffffff"
/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
`
adapter 主適配器加布局
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyViewHolder> {
private Context context;
private List<ShopCarBean.DataBean> lists = new ArrayList<>();
private ShopAdapterTwo shopAdapterTwo;
public ShopAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public ShopAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context,R.layout.item001,null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ShopAdapter.MyViewHolder myViewHolder, final int i) {
//設置商品選中
myViewHolder.mcheckboxs.setChecked(lists.get(i).isChecked());
myViewHolder.mtitles.setText(lists.get(i).getSellerName());
//設置佈局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myViewHolder.mxrecycler01.setLayoutManager(linearLayoutManager);
//子佈局的集合
final List<ShopCarBean.DataBean.ListBean> listBeans =lists.get(i).getList();
//設置適配器
shopAdapterTwo = new ShopAdapterTwo(context, listBeans);
myViewHolder.mxrecycler01.setAdapter(shopAdapterTwo);
//子適配器全選
shopAdapterTwo.setOnCallbacks(new ShopAdapterTwo.OnCallbacks() {
@Override
public void changeData(List<ShopCarBean.DataBean.ListBean> list) {
int nums=0;
for (int j = 0; j <listBeans.size() ; j++) {
boolean checked = listBeans.get(i).isChecked();
if (checked){
nums++;
}
}
if (nums==listBeans.size()){
//子商品全選
myViewHolder.mcheckboxs.setChecked(true);
}else {
//或者全不選
myViewHolder.mcheckboxs.setChecked(false);
}
if (onCallbacks!=null){
//回調
onCallbacks.changeData(lists);
}
}
});
//商家
myViewHolder.mcheckboxs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = myViewHolder.mcheckboxs.isChecked();
//賦值
lists.get(i).setChecked(checked);
List<ShopCarBean.DataBean.ListBean> listBeans = lists.get(i).getList();
for (int i = 0; i < listBeans.size(); i++) {
listBeans.get(i).setChecked(checked);//設置商品是否選中
}
if(onCallbacks!=null){
onCallbacks.changeData(lists);//回傳給調用頁面
}
//刷新適配器
notifyItemChanged(i);
}
});
}
@Override
public int getItemCount() {
return lists.size();
}
public void setdata(List<ShopCarBean.DataBean> list) {
this.lists=list;
notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
RecyclerView mxrecycler01;
TextView mtitles;
CheckBox mcheckboxs;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mcheckboxs = itemView.findViewById(R.id.checkboxs);
mtitles = itemView.findViewById(R.id.tv_titles);
//設置recycler
mxrecycler01 = itemView.findViewById(R.id.xrecycler01);
}
}
//接口回調 全選
private OnCallbacks onCallbacks;
public void setOnCallbacks(OnCallbacks onCallbacks) {
this.onCallbacks = onCallbacks;
}
public interface OnCallbacks{
void changeData(List<ShopCarBean.DataBean> list);
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="30dp">
<CheckBox
android:id="@+id/checkboxs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/tv_titles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/checkboxs" />
</RelativeLayout>
<!--展示商家下的商品-->
<android.support.v7.widget.RecyclerView
android:id="@+id/xrecycler01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout" />
</RelativeLayout>
adapter子適配器 加布局
public class ShopAdapterTwo extends RecyclerView.Adapter<ShopAdapterTwo.MyShopViewHodels> {
private Context context;
private List<ShopCarBean.DataBean.ListBean> listBeans = new ArrayList<>();
public ShopAdapterTwo(Context context, List<ShopCarBean.DataBean.ListBean> listBeans) {
this.context = context;
this.listBeans = listBeans;
}
@NonNull
@Override
public ShopAdapterTwo.MyShopViewHodels onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view =View.inflate(context,R.layout.item02,null);
MyShopViewHodels myShopViewHodels = new MyShopViewHodels(view);
return myShopViewHodels;
}
@Override
public void onBindViewHolder(@NonNull final ShopAdapterTwo.MyShopViewHodels myShopViewHodels, final int i) {
myShopViewHodels.mcheckboxall.setChecked(listBeans.get(i).isChecked());
myShopViewHodels.mshopviews.setNum(listBeans.get(i).getNum());
Glide.with(context).load(listBeans.get(i).getImages()).into(myShopViewHodels.mimageshop);
myShopViewHodels.mtitls.setText(listBeans.get(i).getTitle());
//接收數量發生變化
myShopViewHodels.mshopviews.setOnNumCallBackListener(new ShopAddView.OnNumCallBackListener() {
@Override
public void num(int num) {
listBeans.get(i).setNum(num);//設置給原來的商品
if(onCallbacks!=null){
onCallbacks.changeData(listBeans);
}
}
});
//商品的CheckBox
myShopViewHodels.mcheckboxall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked= myShopViewHodels.mcheckboxall.isChecked();
listBeans.get(i).setChecked(isChecked);
if(onCallbacks!=null){
onCallbacks.changeData(listBeans);
}
}
});
}
@Override
public int getItemCount() {
return listBeans.size();
}
public class MyShopViewHodels extends RecyclerView.ViewHolder {
ShopAddView mshopviews;
CheckBox mcheckboxall;
TextView mtitls;
ImageView mimageshop;
public MyShopViewHodels(@NonNull View itemView) {
super(itemView);
mimageshop = itemView.findViewById(R.id.imageshop);
mtitls = itemView.findViewById(R.id.titls);
mcheckboxall = itemView.findViewById(R.id.checkboxall);
mshopviews = itemView.findViewById(R.id.shopviews);
}
}
//接口回調 全選
private OnCallbacks onCallbacks;
public void setOnCallbacks(OnCallbacks onCallbacks) {
this.onCallbacks = onCallbacks;
}
public interface OnCallbacks{
void changeData(List<ShopCarBean.DataBean.ListBean> list);
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="100dp">
<CheckBox
android:id="@+id/checkboxall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/imageshop"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_toRightOf="@+id/checkboxall" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/imageshop">
<TextView
android:id="@+id/titls"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#d43c3c" />
<bw.bawei.com.lianxi04.ShopAddView
android:id="@+id/shopviews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
></bw.bawei.com.lianxi04.ShopAddView>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
自定義加減
public class ShopAddView extends RelativeLayout implements View.OnClickListener {
private EditText mEdit;
private Context mContext;
public ShopAddView(Context context) {
super(context);
init(context);
}
public ShopAddView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
//初始化layout
private void init(final Context context) {
this.mContext = context;
View view = View.inflate(context, R.layout.shop_car_add_view, null);
mEdit = (EditText) view.findViewById(R.id.ed_shop);
view.findViewById(R.id.tv_jian).setOnClickListener(this);
view.findViewById(R.id.tv_add).setOnClickListener(this);
mEdit.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) {
}
@Override
public void afterTextChanged(Editable s) {
Log.i("CharSequence", s.toString());
if (!TextUtils.isEmpty(s.toString()) && !s.toString().equals("-")) {
int num = Integer.parseInt(s.toString());
if ((s.toString().charAt(0) + "").equals("0")) {
num = 1;
}
if (num < 1) {
num = 1;
Toast.makeText(context, "請輸入最少一個商品", Toast.LENGTH_LONG).show();
mEdit.removeTextChangedListener(this);//先移除監聽,再設置文本,否則會無限死循環
mEdit.setText(num + "");//對輸入框文本進行分段顯示
mEdit.addTextChangedListener(this);
}
if (mOnNumCallBackListener != null) {
mOnNumCallBackListener.num(num);
}
}
}
});
addView(view);
}
public void setNum(int num) {
//設置數量
mEdit.setText(num + "");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_jian://減
String numString = mEdit.getText().toString().trim();
int num = Integer.parseInt(numString);
if (num <= 1) {
Toast.makeText(mContext, "您最少有一個商品", Toast.LENGTH_LONG).show();
return;
}
num--;
if (mOnNumCallBackListener != null) {
mOnNumCallBackListener.num(num);
}
mEdit.setText(num + "");
break;
case R.id.tv_add://加
String numStringAdd = mEdit.getText().toString().trim();
int numAdd = Integer.parseInt(numStringAdd);
numAdd++;
if (mOnNumCallBackListener != null) {
mOnNumCallBackListener.num(numAdd);
}
mEdit.setText(numAdd + "");
break;
}
}
private OnNumCallBackListener mOnNumCallBackListener;
public void setOnNumCallBackListener(OnNumCallBackListener mOnNumCallBackListener) {
this.mOnNumCallBackListener = mOnNumCallBackListener;
}
public interface OnNumCallBackListener {
void num(int num);
}
}
mvp
view層
*/
public interface ShopView {
void success(String data);
void fail(String error);
}
presenter層
public interface ShopPresenter {
void doShopCar(String url);
}
public class ShopPresenterIml implements ShopPresenter, ShopModel.CallBacks {
private ShopModel shopModel;
private ShopView shopView;
public ShopPresenterIml(ShopModel shopModel, ShopView shopView) {
this.shopModel = shopModel;
this.shopView = shopView;
}
@Override
public void doShopCar(String url) {
shopModel.doShopCar(url,this);
}
@Override
public void success(String data) {
shopView.success(data);
}
@Override
public void fail(String error) {
shopView.fail(error);
}
//解決內存泄漏
public void destory(){
if (shopModel!=null){
shopModel=null;
}
if (shopView!=null){
shopView=null;
}
}
}
model層
public interface ShopModel {
interface CallBacks{
void success(String data);
void fail(String error);
}
void doShopCar(String url,CallBacks callBacks);
}
public class ShopModelml implements ShopModel{
@Override
public void doShopCar(String url, final CallBacks callBacks) {
new OkHttpUtils().get(url).result(new OkHttpUtils.HttpListener() {
@Override
public void success(String data) {
callBacks.success(data);
}
@Override
public void fail(String error) {
callBacks.fail(error);
}
});
}
}