自己的View工具类---ViewUtil
来源:互联网 发布:handler源码解析 编辑:程序博客网 时间:2024/05/19 17:57
package com.app.util;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ListAdapter;import android.widget.ListView;/** * 解决view高度之类的问题 * @author Wonghoman */public class ViewUtil {private final String TAG = "ViewUtil";/** * 动态改变listView的高度 * 计算ExpandableListView的group项的高度的方法(即计算listview合并时的高度的方法) * 备注:ExpandableListView是listview的子类所以,listview的方法它同样可以使用 * * maxHeight:最大高度,不作限制的话用“非正数”表示 */public static void setListViewHeightBasedOnChildren(ListView listView, int maxHeight) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if(listAdapter == null) { return; } int totalHeight = 0; for(int i = 0, len = listAdapter.getCount(); i < len; i++) { View listItem = listAdapter.getView(i, null, listView); if(listItem == null) return; listItem.measure(0, 0); // 计算子项View 的宽高 totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); // listView.getDividerHeight()获取子项间分隔符占用的高度 if(maxHeight > 0){ params.height = params.height > maxHeight ? maxHeight : params.height; } listView.setLayoutParams(params); }/** * 可扩展listview展开时调用 * 计算ExpandableListView展开时group下的子view列表的高度的方法 * @param listView * @param groupPosition */ public static void setExpandedListViewHeightBasedOnChildren( ExpandableListView listView, int groupPosition) { ExpandableListAdapter listAdapter = listView.getExpandableListAdapter(); if (listAdapter == null) { return; } View listItem = listAdapter.getChildView(groupPosition, 0, true, null, listView); listItem.measure(0, 0); int appendHeight = 0; for (int i = 0; i < listAdapter.getChildrenCount(groupPosition); i++) { appendHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams();// Log.d(TAG, "Expand params.height" + params.height); params.height += appendHeight; listView.setLayoutParams(params); } /** * 可扩展listview收起时调用 * 计算ExpandableListView合并时消除group下的子view列表的高度的方法 * @param listView * @param groupPosition */ public static void setCollapseListViewHeightBasedOnChildren( ExpandableListView listView, int groupPosition) { ExpandableListAdapter listAdapter = listView.getExpandableListAdapter(); if (listAdapter == null) { return; } View listItem = listAdapter.getChildView(groupPosition, 0, true, null, listView); listItem.measure(0, 0); int appendHeight = 0; for (int i = 0; i < listAdapter.getChildrenCount(groupPosition); i++) { appendHeight += listItem.getMeasuredHeight(); } /*Log.d(TAG, "Collapse childCount=" + listAdapter.getChildrenCount(groupPosition));*/ ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height -= appendHeight; listView.setLayoutParams(params); } /** * 判断点击屏幕的位置是不是在指定的view上 * @param view * @param ev * @return */ public static boolean inRangeOfView(View view, MotionEvent ev) {int[] location = new int[2];view.getLocationOnScreen(location);int x = location[0];int y = location[1];if (ev.getX() < x || ev.getX() > (x + view.getWidth()) || ev.getY() < y|| ev.getY() > (y + view.getHeight())) {return false;}return true;}}
0 0
- 自己的View工具类---ViewUtil
- 定制自己的工具类
- 定义自己的View
- 自己写的数据库的工具类
- 自己编写的JDBC的工具类
- 45.Android ViewUtil
- 拆分显示自己的view
- 如何自定义自己的VIEW
- 自己的工具类 倒影类Reflection
- 扩展 JDOM,定制自己的工具类
- html工具类收藏,自己封装的
- 自己写的文件操作工具类
- 自己实现的JDBC工具类
- 自己用的工具类(一)
- 分享下自己的lucene工具类
- 自己实现的JDBC工具类
- experience00: 建立自己的工具类
- 自己写的Properties工具类
- Linux下Scoket聊天程序
- HDU 1285 topological_sort
- Notification(二)——PendingIntent的flag导致数据相同的问题
- PHP: php_ldap.dll不能加载解决方案
- hdoj.1004 Let the Balloon Rise 20140726
- 自己的View工具类---ViewUtil
- LVSj实现负载均衡
- java对Blob读写
- Android上传图片到服务器上的一些问题
- HDU4893 2014多校第三场1007 Wow! Such Sequence!
- hdoj.1256 画8 20140726
- 数据的交换输出
- rysnc和intotify实现数据同步
- Qt 多线程学习