源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

android使用ExpandableListView控件实现小说目录效果的例子

  • 时间:2020-07-17 17:48 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:android使用ExpandableListView控件实现小说目录效果的例子
今天给大家讲讲android的目录实现方法,就像大家看到的小说目录一样,android 提供了ExpandableListView控件可以实现二级列表展示效果,现在给大家讲讲这个控件的用法,下面是XML定义:
[u]复制代码[/u] 代码如下:
<?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中的代码:
[u]复制代码[/u] 代码如下:
 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展示数据的时候默认是每个模块下的列表项是闭合状态的,如果要实现初始化的时候就展开可以通过ExpandableListView.expandGroup(location)方法来实现,而且每个父级列表项左边会出现一个系统自带的图标,这个图标是用来表示列表展开和闭合的状态的,如果不显示或者要替换这个图标可以用 ExpandableListView.setGroupIndicator(Drawable icon)方法来实现,我这里是直接是没有使用任何图标,你也可以在adapter中自己在xml中定义自己的图标. ExpandableListView填充数据需要是二级菜单的模式所以数据结构大家可以根据项目情况而定,我这里由于标题是定死的所以只传的每个标题下的数据,下面是JournalCatalogListAdapter的代码:
[u]复制代码[/u] 代码如下:
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;     } }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部