Afrag.java
public class Afrag extends Fragment implements View.OnClickListener{
private ExpandableListView mExpandableListView;
private CheckBox mCheckBox;
private TextView mText_price;
private Button mButton;
MyAdapter adapter;
public String url="http://www.zhaoapi.cn/product/getCarts?uid=71";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.afrag, null);
mExpandableListView = view.findViewById(R.id.af_elv);
mCheckBox = view.findViewById(R.id.af_check);
mText_price = view.findViewById(R.id.af_price);
mButton = view.findViewById(R.id.af_btn);
mCheckBox.setOnClickListener(this);
initData();
return view;
}
private void initData() {
OkUtils.getInstance().doGet(url, new OkUtils.OnCallBack() {
@Override
public void onFail() {
}
@Override
public void onResponse(String json) {
ResponseBean responseBean = new Gson().fromJson(json, ResponseBean.class);
List<ResponseBean.DataBean> data = responseBean.getData();
adapter = new MyAdapter(getActivity(), data);
mExpandableListView.setAdapter(adapter);
for (int p=0;p<data.size();p++){
mExpandableListView.expandGroup(p);
}
adapter.setOnListChangeListener(new MyAdapter.onListChangeListener() {
@Override
public void onSjCheckedChange(int i) {
boolean selected = adapter.getSjAllSpSelected(i);
adapter.sjChangeSp(i,!selected);
adapter.notifyDataSetChanged();
refresh();
}
@Override
public void onSpCheckedChange(int i, int i1) {
adapter.changeSp(i,i1);
adapter.notifyDataSetChanged();
refresh();
}
@Override
public void onNumberCheckedChange(int i, int i1, int num) {
adapter.changeNumber(i,i1,num);
adapter.notifyDataSetChanged();
refresh();
}
});
}
});
}
public void refresh(){
boolean allSpSelected = adapter.getAllSpSelected();
mCheckBox.setChecked(allSpSelected);
int totalNum = adapter.getTotalNum();
mButton.setText("结算("+totalNum+")");
float totalPrice = adapter.getTotalPrice();
mText_price.setText("总价¥:"+totalPrice);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.af_check:
boolean spSelected = adapter.getAllSpSelected();
adapter.qxChangeAllSp(!spSelected);
adapter.notifyDataSetChanged();
refresh();
break;
}
}
}
afrag.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/af_elv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:background="#eee">
<CheckBox
android:id="@+id/af_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"/>
<TextView
android:id="@+id/af_price"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="总价¥:0.0"/>
<Button
android:id="@+id/af_btn"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="结算(0)"
android:layout_marginLeft="70dp"
android:background="@color/colorAccent"/>
</LinearLayout>
</LinearLayout>
MyAdapter.java
public class MyAdapter extends BaseExpandableListAdapter{
private Context mContext;
private List<ResponseBean.DataBean> list;
public MyAdapter(Context context, List<ResponseBean.DataBean> list) {
mContext = context;
this.list = list;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int i) {
return list.get(i).getList().size();
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
GroupViewHolder groupViewHolder;
if (view==null){
groupViewHolder = new GroupViewHolder();
view = View.inflate(viewGroup.getContext(), R.layout.item_car_group, null);
groupViewHolder.mCheckBox = view.findViewById(R.id.group_check);
groupViewHolder.mTextView = view.findViewById(R.id.group_text);
view.setTag(groupViewHolder);
}else {
groupViewHolder = (GroupViewHolder) view.getTag();
}
ResponseBean.DataBean dataBean = list.get(i);
groupViewHolder.mTextView.setText(dataBean.getSellerName());
boolean sjAllSpSelected = getSjAllSpSelected(i);
groupViewHolder.mCheckBox.setChecked(sjAllSpSelected);
groupViewHolder.mCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOnListChangeListener.onSjCheckedChange(i);
}
});
return view;
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
ChildViewHolder childViewHolder;
if (view==null){
childViewHolder = new ChildViewHolder();
view = View.inflate(viewGroup.getContext(), R.layout.item_car_chlid, null);
childViewHolder.mCheckBox = view.findViewById(R.id.child_check);
childViewHolder.mImageView = view.findViewById(R.id.child_img);
childViewHolder.mTextView_title = view.findViewById(R.id.child_title);
childViewHolder.mTextView_price = view.findViewById(R.id.child_price);
childViewHolder.mAddSubView = view.findViewById(R.id.child_add);
view.setTag(childViewHolder);
}else {
childViewHolder = (ChildViewHolder) view.getTag();
}
ResponseBean.DataBean dataBean = list.get(i);
List<ResponseBean.DataBean.ListBean> list1 = dataBean.getList();
ResponseBean.DataBean.ListBean listBean = list1.get(i1);
childViewHolder.mCheckBox.setChecked(listBean.getSelected()==1);
childViewHolder.mTextView_title.setText(listBean.getTitle());
childViewHolder.mTextView_price.setText(listBean.getPrice()+"");
childViewHolder.mAddSubView.setNumber(listBean.getNum());
childViewHolder.mCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOnListChangeListener.onSpCheckedChange(i,i1);
}
});
childViewHolder.mAddSubView.setOnNumberChangeListener(new AddSubView.onNumberChangeListener() {
@Override
public void onNumberChange(int num) {
mOnListChangeListener.onNumberCheckedChange(i,i1,num);
}
});
return view;
}
public boolean getSjAllSpSelected(int i){ //获取复选框被点击的商家全部商品状态
ResponseBean.DataBean dataBean = list.get(i);
List<ResponseBean.DataBean.ListBean> list1 = dataBean.getList();
for (ResponseBean.DataBean.ListBean bean:list1){
if (bean.getSelected()==0){
return false;
}
}
return true;
}
public boolean getAllSpSelected(){ //全选点击时获取所有商品状态
for (int i=0;i<list.size();i++){
ResponseBean.DataBean dataBean = list.get(i);
List<ResponseBean.DataBean.ListBean> list1 = dataBean.getList();
for (int j=0;j<list1.size();j++){
if (list1.get(j).getSelected()==0){
return false;
}
}
}
return true;
}
public int getTotalNum(){ //获取总数量
int totalNumber=0;
for (int i=0;i<list.size();i++){
ResponseBean.DataBean dataBean = list.get(i);
List<ResponseBean.DataBean.ListBean> list1 = dataBean.getList();
for (int j=0;j<list1.size();j++){
if (list1.get(j).getSelected()==1){
int num = list1.get(j).getNum();
totalNumber+=num;
}
}
}
return totalNumber;
}
public float getTotalPrice(){ //获取总价格
float totalPrice=0;
for (int i=0;i<list.size();i++){
ResponseBean.DataBean dataBean = list.get(i);
List<ResponseBean.DataBean.ListBean> list1 = dataBean.getList();
for (int j=0;j<list1.size();j++){
if (list1.get(j).getSelected()==1){
int num = list1.get(j).getNum();
float price = (float) list1.get(j).getPrice();
totalPrice+=num*price;
}
}
}
return totalPrice;
}
public void sjChangeSp(int i,boolean isSelected){ //商家改变旗下所有商品状态
ResponseBean.DataBean dataBean = list.get(i);
List<ResponseBean.DataBean.ListBean> list1 = dataBean.getList();
for (int j=0;j<list1.size();j++){
ResponseBean.DataBean.ListBean listBean = list1.get(j);
listBean.setSelected(isSelected?1:0);
}
}
public void changeSp(int i,int i1){
ResponseBean.DataBean dataBean = list.get(i);
List<ResponseBean.DataBean.ListBean> list1 = dataBean.getList();
ResponseBean.DataBean.ListBean listBean = list1.get(i1);
listBean.setSelected(listBean.getSelected()==0?1:0);
}
public void qxChangeAllSp(boolean isSelected){ //全选改变商品是否选中
for (int i=0;i<list.size();i++){
ResponseBean.DataBean dataBean = list.get(i);
List<ResponseBean.DataBean.ListBean> list1 = dataBean.getList();
for (int j=0;j<list1.size();j++){
list1.get(j).setSelected(isSelected?1:0);
}
}
}
public void changeNumber(int i,int i1,int number){ //改变数量
ResponseBean.DataBean dataBean = list.get(i);
List<ResponseBean.DataBean.ListBean> list1 = dataBean.getList();
ResponseBean.DataBean.ListBean listBean = list1.get(i1);
listBean.setNum(number);
}
public interface onListChangeListener{
void onSjCheckedChange(int i);
void onSpCheckedChange(int i,int i1);
void onNumberCheckedChange(int i,int i1,int number);
}
onListChangeListener mOnListChangeListener;
public void setOnListChangeListener(onListChangeListener onListChangeListener){
mOnListChangeListener=onListChangeListener;
}
class GroupViewHolder{
CheckBox mCheckBox;
TextView mTextView;
}
class ChildViewHolder{
CheckBox mCheckBox;
ImageView mImageView;
TextView mTextView_title;
TextView mTextView_price;
AddSubView mAddSubView;
}
@Override
public Object getGroup(int i) {
return null;
}
@Override
public Object getChild(int i, int i1) {
return null;
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
}
AddSubView.java
public class AddSubView extends LinearLayout implements View.OnClickListener{
public int number=1;
private TextView mAdd;
private TextView mSub;
private TextView mNum;
public AddSubView(Context context) {
this(context,null);
}
public AddSubView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = inflate(context, R.layout.add_remove, this);
mAdd = view.findViewById(R.id.add_tv);
mSub = view.findViewById(R.id.sub_tv);
mNum = view.findViewById(R.id.product_number_tv);
mAdd.setOnClickListener(this);
mSub.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.sub_tv:
if (number>1){
number--;
mNum.setText(number+"");
if (mOnNumberChangeListener!=null){
mOnNumberChangeListener.onNumberChange(number);
}
}else {
Toast.makeText(getContext(), "不能再少了", Toast.LENGTH_SHORT).show();
}
break;
case R.id.add_tv:
if (number<5){
number++;
mNum.setText(number+"");
if (mOnNumberChangeListener!=null){
mOnNumberChangeListener.onNumberChange(number);
}
}else {
Toast.makeText(getContext(), "不能再多了", Toast.LENGTH_SHORT).show();
}
break;
}
}
public int getNumber(){
return number;
}
public void setNumber(int number){
this.number=number;
mNum.setText(number+"");
}
public interface OnNumberChangeListener{
void onNumberChange(int number);
}
OnNumberChangeListener mOnNumberChangeListener;
public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener){
mOnNumberChangeListener=onNumberChangeListener;
}
}
add_remove.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="30dp"
android:background="#99000000"
android:padding="2dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<TextView
android:id="@+id/sub_jian"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="-"
android:textSize="16sp"
android:gravity="center"
android:background="#fff"/>
<TextView
android:id="@+id/sub_num"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="1"
android:textSize="16sp"
android:gravity="center"
android:background="#fff"/>
<TextView
android:id="@+id/sub_jia"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="+"
android:textSize="16sp"
android:gravity="center"
android:background="#fff"/>
</LinearLayout>
item_car_group.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/group_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/group_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="fff"/>
</LinearLayout>
item_car_chlid.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/child_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/child_img"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:src="@color/colorAccent"/>
<LinearLayout
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/child_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/child_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="ss"
android:textColor="@color/colorAccent"/>
</LinearLayout>
<fanruiqi.bwie.com.shopcartlx.view.AddSubView
android:id="@+id/child_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
OkUtils.java
public class OkUtils {
private static OkUtils mOkUtils;
Handler handler;
private final OkHttpClient mOkHttpClient;
private OkUtils() {
handler = new Handler(Looper.getMainLooper());
mOkHttpClient = new OkHttpClient.Builder()
.readTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000,TimeUnit.MILLISECONDS)
.connectTimeout(5000,TimeUnit.MILLISECONDS)
.build();
}
public static OkUtils getInstance(){
if (mOkUtils==null){
synchronized (OkUtils.class){
if (mOkUtils==null){
return mOkUtils=new OkUtils();
}
}
}
return mOkUtils;
}
public interface OnCallBack{
void onFail();
void onResponse(String json);
}
public void doGet(String url,final OnCallBack onCallBack){
Request request = new Request.Builder()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response!=null&&response.isSuccessful()){
final String json = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
onCallBack.onResponse(json);
}
});
}
}
});
}
}
ResponseBean.java
public class ResponseBean {
private String msg;
private String code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String sellerName;
private String sellerid;
private List<ListBean> list;
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
public List<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class ListBean {
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private double price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}