android使用ExpandableListView控件實現(xiàn)小說目錄效果的例子
今天給大家講講android的目錄實現(xiàn)方法,就像大家看到的小說目錄一樣,android 提供了ExpandableListView控件可以實現(xiàn)二級列表展示效果,現(xiàn)在給大家講講這個控件的用法,下面是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ù)的時候默認是每個模塊下的列表項是閉合狀態(tài)的,如果要實現(xiàn)初始化的時候就展開可以通過ExpandableListView.expandGroup(location)方法來實現(xiàn),而且每個父級列表項左邊會出現(xiàn)一個系統(tǒng)自帶的圖標,這個圖標是用來表示列表展開和閉合的狀態(tài)的,如果不顯示或者要替換這個圖標可以用
ExpandableListView.setGroupIndicator(Drawable icon)方法來實現(xiàn),我這里是直接是沒有使用任何圖標,你也可以在adapter中自己在xml中定義自己的圖標.
ExpandableListView填充數(shù)據(jù)需要是二級菜單的模式所以數(shù)據(jù)結(jié)構大家可以根據(jù)項目情況而定,我這里由于標題是定死的所以只傳的每個標題下的數(shù)據(jù),下面是JournalCatalogListAdapter的代碼:
public class JournalCatalogListAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private String[] parent = new String[] { "美顏美體", "潮流單品", "娛樂八卦", "情感",
"觀點", "健康生活" };
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 關于ExpandableListView刷新問題的解決方法
- Android UI控件ExpandableListView基本用法詳解
- Android改變ExpandableListView的indicator圖標實現(xiàn)方法
- 分享Android中ExpandableListView控件使用教程
- Android中ExpandableListView的用法實例
- Android ExpandableListView展開列表控件使用實例
- Android ExpandableListView長按事件的完美解決辦法
- Android之IphoneTreeView帶組指示器的ExpandableListView效果
- Android之帶group指示器的ExpandableListView(自寫)
- Android中ExpandableListView使用示例詳解
相關文章
Android開發(fā)環(huán)境搭建圖文教程 親測有效!
這篇文章主要為大家詳細介紹了Android開發(fā)環(huán)境搭建圖文教程,親自測試有效的搭建方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android開發(fā)控制ScrollView滑動速度的方法
這篇文章主要介紹了Android開發(fā)控制ScrollView滑動速度的方法,結(jié)合實例形式分析了Android編程中ScrollView滑動事件相關操作技巧,需要的朋友可以參考下2017-02-02studio碰到問題:java.lang.UnsatisfiedLinkError解決辦法
這篇文章主要介紹了studio碰到問題:java.lang.UnsatisfiedLinkError解決辦法的相關資料,需要的朋友可以參考下2017-02-02Android開發(fā)之AlarmManager的用法詳解
這篇文章主要介紹了Android開發(fā)之AlarmManager的用法,是Android應用開發(fā)中非常實用的技能,需要的朋友可以參考下2014-07-07