带子列表的listview:FloatingGroupExpandableListView

本文介绍如何在Android中使用FloatingGroupExpandableListView库创建带有子列表的效果。通过引入特定依赖库,开发者可以实现可浮起的分组展开列表视图,为应用增添交互性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看效果,如下图


首先加入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);     
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;
            }
        });


不是完整代码,只是大概流程





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值