//
首先购物车是一个二级列表,因为,二级列表的一级列表代表商店,二级列表代表要购买的东西。
所以你要用到的知识点就是:
ExpandableListView
但是这个控件有一个缺点,不能进行滚动。所以要想解决这个缺点,需要和scrollview结合起来。但和scrollview结合起来使用的时候
,expandablelistview的高度只会显示一个。需要系定义控件:如下
public class CustomExpandableListView extends ExpandableListView { public CustomExpandableListView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }布局使用情况如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <ScrollView android:layout_width="match_parent" android:layout_marginBottom="50dp" android:layout_height="match_parent"> <lianxi.bawei.com.gouwuche.view.CustomExpandableListView android:id="@+id/elist" android:layout_width="match_parent" android:layout_height="match_parent"> </lianxi.bawei.com.gouwuche.view.CustomExpandableListView> </ScrollView> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:gravity="center_vertical" android:orientation="horizontal" > <CheckBox android:id="@+id/checkbox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="6dp" android:button="@drawable/select_checkbox" android:focusable="false"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="6dp" android:text="全选" android:textSize="20sp"/> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="6dp" android:text="0台"/> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="6dp" android:text="0元"/> </LinearLayout> </RelativeLayout>//然后在manactivity里进行逻辑:
package lianxi.bawei.com.gouwuche.activity; import android.database.DataSetObserver; import android.support.v4.view.PagerAdapter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import com.google.gson.Gson; import com.squareup.okhttp.Call; import com.squareup.okhttp.Callback; import com.squareup.okhttp.FormEncodingBuilder; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import java.io.IOException; import java.util.List; import lianxi.bawei.com.gouwuche.Bean.PhonesInfo; import lianxi.bawei.com.gouwuche.R; import lianxi.bawei.com.gouwuche.view.CustomExpandableListView; import static lianxi.bawei.com.gouwuche.R.id.elist; public class MainActivity extends AppCompatActivity { private CheckBox checkBox; private TextView tv_num; private TextView tv_price; private List<PhonesInfo.DataInfo> data; private PhonesInfo phonesInfo; private CustomExpandableListView elist; private PhoneAdapter p1; int price=0; int num=0; int price1; int num1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); postdata(); } private void postdata() { OkHttpClient okHttpClient = new OkHttpClient(); String url = "http://api.ehuigou.com/Orders/searchCartsLog"; FormEncodingBuilder builder = new FormEncodingBuilder(); builder.add("store_id", "3850"); Request request = new Request.Builder().url(url).post(builder.build()).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { final String string = response.body().string(); Log.i("xxx", string.toString()); runOnUiThread(new Runnable() { @Override public void run() { Gson g1=new Gson(); phonesInfo= g1.fromJson(string, PhonesInfo.class); Log.i("kkkkkk", phonesInfo.getData().size()+""); // data = phonesInfo.getData(); //Log.i("aaa",data.toString()); p1 = new PhoneAdapter(); elist.setAdapter(p1); int count = elist.getCount(); for (int i = 0; i < count; i++) { elist.expandGroup(i); } checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean falg = ((CheckBox) v).isChecked(); checkBox.setChecked(falg); //判断boolean类型,如果为true,则都设置为true if(falg==true) { //每次让它为初始值,这样价格不会乱序 num=0; price=0; List<PhonesInfo.DataInfo> data = phonesInfo.data; for (int i=0;i<data.size();i++) { data.get(i).setAllCheck(falg); List<PhonesInfo.DataInfo.DatasInfo> datas = data.get(i).getDatas(); num=num+datas.size(); num1=num; for (int j=0;j<datas.size();j++) { price=price+datas.get(j).getPrice(); price1=price; datas.get(j).setItemCheck(falg); } //这是台数,和价格 tv_num.setText(num+"台"); tv_price.setText(price+"元"); } } //这是false的逻辑 else{ List<PhonesInfo.DataInfo> data = phonesInfo.data; for (int i=0;i<data.size();i++) { data.get(i).setAllCheck(false); List<PhonesInfo.DataInfo.DatasInfo> datas = data.get(i).getDatas(); for (int j=0;j<datas.size();j++) { datas.get(j).setItemCheck(false); } } num=0; price=0; tv_num.setText(num+"台"); tv_price.setText(price+"元"); } refresh(); } }); /* checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked==true) { tv_num.setText(num1+"台"); tv_price.setText(price1+"元"); } } });*/ } }); } }); } private void initview() { elist = (CustomExpandableListView) findViewById(R.id.elist); checkBox = (CheckBox) findViewById(R.id.checkbox2); tv_num = (TextView) findViewById(R.id.tv_num); tv_price = (TextView) findViewById(R.id.tv_price); } class PhoneAdapter implements ExpandableListAdapter { @Override public void registerDataSetObserver(DataSetObserver observer) { } @Override public void unregisterDataSetObserver(DataSetObserver observer) { } @Override public int getGroupCount() { return phonesInfo.getData().size(); } @Override public int getChildrenCount(int groupPosition) { return phonesInfo.getData().get(groupPosition).getDatas().size(); } @Override public Object getGroup(int groupPosition) { return phonesInfo.getData().get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return phonesInfo.getData().get(groupPosition).getDatas().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 false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.item_parent_market, null); CheckBox cb_parent = (CheckBox) view.findViewById(R.id.cb_parent); TextView tv_number = (TextView) view.findViewById(R.id.tv_number); tv_number.setText(phonesInfo.getData().get(groupPosition).getTitle()); cb_parent.setChecked(phonesInfo.getData().get(groupPosition).isAllCheck()); cb_parent.setOnClickListener(new View.OnClickListener() { private boolean h=true; @Override public void onClick(View v) { //一级列表的状态 boolean falg = ((CheckBox) v).isChecked(); //给一级列表赋值 phonesInfo.getData().get(groupPosition).setAllCheck(falg); //判断状态 if (falg == true) { Log.i("jjjj", falg + ""); //给二级列表的所有都赋值 List<PhonesInfo.DataInfo.DatasInfo> datas = phonesInfo.data.get(groupPosition).getDatas(); //让二级列表的所有都为truefor (int i=0;i<datas.size();i++) { if(datas.get(i).isItemCheck()==true) { price=price-datas.get(i).getPrice(); num=num-1; } }
num=num+datas.size(); for (int i = 0; i < datas.size(); i++) { datas.get(i).setItemCheck(true); price=price+datas.get(i).getPrice(); refresh(); } tv_num.setText(num+"台"); tv_price.setText(price+"元"); //判断一级级列表里面的东西是不是都是true for (int i=0;i<phonesInfo.data.size();i++) { if(phonesInfo.data.get(i).isAllCheck()==false) { h=false; } } //如果是true,就赋值给checkbox if(h==true) { checkBox.setChecked(true); } } //判断一级列表的状态,有一个是false,总的就是false else if (falg == false) { checkBox.setChecked(falg); //下面的方法:是,一级列表的反选 List<PhonesInfo.DataInfo.DatasInfo> datas = phonesInfo.data.get(groupPosition).getDatas(); num=num-datas.size(); for (int i = 0; i < datas.size(); i++) { datas.get(i).setItemCheck(false); price=price-datas.get(i).getPrice(); } //当那个为fals时,减去那个价钱 tv_price.setText(price+"元"); tv_num.setText(num+"台"); } refresh(); } }); return view; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.item_child_market, null); TextView tv_content = (TextView) view.findViewById(R.id.tv_content); TextView tv_time = (TextView) view.findViewById(R.id.tv_time); TextView tv_pri = (TextView) view.findViewById(R.id.tv_pri); CheckBox cb_child = (CheckBox) view.findViewById(R.id.cb_child); tv_pri.setText(phonesInfo.getData().get(groupPosition).getDatas().get(childPosition).getPrice() + "元"); tv_content.setText(phonesInfo.getData().get(groupPosition).datas.get(childPosition).getType_name()); cb_child.setChecked(phonesInfo.getData().get(groupPosition).datas.get(childPosition).isItemCheck()); cb_child.setOnClickListener(new View.OnClickListener() { private Boolean j; @Override public void onClick(View v) { //二级列表的状态 boolean falg = ((CheckBox) v).isChecked(); //直接赋值 phonesInfo.getData().get(groupPosition).datas.get(childPosition).setItemCheck(falg); //得到当前二级列表的集合 List<PhonesInfo.DataInfo.DatasInfo> datas = phonesInfo.getData().get(groupPosition).getDatas(); Boolean f=true; Boolean s=true; //判断状态 if(falg==false) { num=num-1; price=price- phonesInfo.getData().get(groupPosition).datas.get(childPosition).getPrice(); //假如有一个是false,一级列表的就是false,全选也是false phonesInfo.getData().get(groupPosition).setAllCheck(false); checkBox.setChecked(falg); tv_num.setText(num+"台"); tv_price.setText(price+"元"); } //当二级都选中的时候,分两种,一种是:全都选中,一种是一级列表中的二级列表全选中 else if(falg==true){ num=num+1; price=price+phonesInfo.getData().get(groupPosition).datas.get(childPosition).getPrice(); tv_num.setText(num+"台"); tv_price.setText(price+"元"); //第一个判断的就是,一级列表中的二级列表是否全选中 for(int i=0;i<datas.size();i++) { if(datas.get(i).isItemCheck()==false) { f=false; } } if(f==true) { phonesInfo.getData().get(groupPosition).setAllCheck(true); } //这个是:全都选中, for (int i=0;i<phonesInfo.data.size();i++) { List<PhonesInfo.DataInfo.DatasInfo> datas1 = phonesInfo.data.get(i).getDatas(); if(phonesInfo.data.get(i).isAllCheck()==false) { s=false; } } if(s==true) { checkBox.setChecked(true); } } refresh(); } }); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEmpty() { return false; } @Override public void onGroupExpanded(int groupPosition) { } @Override public void onGroupCollapsed(int groupPosition) { } @Override public long getCombinedChildId(long groupId, long childId) { return 0; } @Override public long getCombinedGroupId(long groupId) { return 0; } } public void refresh() { p1 = new PhoneAdapter(); elist.setAdapter(p1); int count = elist.getCount(); for (int i = 0; i < count; i++) { elist.expandGroup(i); } }}
对了,这个控件没有刷新,需要定义一个方法:来进行刷新
public void refresh() { p1 = new PhoneAdapter(); elist.setAdapter(p1); int count = elist.getCount(); for (int i = 0; i < count; i++) { elist.expandGroup(i); } }//下面的是bean包:
package lianxi.bawei.com.gouwuche.Bean; import java.util.List; /** * 1. 类的用途 * 2. @author forever * 3. @date 2017/4/9 17:10 */ public class PhonesInfo { public String flag; public String code; public List<DataInfo> data; public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataInfo> getData() { return data; } public void setData(List<DataInfo> data) { this.data = data; } public static class DataInfo { public String title; public List<DatasInfo> datas; private boolean allCheck = false; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<DatasInfo> getDatas() { return datas; } public void setDatas(List<DatasInfo> datas) { this.datas = datas; } public boolean isAllCheck() { return allCheck; } public void setAllCheck(boolean allCheck) { this.allCheck = allCheck; } public static class DatasInfo { private int price; private String type_name; private List<String> msg; private String add_time; private boolean itemCheck=false; public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getType_name() { return type_name; } public void setType_name(String type_name) { this.type_name = type_name; } public List<String> getMsg() { return msg; } public void setMsg(List<String> msg) { this.msg = msg; } public String getAdd_time() { return add_time; } public void setAdd_time(String add_time) { this.add_time = add_time; } public boolean isItemCheck() { return itemCheck; } public void setItemCheck(boolean itemCheck) { this.itemCheck = itemCheck; } } } @Override public String toString() { return "PhonesInfo{" + "flag='" + flag + '\'' + ", code='" + code + '\'' + ", data=" + data + '}'; } }
//下面是二级列表和一级列表的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_red_light" android:gravity="center_vertical" android:orientation="horizontal" android:descendantFocusability="blocksDescendants" > <CheckBox android:id="@+id/cb_child" android:layout_width="wrap_content" android:button="@drawable/select_checkbox" android:layout_marginTop="30dp" android:layout_marginLeft="30dp" android:layout_marginBottom="30dp" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_tel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="iphone6"/> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="什么手机"/> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="2016-12-10"/> </LinearLayout> <TextView android:id="@+id/tv_pri" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥3000.00" android:layout_marginLeft="30dp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center_vertical" android:orientation="horizontal" > <CheckBox android:id="@+id/cb_parent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:focusable="false" android:layout_marginBottom="30dp" android:button="@drawable/select_checkbox" android:layout_marginLeft="50dp"/> <TextView android:id="@+id/tv_sign" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="标记"/> <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="12345678"/> </LinearLayout>