ExpandableListView使用详解
2016-01-03 15:54
302 查看
ExpandableListView使用详解
ONE Goal,ONE Passion !中午一觉醒来看到群里有人问一个效果是如何做出来的,我看了来看,就是可以展开列表像的listView嘛.
已经给那为童鞋写了个简单demo希望对他有所帮助.
其他类似方法实现请点击:
/article/8728090.html
第1步: 使用在xml中使用ExpandableListView.
a. ExpandableListView额外支持的一些常用xml属性:xml属性 | 说明 |
---|---|
android:childDivider | 指定各组内各个列表项之间的分割条 |
android:childIndicator | 显示在子列表旁边的Drawable对象 |
android:groupIndicator | 显示在组列表旁边的Drawable对象 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.windowtest1.ExpandableActivity"> <ExpandableListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"></ExpandableListView> </RelativeLayout>
第2步:使用baseExpandableListAdapter填充数据
public class ExpandableActivity extends Activity { ExpandableListView lv; ExpandableActivity act; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_expandable); act = this; lv = (ExpandableListView) findViewById(R.id.list); ExpandableListAdapter adapter = new BaseExpandableListAdapter() { String[] types = new String[]{"样式", "图案", "适合人群", "克重"}; String[][] items = new String[][] { {"吊坠", "项链"}, {"生肖", "卡通"}, {"全部", "男", "女", "儿童"}, {"1-2克", "2-5克"} }; // 获得指定位置,指定子列表项数据 @Override public Object getChild(int groupPosition, int childPosition) { return items[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return types[groupPosition].length(); } // 该方法决定每个子选项的外观 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView tv = new TextView(act); tv.setPadding(20, 0, 0, 0); tv.setText(getChild(groupPosition, childPosition).toString()); return tv; } // 获得指定位置出的数据 @Override public Object getGroup(int groupPosition) { return types[groupPosition]; } @Override public int getGroupCount() { return types.length; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView tv = new TextView(act); tv.setTextSize(40); tv.setText(getGroup(groupPosition).toString()); return tv; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { //打印日志看看选择的具体某一项 System.out.println("groupPosition" + types[groupPosition] + "----" + "childPosition" + items[groupPosition][childPosition]); return false; } }; lv.setAdapter(adapter); } }
代码比较简单就不在解释了.看效果就可以了.
咦…有没有发现,我用红色线圈住的地方有一个向上的箭头.其实那是ExpandableListView 自带的,看起来很不爽啊. 当然我们可以选择去隐藏或者换成其他图标; 这就用到了上面提到的属性:
我们只需在xml中加上这句话就ok了.
android:groupIndicator="@null"
再来看效果: 已经没有了那个图标.
相关文章推荐
- C#反射示例代码
- 4.2.5 Android 下拉刷新的几个方法:SwipeRefreshLayout,android-Ultra-Pull-To-Refresh(ptr),PullToRefreshListView
- 【LWJGL2 WIKI】【辅助库篇】Slick-Util库:介绍
- Android高性能加载大量图片系列课程2-在非UI线程中处理图片
- LSGO软件技术团队2015~2016学年第十五周(1207~1213)总结
- Android UDP简单实现
- 【数组】word search
- FPGA引脚锁定 注意err和高阻状态
- ps/top/htop/dstat/命令
- Thinking in Java-笔记
- mysql备份
- LSGO软件技术团队2015~2016学年第十四周(1130~1206)总结
- Spring 整合redis
- springMVC笔记(一) Controller的实现和配置(一)
- Linux程序包管理和yum用法
- WPF学习日志1---DataGrid绑定与修改更新(数据List & 数据库)[部分代码有摘录其他文章]
- 使用Jquery解析Json基础知识(转)
- My97DatePickerBeta 时间选择控件用法
- ThreadGroup
- 如何搜索自己博客的文章