android使用ExpandableListView控件實(shí)現(xiàn)小說(shuō)目錄效果的例子
今天給大家講講android的目錄實(shí)現(xiàn)方法,就像大家看到的小說(shuō)目錄一樣,android 提供了ExpandableListView控件可以實(shí)現(xiàn)二級(jí)列表展示效果,現(xiàn)在給大家講講這個(gè)控件的用法,下面是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>
這代碼很簡(jiǎn)單,和寫listView的方法差不多,接下來(lái)是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展示數(shù)據(jù)的時(shí)候默認(rèn)是每個(gè)模塊下的列表項(xiàng)是閉合狀態(tài)的,如果要實(shí)現(xiàn)初始化的時(shí)候就展開可以通過(guò)ExpandableListView.expandGroup(location)方法來(lái)實(shí)現(xiàn),而且每個(gè)父級(jí)列表項(xiàng)左邊會(huì)出現(xiàn)一個(gè)系統(tǒng)自帶的圖標(biāo),這個(gè)圖標(biāo)是用來(lái)表示列表展開和閉合的狀態(tài)的,如果不顯示或者要替換這個(gè)圖標(biāo)可以用
ExpandableListView.setGroupIndicator(Drawable icon)方法來(lái)實(shí)現(xiàn),我這里是直接是沒(méi)有使用任何圖標(biāo),你也可以在adapter中自己在xml中定義自己的圖標(biāo).
ExpandableListView填充數(shù)據(jù)需要是二級(jí)菜單的模式所以數(shù)據(jù)結(jié)構(gòu)大家可以根據(jù)項(xiàng)目情況而定,我這里由于標(biāo)題是定死的所以只傳的每個(gè)標(biāo)題下的數(shù)據(jù),下面是JournalCatalogListAdapter的代碼:
public class JournalCatalogListAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private String[] parent = new String[] { "美顏美體", "潮流單品", "娛樂(lè)八卦", "情感",
"觀點(diǎn)", "健康生活" };
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;
}
}
- Android 關(guān)于ExpandableListView刷新問(wèn)題的解決方法
- Android UI控件ExpandableListView基本用法詳解
- Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
- 分享Android中ExpandableListView控件使用教程
- Android中ExpandableListView的用法實(shí)例
- Android ExpandableListView展開列表控件使用實(shí)例
- Android ExpandableListView長(zhǎng)按事件的完美解決辦法
- Android之IphoneTreeView帶組指示器的ExpandableListView效果
- Android之帶group指示器的ExpandableListView(自寫)
- Android中ExpandableListView使用示例詳解
相關(guān)文章
Android中關(guān)于百度糯米app關(guān)閉網(wǎng)頁(yè)或窗口的方法(99%人不知)
這篇文章主要介紹了Android中關(guān)于百度糯米app中關(guān)閉網(wǎng)頁(yè)或窗口的方法,其實(shí)解決方法到很簡(jiǎn)單,但是很多人都不知道如何解決的,在網(wǎng)上也很難找到答案的,下面小編給大家揭曉答案,需要的朋友可以參考下2016-08-08
Android Bitmap詳解及Bitmap的內(nèi)存優(yōu)化
這篇文章主要介紹了Android Bitmap詳解及Bitmap的內(nèi)存優(yōu)化的相關(guān)資料,Bitmap是Android系統(tǒng)中的圖像處理的最重要類之一。用它可以獲取圖像文件信息,進(jìn)行圖像剪切、旋轉(zhuǎn)、縮放等操作,并可以指定格式保存圖像文件,需要的朋友可以參考下2017-03-03
Android入門之在Activity之間穿梭的Intent
Intent可以用來(lái)啟動(dòng)Activity(startActivity(Intent))、Serveice(startService(Intent))等組件,可以用來(lái)綁定Activity和Service以建立它們之間的通信(bindServiceConnaction(Intent,ServiceConnection,int)),可以作為Broadcast Intent發(fā)送給廣播接收器2021-10-10
Android中傳值Intent與Bundle的區(qū)別小結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于Android中傳值Intent與Bundle的區(qū)別,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Android學(xué)習(xí)筆記——Menu介紹(三)
今天繼續(xù)昨天沒(méi)有講完的Menu的學(xué)習(xí),主要是Popup Menu的學(xué)習(xí),需要的朋友可以參考下2014-10-10
android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息
這篇文章主要介紹了android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
Android學(xué)習(xí)教程之圓形Menu菜單制作方法(1)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)教程之圓形Menu菜單操作代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android Compose實(shí)現(xiàn)底部按鈕以及首頁(yè)內(nèi)容詳細(xì)過(guò)程
這篇文章主要介紹了如何利用compose框架制作app底部按鈕以及首頁(yè)內(nèi)容的詳細(xì)代碼,具有一定價(jià)值,感興趣的可以了解一下2021-11-11
Android SeekBar實(shí)現(xiàn)禁止滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android SeekBar實(shí)現(xiàn)禁止滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03

