如何判断当前View对用户是否可见?

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;
    }
}

你可能感兴趣的:(如何判断当前View对用户是否可见?)