自定义的ExpandableListView,可以使它外层有Scrollview的时候,可以显示完整的数据。
public class ExListView extends ExpandableListView { public ExListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public ExListView(Context context) { super(context); } public ExListView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }父类的对象
public class SRepairParent { private String categoryCode; private String categoryName; private Boolean flag = false; public Boolean getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } public String getCategoryName() { return categoryName; } public void setCategoryName(String categoryName) { this.categoryName = categoryName; } public String getCategoryCode() { return categoryCode; } public void setCategoryCode(String categoryCode) { this.categoryCode = categoryCode; } @Override public String toString() { return "SRepairParent{" + "categoryCode='" + categoryCode + '\'' + ", categoryName='" + categoryName + '\'' + ", flag=" + flag + '}'; } }子类的对象实体
public class SRepairChild { private String productCode; private String productDes; private String productName; private String productPrice; private Boolean flag = false; public String getProductCode() { return productCode; } public void setProductCode(String productCode) { this.productCode = productCode; } public String getProductDes() { return productDes; } public void setProductDes(String productDes) { this.productDes = productDes; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductPrice() { return productPrice; } public void setProductPrice(String productPrice) { this.productPrice = productPrice; } public Boolean getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } @Override public String toString() { return "SRepairChild{" + "productCode='" + productCode + '\'' + ", productDes='" + productDes + '\'' + ", productName='" + productName + '\'' + ", productPrice='" + productPrice + '\'' + ", flag=" + flag + '}'; } }具体的Activity
public class ReservationActivity extends ActionBarActivity implements View.OnClickListener { private ExListView reservation_elv; private List<Map<String, SRepairParent>> parents; private List<List<Map<String, SRepairChild>>> childs; static ExpAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reservation); initView(); initListener(); parents = new ArrayList<Map<String, SRepairParent>>(); childs = new ArrayList<List<Map<String, SRepairChild>>>(); downLoad("7413") ; initData(); } private void initView() { reservation_elv = (ExListView) findViewById(R.id.reservation_elv); } private void initListener() { } private void initData() { list= new ArrayList<>(); reservation_elv.setGroupIndicator(null); reservation_elv.setOnItemLongClickListener(new MyListener()); reservation_elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { childs.get(groupPosition).get(childPosition).get("child").setFlag(!childs.get(groupPosition).get(childPosition).get("child").getFlag()); for (int i = 0; i < childs.get(groupPosition).size(); i++) { Log.i("aaaaa", childs.get(groupPosition).size() + ""); if (childs.get(groupPosition).get(i).get("child").getFlag() == true) { parents.get(groupPosition).get("group").setFlag(true); break; } else if (childs.get(groupPosition).get(childs.get(groupPosition).size() - 1).get("child").getFlag() == false) { parents.get(groupPosition).get("group").setFlag(false); } } int m=0; list.clear(); for (int i = 0; i < parents.size(); i++) { for (int j = 0; j < childs.get(i).size(); j++) { if (childs.get(i).get(j).get("child").getFlag()) { m++; if (m==1){ serviceCode.append(childs.get(i).get(j).get("child").getProductCode()); }else{ serviceCode.append("+"+childs.get(i).get(j).get("child").getProductCode()); } OrderEntity orderEntity = new OrderEntity(); orderEntity.setCode(childs.get(i).get(j).get("child").getProductCode()); orderEntity.setQty("1"); list.add(orderEntity); price += Double.parseDouble(childs.get(i).get(j).get("child").getProductPrice()); } else { continue; } } } adapter.notifyDataSetChanged(); return false; } }); View view = LayoutInflater.from(this).inflate(R.layout.groups, null); ImageView imageView = (ImageView) view.findViewById(R.id.groups_icon); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double price = 0.0; for (int i = 0; i < parents.size(); i++) { for (int j = 0; j < childs.get(i).size(); j++) { if (childs.get(i).get(j).get("child").getFlag()) { price += Double.parseDouble(childs.get(i).get(j).get("child").getProductPrice()); } else { continue; } } } reservation_price.setText(price + ""); reservation_discount.setText(price+""); adapter.notifyDataSetChanged(); } }); reservation_elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { return false; } }); } public static Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 00: Bundle bundle = msg.getData(); reservation_price.setText(bundle.getDouble("price") + ""); reservation_discount.setText(bundle.getDouble("price") + ""); adapter.notifyDataSetChanged(); break; } } }; private void downLoad(String carTypeCode) { HttpUtil.httpGet(Constants.SELECT_REPAIR + "?carTypeCode=" + carTypeCode, new HttpUtil.HttpCallBack() { @Override public void callSuccessBack(String json) { SRepairBean bean = GsonTools.changeGsonToBean(json, SRepairBean.class); Log.i("12345", bean.toString()); Log.i("12345", "1111111111111111111"); Map<String, SRepairParent> map; for (int i = 0; i < bean.getProductCates().size(); i++) { map = new HashMap<String, SRepairParent>(); SRepairParent parent = bean.getProductCates().get(i).getProductCat(); map.put("group", parent); parents.add(map); } Log.i("12345", parents.toString()); List<Map<String, SRepairChild>> child_2; Map<String, SRepairChild> map1; for (int i = 0; i < bean.getProductCates().size(); i++) { child_2 = new ArrayList<Map<String, SRepairChild>>(); for (int j = 0; j < bean.getProductCates().get(i).getProducts().size(); j++) { map1 = new HashMap<String, SRepairChild>(); map1.put("child", bean.getProductCates().get(i).getProducts().get(j)); child_2.add(map1); } childs.add(child_2); } adapter = new ExpAdapter(ReservationActivity.this, parents, childs); reservation_elv.setAdapter(adapter); MeasureHeight.getListViewHeight(reservation_elv); } @Override public void callFailureBack(String json) { } }); } private class MyListener implements AdapterView.OnItemLongClickListener { @Override public boolean onItemLongClick(AdapterView<?> arg0, View v, int arg2, long arg3) { int groupId = (Integer) v.getTag(R.id.reservation_price); int childId = (Integer) v.getTag(R.id.reservation_discount); Log.i("12333", arg2 + ""); if (childId == -1) { View view = LayoutInflater.from(ReservationActivity.this).inflate(R.layout.reservation_dialog_layout, null); popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); popupWindow.showAsDropDown(v); } return false; } } /** * 获取网络图片 显示到ImageView上 */ private void RequestSetImageView(ImageView view, String url) { //加载品牌的logo BitmapDisplayConfig config = new BitmapDisplayConfig(); GHApplaction.bitmapUtils.display(view, url, config); }适配器
public class ExpAdapter extends BaseExpandableListAdapter { private Context context; // private Resources resources; private String[] group; private String[][] child; private LayoutInflater mInflater; List<Map<String,SRepairParent>> parents; List<List<Map<String,SRepairChild>>> childs; public ExpAdapter(Context ctx, List<Map<String,SRepairParent>> parents, List<List<Map<String,SRepairChild>>> childs) { context = ctx; this.parents = parents; this.childs = childs; mInflater = LayoutInflater.from(ctx); // resources = ctx.getResources(); } @Override public Object getChild(int groupPosition, int childPosition) { return childs.get(groupPosition).get(childPosition).get("child"); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = mInflater.inflate(R.layout.child,null); TextView childTv = (TextView) view.findViewById(R.id.child_text); TextView childPv = (TextView) view.findViewById(R.id.child_money); final ImageView imageView1 = (ImageView) view.findViewById(R.id.child_icon); childTv.setText(childs.get(groupPosition).get(childPosition).get("child").getProductName()); childPv.setText(childs.get(groupPosition).get(childPosition).get("child").getProductPrice()); view.setTag(R.id.reservation_price, groupPosition); view.setTag(R.id.reservation_discount, childPosition); // view.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // childs.get(groupPosition).get(childPosition).get("child").setFlag(!childs.get(groupPosition).get(childPosition).get("child").isFlag()); // imageView1.setSelected(childs.get(groupPosition).get(childPosition).get("child").isFlag()); // parents.get(groupPosition).get("group").setFlag(true); // } // }); if (childs.get(groupPosition).get(childPosition).get("child").getFlag()){ imageView1.setSelected(true); }else{ imageView1.setSelected(false); } notifyDataSetChanged(); // imageView1.setSelected(childs.get(groupPosition).get(childPosition).get("child").isFlag()); return view; } @Override public int getChildrenCount(int groupPosition) { return childs.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return parents.get(groupPosition).get("group"); } @Override public int getGroupCount() { return parents.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = mInflater.inflate(R.layout.groups,null); TextView groupTv = (TextView) view.findViewById(R.id.groups_text); final ImageView imageView = (ImageView) view.findViewById(R.id.groups_icon); groupTv.setText(parents.get(groupPosition).get("group").getCategoryName()); view.setTag(R.id.reservation_price, groupPosition); view.setTag(R.id.reservation_discount, -1); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { parents.get(groupPosition).get("group").setFlag(!parents.get(groupPosition).get("group").getFlag()); // imageView.setSelected(parents.get(groupPosition).get("group").isFlag()); if (parents.get(groupPosition).get("group").getFlag()){ for (int i =0;i<childs.get(groupPosition).size();i++){ childs.get(groupPosition).get(i).get("child").setFlag(true); } }else{ for (int i =0;i<childs.get(groupPosition).size();i++){ childs.get(groupPosition).get(i).get("child").setFlag(false); } } double price=0; for (int i = 0; i < parents.size(); i++) { for (int j = 0; j < childs.get(i).size(); j++) { if (childs.get(i).get(j).get("child").getFlag()){ price += Double.parseDouble(childs.get(i).get(j).get("child").getProductPrice()); }else{ continue; } } } Message message = new Message(); message.what = 00; Bundle bundle = new Bundle(); bundle.putDouble("price",price); message.setData(bundle); ReservationActivity.handler.sendMessage(message); notifyDataSetChanged(); } }); if (parents.get(groupPosition).get("group").getFlag()){ imageView.setSelected(true); }else{ imageView.setSelected(false); } notifyDataSetChanged(); return view; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }