如何判断当前View对用户是否可见?
2021-05-08 本文已影响0人
好学人
public class ViewUtils {
private static class InitFactory {
private static final Rect RECT = new Rect();
}
/**
* 当前View是否对用户可见
*/
public static boolean isVisibleToUser(View view) {
// 判断当前View及其父View是否可见
boolean isShown = view.isShown();
// 判断当前View所在的Window是否可见
boolean hasWindowFocus = view.hasWindowFocus();
boolean isWindowVisible = view.getWindowVisibility() == View.VISIBLE;
// 是否在屏幕的显示区域内
boolean globalVisible = view.getGlobalVisibleRect(InitFactory.RECT);
// 综合判断当前View是否可见
return isShown && hasWindowFocus && isWindowVisible && globalVisible;
}
}