Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions app/src/main/java/com/stay4it/utilities/view/ViewUtils.java
Original file line number Diff line number Diff line change
@@ -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 extends View> T findViewById(View layout, int id) {
return (T) layout.findViewById(id);
}




}