Android二级列表

android中二级列表的显示与ListView、RecyelerView等步骤类似:

(1)xml布局

<ExpandableListView
        android:id="@+id/expandablelistview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />


(2)代码:

private ExpandableListView expandablelistview;// 可扩展的二级列表
//创建对象
expandablelistview = (ExpandableListView) findViewById(R.id.expandablelistview);
//实现二级列表的Group点击效果
expandablelistview.setOnGroupClickListener(new OnGroupClickListener() {
 
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                if (!parent.isGroupExpanded(groupPosition)) {
                    departmentId = (String) tree.get(groupArray
                            .get(groupPosition));
                getemployee(groupPosition);// 获取部门下的员工信息
                    if (pos != groupPosition) {
                        parent.collapseGroup(pos);
                    }
                    pos = groupPosition;
                } else {
                    parent.collapseGroup(groupPosition);
                }
                return true;
            }
        });   


(3)适配器:

public class ExpandableAdapter extends BaseExpandableListAdapter {
 
        public ExpandableAdapter() {
            hand = new Handler() {
 
                @Override
                public void handleMessage(Message msg) {
                    notifyDataSetChanged();
                    super.handleMessage(msg);
                }
            };
        }
 
        public Object getChild(int groupPosition, int childPosition) {
            if (childArray.size() == 0) {
                return null;
            }
            return childArray.get(groupPosition).get(childPosition);
        }
 
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
 
        public int getChildrenCount(int groupPosition) {
            // if (childArray.size() == 0) {
            // return 0;
            // }
            // if (childArray.size() == 1) {
            // return 1;
            // }
            return childArray.size();
        }
 
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            if (childArray.size() == 0) {
                return getGenericView("无员工信息");
            }
            if (childArray.size() == 1) {
                return getGenericViewchild(childArray.get(0).get(0));
            }
            String string = childArray.get(groupPosition).get(childPosition);
            return getGenericViewchild(string);
        }
 
        // group method stub
        public Object getGroup(int groupPosition) {
            return groupArray.get(groupPosition);
        }
 
        public int getGroupCount() {
            return groupArray.size();
        }
 
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
 
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            String string = groupArray.get(groupPosition);
            return getGenericView(string);
        }
 
        // View stub to create Group/Children 's View
        public TextView getGenericView(String string) {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.FILL_PARENT);
            TextView text = new TextView(Manager_Employee_List.this);
            text.setLayoutParams(layoutParams);
            // Center the text vertically
            text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            text.setPadding(100, 0, 0, 0);
            text.setTextSize(25);
            text.setBackgroundResource(R.drawable.back_top);
            text.setText(string);
            return text;
        }
 
        public View getGenericViewchild(final String string) {
            View v = View.inflate(Manager_Employee_List.this,
                    R.layout.activity_baseadapter_list, null);
            TextView text = (TextView) v.findViewById(R.id.tv_baseadapter_list);
            ImageButton alter = (ImageButton) v
                    .findViewById(R.id.ib_baseadapter_alter);
            // 修改
            alter.setOnClickListener(new OnClickListener() {
 
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "修改", 0).show();
                    Intent intent_Alter = new Intent(
                            Manager_Employee_List.this, Manager_Add.class);
                    intent_Alter.putExtra("companyId", companyId);
                    PeopleNameAlter peopleNameAlter = (PeopleNameAlter) treeMap
                            .get(string);
                    intent_Alter.putExtra("biaoshi", true);
                    intent_Alter.putExtra("banc", peopleNameAlter.banc);
                    intent_Alter.putExtra("dapart", peopleNameAlter.dapart);
                    intent_Alter.putExtra("id", peopleNameAlter.id);
                    intent_Alter.putExtra("loginname",
                            peopleNameAlter.loginname);
                    intent_Alter.putExtra("phone", peopleNameAlter.phone);
                    intent_Alter.putExtra("pwd", peopleNameAlter.pwd);
                    intent_Alter.putExtra("name", string);
                    startActivity(intent_Alter);
                }
            });
            ImageButton delete = (ImageButton) v
                    .findViewById(R.id.ib_baseadapter_delete);
            // 删除
            delete.setOnClickListener(new OnClickListener() {
 
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "删除", 0).show();
                    PeopleNameAlter peopleNameAlterDelete = (PeopleNameAlter) treeMap
                            .get(string);
                    deletePeople(peopleNameAlterDelete.id);
                    namelingshi = string;
                }
            });
            text.setText(string);
            return v;
        }
 
        public boolean hasStableIds() {
            return false;
        }
 
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
 
    }


(4)让ExpandableListView使用适配器

expandablelistview.setAdapter(new ExpandableAdapter());


具体的显示效果,可以再适配器中进行设置。

本文出自 “移动平台开发” 博客,请务必保留此出处http://liuxudong1001.blog.51cto.com/10877072/1753742

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值