1.主佈局
<?xml version="1.0" encoding="utf-8"?><ExpandableListView
android:id="@+id/expand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/qx"
android:text="全選"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="合計"
android:id="@+id/tvSum"
android:textSize="20sp"
android:paddingRight="50dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="結算"
android:id="@+id/jiesuan"
android:textSize="20sp"
android:background="#f21212"
/>
</LinearLayout>
2.shop1接口
public interface Shop1 {
void onSuccess(List<ShopBean.DataBean> list);
void onFailer(String error);
}
3.View層
public interface ShopView1 {
void onSuccess(List<ShopBean.DataBean> list);
void onFailer(String error);
}
4.Model層
public class ShopModel {
private String path=“http://120.27.23.105/product/getCarts?source=android&uid=99”;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
public void shop1(final Shop1 shop1){
OkHttp.createget(path, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
ShopBean shopBean = gson.fromJson(string, ShopBean.class);
List<ShopBean.DataBean> data = shopBean.getData();
if (data!=null){
shop1.onSuccess(data);
}else {
shop1.onFailer("失敗");
}
}
});
}
});
}
}
5.Presenter層
public class ShopPresenter {
private ShopView1 shopView1;
private ShopModel shopModel;
public ShopPresenter(ShopView1 shopView1) {
this.shopView1 = shopView1;
shopModel= new ShopModel();
}
public void shop1(){
shopModel.shop1(new Shop1() {
@Override
public void onSuccess(List<ShopBean.DataBean> list) {
shopView1.onSuccess(list);
}
@Override
public void onFailer(String error) {
shopView1.onFailer(error);
}
});
}
}
6.Bean層
public class ShopBean {
private String msg;
private String code;
private List 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;
private boolean isChecked;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
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 boolean isChecked() {
return selected!=0;
}
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;
}
@Override
public String toString() {
return "ListBean{" +
"bargainPrice=" + bargainPrice +
", createtime='" + createtime + '\'' +
", detailUrl='" + detailUrl + '\'' +
", images='" + images + '\'' +
", num=" + num +
", pid=" + pid +
", price=" + price +
", pscid=" + pscid +
", selected=" + selected +
", sellerid=" + sellerid +
", subhead='" + subhead + '\'' +
", title='" + title + '\'' +
'}';
}
}
@Override
public String toString() {
return "DataBean{" +
"sellerName='" + sellerName + '\'' +
", sellerid='" + sellerid + '\'' +
", list=" + list +
'}';
}
}
@Override
public String toString() {
return "ShopBean{" +
"msg='" + msg + '\'' +
", code='" + code + '\'' +
", data=" + data +
'}';
}
}
7.group2第二層佈局
<?xml version="1.0" encoding="utf-8"?><CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox2"
android:focusable="false"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name1"
android:textSize="20sp"
/>
8.第三層佈局
<?xml version="1.0" encoding="utf-8"?><CheckBox
android:id="@+id/checkboxz"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="37dp" />
<ImageView
android:id="@+id/imagez"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="52dp"
android:layout_marginLeft="52dp"
android:layout_marginTop="0dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textz1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imagez"
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_alignBottom="@+id/imagez"
android:layout_toRightOf="@+id/imagez"
android:text="sdfa" />
<bw.com.yk.AddSum
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sumz"
android:layout_alignBottom="@+id/textz2"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
>
</bw.com.yk.AddSum>
9.加減器
public class AddSum extends LinearLayout {
private TextView add;
private TextView jian;
private TextView number;
public AddSum(Context context) {
this(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 initView(Context context) {
View view = View.inflate(context,R.layout.layout_add,this);
add = view.findViewById(R.id.tAdd);
jian = view.findViewById(R.id.tSub);
number = view.findViewById(R.id.tNumber);
number.setText("1");
}
private void initListener() {
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
add();
}
});
jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
jian();
}
});
}
private void jian() {
String s = number.getText().toString();
int parseInt = Integer.parseInt(s);
if (parseInt>1){
parseInt--;
setCurrent(parseInt);
}else {
Toast.makeText(getContext().getApplicationContext(), "不能再少了", Toast.LENGTH_SHORT).show();
}
}
private void add() {
String s = number.getText().toString();
int parseInt = Integer.parseInt(s);
parseInt++;
setCurrent(parseInt);
}
public void setCurrent(int parseInt) {
number.setText(parseInt+"");
if (onNumChangeListener!=null){
onNumChangeListener.OnClick(this,parseInt);
}
}
public interface OnNumChangeListener{
void OnClick(View view,int curnum);
}
OnNumChangeListener onNumChangeListener;
public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener) {
this.onNumChangeListener = onNumChangeListener;
}
}
10.shape邊框
11.Frag04
public class Frag04 extends Fragment implements ShopView1 {
private ShopPresenter shopPresenter;
private View view;
private ExpandableListView expandableListView;
private CheckBox qx;
private TextView heji;
private ExpandAdapter2 expandAdapter2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_frag04, container, false);
initView();
initListener();
return view;
}
private void initListener() {
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
ShopBean.DataBean group = (ShopBean.DataBean) expandAdapter2.getGroup(groupPosition);
group.setChecked(!group.isChecked());
int c=0;
if (group.isChecked()){
c=1;
}
List<ShopBean.DataBean.ListBean> list = group.getList();
for (int i = 0; i < list.size(); i++) {
ShopBean.DataBean.ListBean listBean = list.get(i);
listBean.setSelected(c);
}
expandAdapter2.notifyDataSetChanged();
getTotal();
return true;
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
ShopBean.DataBean.ListBean child = (ShopBean.DataBean.ListBean) expandAdapter2.getChild(groupPosition, childPosition);
boolean checked = child.isChecked();
if (checked){
child.setSelected(0);
}else {
child.setSelected(1);
}
expandAdapter2.notifyDataSetChanged();
getTotal();
return true;
}
});
}
private void initView() {
qx = view.findViewById(R.id.qx);
heji = view.findViewById(R.id.tvSum);
shopPresenter=new ShopPresenter(this);
shopPresenter.shop1();
expandableListView = view.findViewById(R.id.expand);
qx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
setCheckAll(1);
}else {
setCheckAll(0);
}
}
});
}
public void setCheckAll(int s) {
int groupCount = expandAdapter2.getGroupCount();
for (int j = 0; j <groupCount ; j++) {
ShopBean.DataBean group = (ShopBean.DataBean) expandAdapter2.getGroup(j);
if (group.isChecked()==false){
group.setChecked(true);
}else {
group.setChecked(false);
}
List<ShopBean.DataBean.ListBean> list = group.getList();
for (int k = 0; k <list.size() ; k++) {
ShopBean.DataBean.ListBean listBean = list.get(k);
listBean.setSelected(s);
}
}
expandAdapter2.notifyDataSetChanged();
getTotal();
}
private void getTotal() {
double totla=0;
int groupCount = expandAdapter2.getGroupCount();
for (int i = 0; i <groupCount ; i++) {
ShopBean.DataBean group = (ShopBean.DataBean) expandAdapter2.getGroup(i);
List<ShopBean.DataBean.ListBean> list = group.getList();
for (int j = 0; j <list.size() ; j++) {
ShopBean.DataBean.ListBean listBean = list.get(j);
boolean checked = listBean.isChecked();
if (checked){
double price = list.get(j).getPrice();
totla+=price*listBean.getNum();
}
}
}
heji.setText("合計:"+totla);
}
@Override
public void onSuccess(List<ShopBean.DataBean> list) {
expandAdapter2 = new ExpandAdapter2(getActivity(),list);
expandableListView.setAdapter(expandAdapter2);
for (int i = 0; i < expandAdapter2.getGroupCount() ; i++) {
expandableListView.expandGroup(i);
}
expandAdapter2.setOnNumChangeListener(new AddSum.OnNumChangeListener() {
@Override
public void OnClick(View view, int curnum) {
getTotal();
}
});
}
@Override
public void onFailer(String error) {
}
}
12.ExpandAdapter2
public class ExpandAdapter2 extends BaseExpandableListAdapter {
private Context context;
private List<ShopBean.DataBean> list;
private AddSum.OnNumChangeListener onNumChangeListener;
public ExpandAdapter2(Context context, List<ShopBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).getList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
MyHolder myHolder=null;
if (convertView==null){
convertView=View.inflate(context, R.layout.group2,null);
myHolder = new MyHolder();
myHolder.checkBox=convertView.findViewById(R.id.checkbox2);
myHolder.textView=convertView.findViewById(R.id.name1);
convertView.setTag(myHolder);
}else {
myHolder= (MyHolder) convertView.getTag();
}
myHolder.textView.setText(list.get(groupPosition).getSellerName());
myHolder.checkBox.setChecked(list.get(groupPosition).isChecked());
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder = null;
if (convertView==null){
convertView=View.inflate(context,R.layout.zi,null);
childViewHolder = new ChildViewHolder();
childViewHolder.cbChecked=convertView.findViewById(R.id.checkboxz);
childViewHolder.addSum=convertView.findViewById(R.id.sumz);
childViewHolder.ivGoodsIcon=convertView.findViewById(R.id.imagez);
childViewHolder.tvGoodsName=convertView.findViewById(R.id.textz1);
childViewHolder.tvGoodsPrice=convertView.findViewById(R.id.textz2);
convertView.setTag(childViewHolder);
}else {
childViewHolder= (ChildViewHolder) convertView.getTag();
}
String images = list.get(groupPosition).getList().get(childPosition).getImages();
String[] split = images.split("\\|");
if (split.length>0){
Glide.with(context).load(split[childPosition]).into(childViewHolder.ivGoodsIcon);
}
childViewHolder.tvGoodsName.setText(list.get(groupPosition).getList().get(childPosition).getTitle());
childViewHolder.tvGoodsPrice.setText(list.get(groupPosition).getList().get(childPosition).getPrice()+"");
childViewHolder.cbChecked.setChecked(list.get(groupPosition).getList().get(childPosition).isChecked());
childViewHolder.addSum.setCurrent(list.get(groupPosition).getList().get(childPosition).getNum());
childViewHolder.addSum.setOnNumChangeListener(new AddSum.OnNumChangeListener() {
@Override
public void OnClick(View view, int curnum) {
list.get(groupPosition).getList().get(childPosition).setNum(curnum);
if (onNumChangeListener!=null){
onNumChangeListener.OnClick(view,curnum);
}
}
});
return convertView;
}
public void setOnNumChangeListener(AddSum.OnNumChangeListener onNumChangeListener){
this.onNumChangeListener=onNumChangeListener;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class MyHolder{
CheckBox checkBox;
TextView textView;
}
class ChildViewHolder {
CheckBox cbChecked;
ImageView ivGoodsIcon;
TextView tvGoodsName;
TextView tvGoodsPrice;
AddSum addSum;
}
}