android使用ExpandableListView控件實(shí)現(xiàn)小說目錄效果的例子
今天給大家講講android的目錄實(shí)現(xiàn)方法,就像大家看到的小說目錄一樣,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>
這代碼很簡單,和寫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展示數(shù)據(jù)的時(shí)候默認(rèn)是每個(gè)模塊下的列表項(xiàng)是閉合狀態(tài)的,如果要實(shí)現(xiàn)初始化的時(shí)候就展開可以通過ExpandableListView.expandGroup(location)方法來實(shí)現(xiàn),而且每個(gè)父級(jí)列表項(xiàng)左邊會(huì)出現(xiàn)一個(gè)系統(tǒng)自帶的圖標(biāo),這個(gè)圖標(biāo)是用來表示列表展開和閉合的狀態(tài)的,如果不顯示或者要替換這個(gè)圖標(biāo)可以用
ExpandableListView.setGroupIndicator(Drawable icon)方法來實(shí)現(xiàn),我這里是直接是沒有使用任何圖標(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[] { "美顏美體", "潮流單品", "娛樂八卦", "情感",
"觀點(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刷新問題的解決方法
- Android UI控件ExpandableListView基本用法詳解
- Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
- 分享Android中ExpandableListView控件使用教程
- Android中ExpandableListView的用法實(shí)例
- Android ExpandableListView展開列表控件使用實(shí)例
- Android ExpandableListView長按事件的完美解決辦法
- Android之IphoneTreeView帶組指示器的ExpandableListView效果
- Android之帶group指示器的ExpandableListView(自寫)
- Android中ExpandableListView使用示例詳解
相關(guān)文章
Android編程之殺毒的實(shí)現(xiàn)原理及具體實(shí)例
這篇文章主要介紹了Android編程之殺毒的實(shí)現(xiàn)原理及具體實(shí)例,結(jié)合實(shí)例形式分析了Android殺毒功能的原理與簡單實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-12-12Android開發(fā)環(huán)境搭建圖文教程 親測(cè)有效!
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)環(huán)境搭建圖文教程,親自測(cè)試有效的搭建方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android開發(fā)控制ScrollView滑動(dòng)速度的方法
這篇文章主要介紹了Android開發(fā)控制ScrollView滑動(dòng)速度的方法,結(jié)合實(shí)例形式分析了Android編程中ScrollView滑動(dòng)事件相關(guān)操作技巧,需要的朋友可以參考下2017-02-02Flutter質(zhì)感設(shè)計(jì)之進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08studio碰到問題:java.lang.UnsatisfiedLinkError解決辦法
這篇文章主要介紹了studio碰到問題:java.lang.UnsatisfiedLinkError解決辦法的相關(guān)資料,需要的朋友可以參考下2017-02-02Android開發(fā)之AlarmManager的用法詳解
這篇文章主要介紹了Android開發(fā)之AlarmManager的用法,是Android應(yīng)用開發(fā)中非常實(shí)用的技能,需要的朋友可以參考下2014-07-07