android使用ExpandableListView控件实现小说目录效果的例子
今天给大家讲讲android的目录实现方法,就像大家看到的小说目录一样,android提供了ExpandableListView控件可以实现二级列表展示效果,现在给大家讲讲这个控件的用法,下面是XML定义:
<?xmlversion="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中的代码:
privateExpandableListViewelv_journal_catalog;
privateList<List<Article>>childrenObj;
privateJournalCatalogListAdapteradapter;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.journal_catalog);
init();
elv_journal_catalog.setGroupIndicator(null);
elv_journal_catalog.setDivider(null);
loadData();
}
privatevoidinit(){
elv_journal_catalog=(ExpandableListView)findViewById(R.id.elv_journal_catalog);
elv_journal_catalog.setOnChildClickListener(listener);
}
privatevoidloadData(){
Messagemsg=handler.obtainMessage();
msg.what=1;
msg.sendToTarget();
childrenObj=newArrayList<List<Article>>();
newThread(){
@Override
publicvoidrun(){
if(!isLoading){
queryArticleList();
}else{
queryArticleListFromSqlite();
}
}
}.start();
adapter=newJournalCatalogListAdapter(this,childrenObj);
elv_journal_catalog.setAdapter(adapter);
}
ExpandableListView展示数据的时候默认是每个模块下的列表项是闭合状态的,如果要实现初始化的时候就展开可以通过ExpandableListView.expandGroup(location)方法来实现,而且每个父级列表项左边会出现一个系统自带的图标,这个图标是用来表示列表展开和闭合的状态的,如果不显示或者要替换这个图标可以用
ExpandableListView.setGroupIndicator(Drawableicon)方法来实现,我这里是直接是没有使用任何图标,你也可以在adapter中自己在xml中定义自己的图标.
ExpandableListView填充数据需要是二级菜单的模式所以数据结构大家可以根据项目情况而定,我这里由于标题是定死的所以只传的每个标题下的数据,下面是JournalCatalogListAdapter的代码:
privateLayoutInflaterinflater; privateString[]parent=newString[]{"美颜美体","潮流单品","娱乐八卦","情感", privateList<List<Article>>clildren=newArrayList<List<Article>>(); publicJournalCatalogListAdapter(Contextcontext, @Override @Override @Override @Override @Override @Override @Override @Override Stringtitle=String.valueOf(getGroup(groupPosition)); @Override @Override
publicclassJournalCatalogListAdapterextendsBaseExpandableListAdapter{
"观点","健康生活"};
List<List<Article>>clildren){
this.clildren=clildren;
inflater=LayoutInflater.from(context);
}
publicObjectgetChild(intgroupPosition,intchildPosition){
returnclildren.get(groupPosition).get(childPosition);
}
publiclonggetChildId(intgroupPosition,intchildPosition){
returnchildPosition;
}
publicViewgetChildView(intgroupPosition,intchildPosition,
booleanisLastChild,ViewconvertView,ViewGroupparent){
if(convertView==null){
convertView=inflater.inflate(
R.layout.journal_catalog_list_item_content,null);
}
TextViewtextView=(TextView)convertView
.findViewById(R.id.tv_journal_catalog_list_item_content);
Articlea=(Article)getChild(groupPosition,childPosition);
textView.setText(a.getTitle());
returnconvertView;
}
publicintgetChildrenCount(intgroupPosition){
returnclildren.get(groupPosition).size();
}
publicObjectgetGroup(intgroupPosition){
returnparent[groupPosition];
}
publicintgetGroupCount(){
returnparent.length;
}
publiclonggetGroupId(intgroupPosition){
returngroupPosition;
}
publicViewgetGroupView(intgroupPosition,booleanisExpanded,
ViewconvertView,ViewGroupparent){
if(convertView==null){
convertView=inflater.inflate(
R.layout.journal_catalog_list_item_title,null);
}
TextViewtextView=(TextView)convertView
.findViewById(R.id.tv_journal_catalog_list_item_title);
textView.setText(title);
convertView.setOnClickListener(null);
returnconvertView;
}
publicbooleanhasStableIds(){
returntrue;
}
publicbooleanisChildSelectable(intgroupPosition,intchildPosition){
returntrue;
}
}
相关文章