先看效果,如下图
首先加入diegocarloslima依赖库
布局中创建FloatingGroupExpandableListView
创建适配器<com.diegocarloslima.fgelv.lib.FloatingGroupExpandableListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:groupIndicator="@null" />
class ProductAdapter extends BaseExpandableListAdapter { ArrayList<Brand> brands; HashMap<Brand, ArrayList<Product>> products; int[] color = SDApp.color; Context context; public ProductAdapter(Context context, ArrayList<Brand> brands, HashMap<Brand, ArrayList<Product>> products) { this.context = context; this.brands = brands; this.products = products; } public void setData(ArrayList<Brand> brands, HashMap<Brand, ArrayList<Product>> products) { this.brands = brands; this.products = products; notifyDataSetChanged(); } @Override public int getGroupCount() { return brands == null ? 0 : brands.size(); } @Override public int getChildrenCount(int groupPosition) { return products.get(brands.get(groupPosition)) == null ? 0 : products .get(brands.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return brands == null ? null : brands.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return products.get(brands.get(groupPosition)) == null ? 0 : products .get(brands.get(groupPosition)); } @Override public long getGroupId(int groupPosition) { return 0; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupHolder viewHolder; Brand brand = brands.get(groupPosition); if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_product_group, null); viewHolder = new GroupHolder(convertView); convertView.setTag(viewHolder); } else { viewHolder = (GroupHolder) convertView.getTag(); } viewHolder.mBrandName.setText(brand.getName()); final int resId = isExpanded ? R.drawable.list_open : R.drawable.list_close; viewHolder.mExpandedImage.setImageResource(resId); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) { final ChildHolder viewHolder; Brand brand = brands.get(groupPosition); final Product product = products.get(brand).get(childPosition); if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_product_child, null); viewHolder = new ChildHolder(convertView); convertView.setTag(viewHolder); } else { viewHolder = (ChildHolder) convertView.getTag(); } viewHolder.child.setBackgroundColor(color[childPosition % 2]); viewHolder.name.setText(product.getName()); viewHolder.spec.setText(product.getSpec()); if (product.getNum() == null || product.getNum() == 0.00) { viewHolder.num.setText("0"); } else { viewHolder.num.setText(product.getNum() + ""); } return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupHolder { @InjectView(R.id.brandName) TextView mBrandName; @InjectView(R.id.expandedImage) ImageView mExpandedImage; GroupHolder(View view) { ButterKnife.inject(this, view); } } class ChildHolder { @InjectView(R.id.name) TextView name; @InjectView(R.id.spec) TextView spec; @InjectView(R.id.price) TextView price; @InjectView(R.id.lastNum) TextView lastNum; @InjectView(R.id.cutsBtn) ImageButton cutsBtn; @InjectView(R.id.num) TextView num; @InjectView(R.id.addBtn) ImageButton addBtn; @InjectView(R.id.child) LinearLayout child; @InjectView(R.id.btn) RelativeLayout mRela; ChildHolder(View view) { ButterKnife.inject(this, view); } } }
然后给listview设置适配器
ProductAdapter productAdapter = new ProductAdapter(context, brands, products);
mListView.setAdapter(wrapperAdapter);WrapperExpandableListAdapter wrapperAdapter = new WrapperExpandableListAdapter(productAdapter);
可以设置listview的展开和收起的操作
mListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { //展开操作
} });
mListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { //收起操作 } });
mList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { //子view点击事件 return false; } });
不是完整代码,只是大概流程