时间:2022-04-20 08:58:52 | 栏目:Android代码 | 点击:次
今天给大家讲讲android的目录实现方法,就像大家看到的小说目录一样,android 提供了ExpandableListView控件可以实现二级列表展示效果,现在给大家讲讲这个控件的用法,下面是XML定义:
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的代码:
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;
}
}