带子列表的listview:FloatingGroupExpandableListView
发布日期:2021-05-28 16:40:37 浏览次数:9 分类:技术文章

本文共 4688 字,大约阅读时间需要 15 分钟。

先看效果,如下图

首先加入diegocarloslima依赖库

 
布局中创建FloatingGroupExpandableListView
创建适配器
 
class ProductAdapter extends BaseExpandableListAdapter {        ArrayList
brands; HashMap
> products; int[] color = SDApp.color; Context context; public ProductAdapter(Context context, ArrayList
brands, HashMap
> products) { this.context = context; this.brands = brands; this.products = products; } public void setData(ArrayList
brands, HashMap
> 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);     WrapperExpandableListAdapter wrapperAdapter = new WrapperExpandableListAdapter(productAdapter);mListView.setAdapter(wrapperAdapter); 可以设置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;            }        }); 不是完整代码,只是大概流程

转载地址:https://blog.csdn.net/qq_32413159/article/details/72377433 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:android录音MP3格式文件
下一篇:推荐两款实用的json数据请求及处理插件

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年02月14日 10时34分33秒