• ExpandableListView(可展开的列表组件)的说明以及其用法


    ExpandableListView的用法和ListView非常像,只是其所显示的列表项应该由ExpandableListAdapter提供,下面是它的xml属性及说明:

    然而,接下来是用事实说话了:

    --------------------------------------------------------------------------------

    1、该项目的布局文件非常简单,和ListView差不多,此处就不贴出代码了。

    2、ExpandableListViewActivity.java关键代码如下:

    (要定义一个全局的私有的listView变量)

    listView = (ExpandableListView) findViewById(R.id.list);
    MyExpandableListAdapter adapter = new MyExpandableListAdapter();
    listView.setAdapter(adapter);

    3、MyExpandableListAdapter代码如下:

    public class MyExpandableListAdapter implements ExpandableListAdapter {
        int[] logos = new int[]{
                R.drawable.word,
                R.drawable.excel,
                R.drawable.email,
                R.drawable.ppt
        };
        private String[] armTypes = new String[]{
                "WORD", "EXCEL", "EMAIL", "PPT"
        };
        private String[][] arms = new String[][]{
                {"文档编辑", "文档排版", "文档处理", "文档打印"},
                {"表格编辑", "表格排版", "表格处理", "表格打印"},
                {"收发邮件", "管理邮箱", "登录登出", "注册绑定"},
                {"演示编辑", "演示排版", "演示处理", "演示打印"},
        };
    
        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
    
        }
    
        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
    
        }
    
        @Override
        public int getGroupCount() {
            return armTypes.length;
        }
    
        @Override
        public int getChildrenCount(int groupPosition) {
            return arms[groupPosition].length;
        }
    
        @Override
        public Object getGroup(int groupPosition) {
            return armTypes[groupPosition];
        }
    
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return arms[groupPosition][childPosition];
        }
    
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
    
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
    
        @Override
        public boolean hasStableIds() {
            return true;
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            LinearLayout ll = new LinearLayout(ExpandableListViewActivity.this);
            ll.setOrientation(LinearLayout.HORIZONTAL);
            ImageView logo = new ImageView(ExpandableListViewActivity.this);
            logo.setImageResource(logos[groupPosition]);
            logo.setPadding(36, 15, 0, 0);
            ll.addView(logo);
            TextView textView = getTextView();
            textView.setText(getGroup(groupPosition).toString());
            textView.setPadding(10, 0, 0, 0);
            ll.addView(textView);
            return ll;
        }
    
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            TextView textView = getTextView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    
        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }
    
        @Override
        public boolean isEmpty() {
            return false;
        }
    
        @Override
        public void onGroupExpanded(int groupPosition) {
    
        }
    
        @Override
        public void onGroupCollapsed(int groupPosition) {
    
        }
    
        @Override
        public long getCombinedChildId(long groupId, long childId) {
            return 0;
        }
    
        @Override
        public long getCombinedGroupId(long groupId) {
            return 0;
        }
    
        private TextView getTextView() {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
            TextView textView = new TextView(ExpandableListViewActivity.this);
            textView.setLayoutParams(lp);
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            textView.setPadding(36, 0, 0, 0);
            textView.setTextSize(20);
            return textView;
        }
    }

    代码上面没有做多少注释,在这里说明一下吧:

    该adapter有两个关键方法:getChildView()和getGroupView();

            ----------前者返回的view对象作为子列表项,后者返回的view作为组列表项。

    4、下面是该程序的运行界面了:

  • 相关阅读:
    sql over(partition by) 开窗函数的使用
    利用curl函数处理GET数据获取微信公众号的access_token
    2018.4.12
    字段和属性
    C#实现回车键登录
    判断DataTable里面数据是否有重复数据
    一个强大的人民币大写转换的正则表达式
    C#将image中的显示的图片转换成二进制
    遍历Dev LayoutControl中的所有控件信息
    遍历窗体中所有控件的信息
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5603953.html
Copyright © 2020-2023  润新知