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

2019-12-10 20:02:16王振洲

        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[] { "美颜美体", "潮流单品", "娱乐八卦", "情感",