From 02f60424d7284c9f5486a206c8bbf71a00d745d8 Mon Sep 17 00:00:00 2001 From: wanghao <1045189341@qq.com> Date: Wed, 18 May 2016 15:02:05 +0800 Subject: [PATCH] add viewUtils --- .../com/stay4it/utilities/view/ViewUtils.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 app/src/main/java/com/stay4it/utilities/view/ViewUtils.java diff --git a/app/src/main/java/com/stay4it/utilities/view/ViewUtils.java b/app/src/main/java/com/stay4it/utilities/view/ViewUtils.java new file mode 100644 index 0000000..e108977 --- /dev/null +++ b/app/src/main/java/com/stay4it/utilities/view/ViewUtils.java @@ -0,0 +1,55 @@ +package com.stay4it.utilities.view; + +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewParent; + +public class ViewUtils { + /** 把自身从父View中移除 */ + public static void removeSelfFromParent(View view) { + //先找到父类,再通过父类移除孩子 + if (view != null) { + ViewParent parent = view.getParent(); + if (parent != null && parent instanceof ViewGroup) { + ViewGroup group = (ViewGroup) parent; + group.removeView(view); + } + } + } + +/** 请求View树重新布局,用于解决中层View有布局状态而导致上层View状态断裂 */ +public static void requestLayoutParent(View view, boolean isAll) { + ViewParent parent = view.getParent(); + while (parent != null && parent instanceof View) { + if (!parent.isLayoutRequested()) { + parent.requestLayout(); + if (!isAll) { + break; + } + } + parent = parent.getParent(); + } +} + +/** 判断触点是否落在该View上 */ +public static boolean isTouchInView(MotionEvent ev, View v) { + int[] vLoc = new int[2]; + v.getLocationOnScreen(vLoc); + float motionX = ev.getRawX(); + float motionY = ev.getRawY(); + return motionX >= vLoc[0] && + motionX <= (vLoc[0] + v.getWidth()) && + motionY >= vLoc[1] && + motionY <= (vLoc[1] + v.getHeight()); +} + +/** FindViewById的泛型封装,减少强转代码 */ +public static T findViewById(View layout, int id) { + return (T) layout.findViewById(id); +} + + + + +}