欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android使用ExpandableListView控件实现小说目录效果的例子

程序员文章站 2023-02-02 12:27:53
今天给大家讲讲android的目录实现方法,就像大家看到的小说目录一样,android 提供了expandablelistview控件可以实现二级列表展示效果,现在给大家讲...

今天给大家讲讲android的目录实现方法,就像大家看到的小说目录一样,android 提供了expandablelistview控件可以实现二级列表展示效果,现在给大家讲讲这个控件的用法,下面是xml定义:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff"
  >
  <expandablelistview
       android:id="@+id/elv_journal_catalog"
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       android:cachecolorhint="#ffffff"
       />
</linearlayout>

这代码很简单,和写listview的方法差不多,接下来是expandablelistview在activity中的代码:
复制代码 代码如下:

 private expandablelistview elv_journal_catalog;
    private list<list<article>> childrenobj;
         private journalcataloglistadapter adapter;
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.journal_catalog);
        init();
        elv_journal_catalog.setgroupindicator(null);
        elv_journal_catalog.setdivider(null);

        loaddata();
    }

    private void init() {
        elv_journal_catalog = (expandablelistview) findviewbyid(r.id.elv_journal_catalog);
        elv_journal_catalog.setonchildclicklistener(listener);
    }
    private void loaddata() {
        message msg = handler.obtainmessage();
        msg.what = 1;
        msg.sendtotarget();

        childrenobj = new arraylist<list<article>>();
        new thread() {

            @override
            public void run() {
                if (!isloading) {
                    queryarticlelist();
                } else {
                    queryarticlelistfromsqlite();
                }
            }

        }.start();

        adapter = new journalcataloglistadapter(this, childrenobj);
        elv_journal_catalog.setadapter(adapter);
    }

expandablelistview展示数据的时候默认是每个模块下的列表项是闭合状态的,如果要实现初始化的时候就展开可以通过expandablelistview.expandgroup(location)方法来实现,而且每个父级列表项左边会出现一个系统自带的图标,这个图标是用来表示列表展开和闭合的状态的,如果不显示或者要替换这个图标可以用
expandablelistview.setgroupindicator(drawable icon)方法来实现,我这里是直接是没有使用任何图标,你也可以在adapter中自己在xml中定义自己的图标.
expandablelistview填充数据需要是二级菜单的模式所以数据结构大家可以根据项目情况而定,我这里由于标题是定死的所以只传的每个标题下的数据,下面是journalcataloglistadapter的代码:

复制代码 代码如下:

public class journalcataloglistadapter extends baseexpandablelistadapter {

    private layoutinflater inflater;

    private string[] parent = new string[] { "美颜美体", "潮流单品", "娱乐八卦", "情感",
            "观点", "健康生活" };

    private list<list<article>> clildren = new arraylist<list<article>>();

    public journalcataloglistadapter(context context,
            list<list<article>> clildren) {
        this.clildren = clildren;
        inflater = layoutinflater.from(context);
    }

    @override
    public object getchild(int groupposition, int childposition) {
        return clildren.get(groupposition).get(childposition);
    }

    @override
    public long getchildid(int groupposition, int childposition) {
        return childposition;
    }

    @override
    public view getchildview(int groupposition, int childposition,
            boolean islastchild, view convertview, viewgroup parent) {
        if (convertview == null) {
            convertview = inflater.inflate(
                    r.layout.journal_catalog_list_item_content, null);
        }
        textview textview = (textview) convertview
                .findviewbyid(r.id.tv_journal_catalog_list_item_content);
        article a = (article) getchild(groupposition, childposition);
        textview.settext(a.gettitle());
        return convertview;
    }

    @override
    public int getchildrencount(int groupposition) {
        return clildren.get(groupposition).size();
    }

    @override
    public object getgroup(int groupposition) {
        return parent[groupposition];
    }

    @override
    public int getgroupcount() {
        return parent.length;
    }

    @override
    public long getgroupid(int groupposition) {
        return groupposition;
    }

    @override
    public view getgroupview(int groupposition, boolean isexpanded,
            view convertview, viewgroup parent) {
        if (convertview == null) {
            convertview = inflater.inflate(
                    r.layout.journal_catalog_list_item_title, null);
        }
        textview textview = (textview) convertview
                .findviewbyid(r.id.tv_journal_catalog_list_item_title);

        string title = string.valueof(getgroup(groupposition));
        textview.settext(title);
        convertview.setonclicklistener(null);
        return convertview;
    }

    @override
    public boolean hasstableids() {
        return true;
    }

    @override
    public boolean ischildselectable(int groupposition, int childposition) {
        return true;
    }
}