ExpandableListView
来源:互联网 发布:sql server怎么打开 编辑:程序博客网 时间:2024/05/16 23:33
1.ExpandableListView的group高度如何自定义?
在group中是采用自定义的布局,但是没有能改变group的高度,后来发现
group
的高度是系统给定的。需要对group
重新配置布局属性,如下:
/*-------------------------------------------* 改变每个分组的显示时大小* ---------------------------------------*/AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, //宽度 140);//高度convertView.setLayoutParams(layoutParams);
2.ExpandableListView嵌套ExpandableListView时,里层的ListView无法自适应内部控件的大小
能进行需要效果的处理,但是每一次都需要重新创建View,不能通过如下代码进行处理,不然会出错
if(convertView == null){}else{}
第一层Adapter中启动第二层(传入本层的ExpandableListView)
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){ //代码 //。。。。。。 ExpandableListView expandableListView = (ExpandableListView) convertView.findViewById(R.id.reading_item_expandable_listview); //传入`ExpandableListView` ReadingItemExpandAdapater readingItemExpandAdapater = new ReadingItemExpandAdapater(activity, readingQuestion.getReadingItems(), activity, question.expandableListView); //设置适配器 question.expandableListView.setAdapter(readingItemExpandAdapater); fixListViewHeight(question.expandableListView);}
第二层Adapter的onGroupExpanded
中对父层的ListView进行高度的重新计算
- 点击最里层
ExpandableListView
时,通过计算让父控件正确展开。
public void onGroupExpanded(int groupPosition){ fixListViewHeight(expandableListView); //改变父控件大小}
- fixListViewHeight是完成 对child View进行高度计算的功能
public void fixListViewHeight(ExpandableListView listView) { if(listView == null) return; // 如果没有设置数据适配器,则ListView没有子项,返回。 ListAdapter listAdapter = listView.getAdapter(); int totalHeight = 0; if (listAdapter == null) { return; } for (int index = 0, len = listAdapter.getCount(); index < len; index++) { View listViewItem = listAdapter.getView(index , null, listView); listViewItem.measure(0, 0); // 计算所有子项的高度和 totalHeight += listViewItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); // listView.getDividerHeight()获取子项间分隔符的高度 // params.height设置ListView完全显示需要的高度 params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
3. ExpandableListView如何展开
展开
//展开Groupfor(int i = 0; i < expandableAdapater.getGroupCount(); i++){ //展开组别 expandableListView.expandGroup(i); //下面这个无法实现 //expandableAdapater.onGroupExpanded(i);}
收缩
//收缩ExpandableListViewfor(int groupPosition = 0; groupPosition < expandableAdapater.getGroupCount(); groupPosition++){ expandableListView.collapseGroup(groupPosition);}
两层ExpandableListView嵌套,外加一层ListView,如何展开所有内容?
4. 动态获得控件
在ExpandableListView
中经常需要动态创建一些控件,简单代码如下:
private TextView getTextView(){ //属性如下 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); TextView textView = new TextView(activity); textView.setLayoutParams(lp); //偏移 textView.setPadding(50,0,0,0); textView.setTextSize(30); //内部文字位置 textView.setGravity(Gravity.CENTER_VERTICAL); return textView; }
5.getHeight和getMeasureHeight
- getWidth(): View在設定好佈局後整個View的寬度。
- getMeasuredWidth():對View上的內容進行測量後得到的View內容佔據的寬度
1 0
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- expandableListView
- ExpandableListView
- expandablelistview
- ExpandableListview
- ExpandableListView
- ExpandableListView
- ExpandableListView
- Expandablelistview
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- bootstrap datetimepicker 中文显示
- [jsp]JSP+DIV右键菜单
- SQLite和SQLiteOpenHelper
- 尚学堂lucene项目精讲视频
- cp936的表示
- ExpandableListView
- Failed to connect to socket /com/ubuntu/upstart: Connection refused: Errors were encountered while p
- 筆記
- linux下安装libcurl
- delphi 与 C++的基本语法区别
- java面向对象基础回顾
- Run Android apps in Windows, OS X, Linux with Chrome (and a modified runtime)
- MySQL安装
- Android Studio 找不到设备