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); }