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

原文:https://my.oschina.net/yaowen424/blog/533092
摘要: 在日常开发,有可能会遇到需要一些可以展开的列表,学过了jQuery都知道,jQuery很方便的就做出来了。但是,用Android的该怎么做呢?其实在我没有接触过ExpandableListView类之前,都是自定义控件继承于ListView的。然后,后来发现了Android也给我们提供了ExpandableListView类,极大的方便了我们开发;

    摘要提到了,我之前自定义的控件继承于ListView。顾名思义,ExpandableListView是ListView的子类。它在普通的ListView的基础上进行了拓展,它把应用中的列表分为几组,每组又包含多个列表项。

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(361500);
        ll.addView(logo);
        TextView textView = getTextView();
        textView.setText(getGroup(groupPosition).toString());
        textView.setPadding(10000);
        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(36000);
        textView.setTextSize(20);
        return textView;
    }
}

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

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

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

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

  5、 就到这儿了结束吧,     谢谢观看。

发布了153 篇原创文章 · 获赞 29 · 访问量 40万+

猜你喜欢

转载自blog.csdn.net/wjw_java/article/details/75103455