android ExpandableListView (可扩展ListView)
2015-09-14 11:09
148 查看
ExpandableListView是listView的子类(效果类似于QQ好友界面的分组)
与Adapter类似,实现 ExpandableListView由如下三种常用方法
1.扩展BaseExpandableListAdapter实现ExpandableListAdapter
2.使用SimpleExpandableListAdapter将两个List集合包装秤ExpandableListAdapter
3.使用SimpleCursorTreeAdapter将Cursor中的数据包装成SimpleCursorTreeAdapter
与Adapter类似,实现 ExpandableListView由如下三种常用方法
1.扩展BaseExpandableListAdapter实现ExpandableListAdapter
2.使用SimpleExpandableListAdapter将两个List集合包装秤ExpandableListAdapter
3.使用SimpleCursorTreeAdapter将Cursor中的数据包装成SimpleCursorTreeAdapter
<span style="font-size:14px;">public class ExpandableListViewTest extends Activity { ExpandableListView expandableListView; int img [] = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher , R.drawable.ic_launcher, R.drawable. ic_launcher, R.drawable.ic_launcher }; private String name [] = new String[] { "张三", "李四" , "王五" , "赵六" , "孙七" }; private String action [][] = new String[][] { { "洼地", "除草" , "施肥" }, { "洼地", "除草" , "施肥" }, { "洼地", "除草", "施肥" }, { "洼地" , "除草" , "施肥" }, { "洼地", "除草" , "施肥" } }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout. activity_expandable_listview); ExpandableListAdapter adapter = new ExpandableListAdapter() { @Override public long getCombinedGroupId(long groupId) { // TODO Auto-generated method stub return 0; } @Override public boolean areAllItemsEnabled() { // TODO Auto-generated method stub return false ; } @Override public long getCombinedChildId(long groupId, long childId) { // TODO Auto-generated method stub return 0; } @Override public void unregisterDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public void registerDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public void onGroupExpanded(int groupPosition) { // TODO Auto-generated method stub } @Override public void onGroupCollapsed(int groupPosition) { // TODO Auto-generated method stub } @Override public boolean isEmpty() { // TODO Auto-generated method stub return false ; } //此处改为true @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true ; } //此处改为true @Override public boolean hasStableIds() { // TODO Auto-generated method stub return true ; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LinearLayout layout = new LinearLayout(ExpandableListViewTest.this ); layout.setOrientation(LinearLayout. HORIZONTAL); ImageView view = new ImageView(ExpandableListViewTest.this ); view.setImageResource( img[groupPosition]);//根据getGroupId返回的groupPosition来获取图片的ID layout.addView(view); TextView text = new TextView(ExpandableListViewTest.this ); text.setTextSize(20); text.setText(getGroup(groupPosition).toString()); //根据getGroup()方法来获取每一个text数据 layout.addView(text); return layout; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } //返回父类数组的长度 @Override public int getGroupCount() { // TODO Auto-generated method stub return name .length ; } //返回父类数组的每一个对象 @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return name [groupPosition]; } //返回每一组子选项数组的长度 @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return action [groupPosition].length; } //返回的text作为子列表的每一列的text @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub TextView text = new TextView(ExpandableListViewTest.this ); text.setTextSize(20); text.setText(getChild(groupPosition, childPosition).toString()); return text; } //返回子列表的id @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } //从数组获取子列表的每一个数据 @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return action [groupPosition][childPosition]; } }; expandableListView = (ExpandableListView) findViewById(R.id.expandable ); expandableListView.setAdapter(adapter); } } </span>
相关文章推荐
- Android开发在string.xml文件中设置部分字体颜色大小
- android acivity 和 service 通信
- 解锁android设备遇到错误bootloader Check 'Allow OEM Unlock' in Developer Options
- Android快速开发框架汇总
- android listview 属性详解
- 修改Android studio主题
- android AbsListView之ListView使用(2)-BaseAdapter
- android AbsListView之ListView使用(1)
- android-自定义Dialog
- Android底层的学习经验介绍
- Android 播放视频文件
- android ImageSwitcher
- Android基础控件ToggleButton→ Switch→ SeekBar→ProgressBar 控件
- android ViewSwticher
- phoneGap(cordova)插件开发学习
- Android之日期及时间选择对话框用法实例分析
- Android-RoundCornerProgressBar——Android圆角进度条(Progress Bar)类库
- Android学习文档之秘密发送短信
- Argument for @NotNull parameter 'name' of com/android/tools/idea/welcome/Platform.<init> must not be
- Android自定义ProgressBar