ExpandableListView 和SwipeLayout 实现双层列表 ,childitem 滑动删除效果

本文介绍如何使用Android中的ExpandableListView实现双层列表效果,并详细解释了自定义Adapter的过程,包括如何处理数据结构、展示组视图及子项视图等。

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

ExpandableListView  可以实现双层列表效果。而且代码量不多,比较简单实用。

    private Map<String, List<String>> dataset = new HashMap<String, List<String>>();
    private ArrayList<String> parentList;
    private ArrayList<String> childList;

    public void initDatas() {

        String mac;
        parentList = new ArrayList<String>();

        parentList.add("343");
        parentList.add("979");
        initDataSet();


    }

    public void initDataSet() {
        dataset.clear();
        for (String info : parentList) {
            childList = new ArrayList<String>();
            childList.add("17771861240");
            childList.add("17771861241");
            dataset.put(info, childList);
        }
    }


初始化数据,每个parent对应一个childList,所以数据用HashMap  保存。主要就是Adapter 中的代码实现。

 private SwipeLayout currentExpandedSwipeLayout;
    BaseExpandableListAdapter expandAdapter = new BaseExpandableListAdapter() {
        @Override
        public int getGroupCount() {
            return dataset.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            if (dataset.get(parentList.get(groupPosition)) == null) {
                return 0;
            }
            return dataset.get(parentList.get(groupPosition)).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return parentList.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return dataset.get(parentList.get(groupPosition)).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;
        }

	//实现GroupView ,isExpanded GroupView 是否展开,因为在ExpandableListView 中展开的图标是默认在左边,所以我们要去掉默认的,然后自己定义
一个,根据IsExpanded 来设置图标。setGroupIndicator(null) ,去掉默认的。
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            if (convertView == null) {

                convertView = LayoutInflater.from(AccountManagerActivity.this).inflate(R.layout.group_layout, null);
            }
            convertView.setTag(R.layout.group_layout, groupPosition);
            convertView.setTag(R.layout.expandlist_item, -1);
            TextView text = (TextView) convertView.findViewById(R.id.group_title);
            text.setText(parentList.get(groupPosition));
            ImageView img = (ImageView) convertView.findViewById(R.id.group_img);
            if (isExpanded) {
                img.setBackgroundResource(R.drawable.icon_detail_down_button);
            } else {
                img.setBackgroundResource(R.drawable.icon_detail_pull_button);
            }
            return convertView;
        }


        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            if (convertView == null) {

                convertView = LayoutInflater.from(AccountManagerActivity.this).inflate(R.layout.expandlist_item, null);

            }
            convertView.setTag(R.layout.group_layout, groupPosition);
            convertView.setTag(R.layout.expandlist_item, childPosition);
            TextView tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            Button btn_delete = (Button) convertView.findViewById(R.id.ib_delete);
            final SwipeLayout child = (SwipeLayout) convertView.findViewById(R.id.child);
            tv_num.setText(getText(R.string.normal_user).toString() + (childPosition + 1));
            final String phone = dataset.get(parentList.get(groupPosition)).get(childPosition);
            tv_name.setText(phone);
            //设置SwipeLayout  的模式。         
            child.setShowMode(SwipeLayout.ShowMode.PullOut);
	    //设置需要滑动出现的布局,要在XML 中给该布局加上TAG
            child.addDrag(SwipeLayout.DragEdge.Right, child.findViewWithTag("button"));
            child.addSwipeListener(new SwipeLayout.SwipeListener() {
                @Override
                public void onStartOpen(SwipeLayout layout) {
                    if (currentExpandedSwipeLayout != null && currentExpandedSwipeLayout != layout)
                        currentExpandedSwipeLayout.close(true);

                }

                @Override
                public void onOpen(SwipeLayout layout) {
                    currentExpandedSwipeLayout = layout;


                }

                @Override
                public void onStartClose(SwipeLayout layout) {

                }

                @Override
                public void onClose(SwipeLayout layout) {
                }

                @Override
                public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

                }

                @Override
                public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

                }
            });
}

这样就实现了双层列表,而且子类Item 可以滑动删除的效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值