二级列表SimpleExpandableListAdapter ,ExpandableListAdapter
2017-08-09 21:09
351 查看
第一种
package com.bwei.expandablelistview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ExpandableListView; import android.widget.SimpleExpandableListAdapter; public class MainActivity extends Activity { // 一级数据源 private String[] names = new String[] { "百度", "腾讯", "王过" }; // 二级数据源 private String[][] items = new String[][] { { "百度地图", "百度外卖", "百度云" }, { "QQ", "微信", "王者荣耀" }, { "AK47", "直播","茶杯" } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找控件 ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv); // 数据 定义子列表项List数据集合 List<List<Map<String, String>>> data = new ArrayList<List<Map<String, String>>>(); // 创建数据 一级 //定义父列表项List数据集合 List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); for (int i = 0; i < names.length; i++) { //提供父列表的数据 Map<String, String> map = new HashMap<String, String>(); map.put("name", names[i]); groupData.add(map); // 二级数据源 提供当前父列的子列数据 List<Map<String, String>> childData2 = new ArrayList<Map<String, String>>(); for (int j = 0; j < items.length; j++) { Map<String, String> map2 = new HashMap<String, String>(); map2.put("item", items[i][j]); childData2.add(map2); } data.add(childData2); } /** * 第一个参数 应用程序接口 this * 第二个父列List<?extends Map<String,Object>>集合 为父列提供数据 * 第三个参数 父列显示的组件资源文件 * 第四个参数 键值列表 父列Map字典的key * 第五个要显示的父列组件id * 第六个 子列的显示资源文件 * 第七个参数 键值列表的子列Map字典的key * 第八个要显示子列的组件id */ // 创建适配器 SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupData, R.layout.group_layout, new String[] { "name" }, new int[] { R.id.tv_name }, data, R.layout.child_layout, new String[] { "item" }, new int[] { R.id.tv_child }); // 设置适配器 elv.setAdapter(adapter); } }
第二种方式
package com.bwei.expandablelistview; import android.os.Bundle; import android.app.Activity; import android.database.DataSetObserver; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { // 一级数据源 private String[] names = new String[] { "百度", "腾讯", "王过" }; // 二级数据源 private String[][] items = new String[][] { { "百度地图", "百度外卖", "百度云" }, { "QQ", "微信", "王者荣耀" }, { "AK47", "直播" } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找控件 ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv); // 创建适配器 MyExpandableListAdapter adapter = new MyExpandableListAdapter(); // 设置适配器 elv.setAdapter(adapter); // 点击一级条目 elv.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "跳转吧" + groupPosition, 0) .show(); } }); // 设置一级的箭头 // elv.setGroupIndicator(null); /* * elv.setOnChildClickListener(new OnChildClickListener() { * * @Override public boolean onChildClick(ExpandableListView parent, View * v, int groupPosition, int childPosition, long id) { // TODO * Auto-generated method stub Toast.makeText(MainActivity.this, * "跳转:"+childPosition, 0).show(); return false; } }); */ } private class MyExpandableListAdapter implements ExpandableListAdapter { @Override public void registerDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public void unregisterDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } // 一级数据个数 @Override public int getGroupCount() { // TODO Auto-generated method stub return names.length; } // 二级数据个数 @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return items[groupPosition].length; } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return names[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return items[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return 0; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return 0; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } // 加载一级布局 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view =null; if (groupPosition == 0) { view = View.inflate(MainActivity.this, R.layout.group_layout, null); } else { view = View.inflate(MainActivity.this, R.layout.group, null); } TextView tv_name = (TextView) view.findViewById(R.id.tv_name); tv_name.setText(names[groupPosition]); return view; } // 加载二级布局 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = View.inflate(MainActivity.this, R.layout.child_layout, null); TextView tv_child = (TextView) view.findViewById(R.id.tv_child); tv_child.setText(items[groupPosition][childPosition]); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return false; } @Override public boolean areAllItemsEnabled() { // TODO Auto-generated method stub return false; } @Override public boolean isEmpty() { // TODO Auto-generated method stub return false; } @Override public void onGroupExpanded(int groupPosition) { // TODO Auto-generated method stub } @Override public void onGroupCollapsed(int groupPosition) { // TODO Auto-generated method stub } @Override public long getCombinedChildId(long groupId, long childId) { // TODO Auto-generated method stub return 0; } @Override public long getCombinedGroupId(long groupId) { // TODO Auto-generated method stub return 0; } } }
相关文章推荐
- 安卓学习--二级列表BaseExpandableListAdapter
- BaseExpandableListAdapter,二级列表的完全自定义(一)
- android二级列表(BaseExpandableListAdapter的使用) 仿通话记录
- ExpandableListView如何进行二级扩展(SimpleExpandableListAdapter)
- Android中文API(95)——SimpleExpandableListAdapter
- 可展开列表ExpandableListView—SimpleExpandableListAdpater
- 使用BaseExpandableListAdapter实现可折叠的列表
- Android适配器之-----SimpleExpandableListAdapter
- Android中文API(95)——SimpleExpandableListAdapter
- SimpleExpandableListAdapter
- Android ExpandableListView 进行二级扩展 (BaseExpandableListAdapter)
- Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)
- 【转】实现展开列ExpandableListView的三种方式之SimpleExpandableListAdapter实例
- BaseAdapter ArrayAdapter SimpleCursorAdapter SimpleAdapter BaseExpandableListAdapter
- marsAndroid学习——常见控件ExpandableListActivityh和SimpleExpandableListAdapter的简单使用
- SimpleExpandableListAdapter(分组List)参数详解
- android:使用BaseExpandableListAdapter实现可折叠的列表
- marsAndroid学习——常见控件ExpandableListActivityh和SimpleExpandableListAdapter的简单使用
- android:使用BaseExpandableListAdapter实现可折叠的列表
- 模拟QQ分组(具有伸缩功能) (添加开源框架的光闪烁效果)SimpleExpandableListAdapter 适配器的用法,并且可添加组及其组内数据。