Android判断view在屏幕可见

/**
* 判断当前view是否在屏幕可见
*/
public static boolean getLocalVisibleRect(Context context, View view, int offsetY) {
Point p = new Point();
((Activity) context).getWindowManager().getDefaultDisplay().getSize(p);
int screenWidth = p.x;
int screenHeight = p.y;
Rect rect = new Rect(0, 0, screenWidth, screenHeight);
int[] location = new int[2];
location[1] = location[1] + UIUtils.dip2px(context, offsetY);
view.getLocationInWindow(location);
view.setTag(location[1]);//存储y方向的位置
if (view.getLocalVisibleRect(rect)) {
return true;
} else {
return false;
}
}

经常配合滑动事件使用
scrollView.setOnScrollListener(new ObservableScrollView.OnScrollListener() {
@Override
public void onScrollStateChanged(ObservableScrollView view, int scrollState) {

}

@Override
public void onScroll(ObservableScrollView view, boolean isTouchScroll, int l, int t, int oldl, int oldt) {

}
});

}
posted on 2019-12-09 15:26  白衣雨果  阅读(9249)  评论(0编辑  收藏  举报