use*_*544 37 android visible android-scrollview android-view
我要检查,如果View
内的ScrollView
是目前Android中可见.我没有检查它是否专注于它,但它是否正在屏幕上显示.有没有一种方法View
可以告诉我视图当前是否可见?
小智 27
这段代码适合我:
public static boolean isVisible(final View view) {
if (view == null) {
return false;
}
if (!view.isShown()) {
return false;
}
final Rect actualPosition = new Rect();
view.getGlobalVisibleRect(actualPosition);
final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight());
return actualPosition.intersect(screen);
}
Run Code Online (Sandbox Code Playgroud)
小智 18
zegee29的回答很有帮助。虽然我想建议使用view.getGlobalVisibleRect(actualPosition)
too的结果,因为在某些情况下当item根本不可见时Rect.intersects()
返回,所以生成的代码是:true
fun View.isVisible(): Boolean {
if (!isShown) {
return false
}
val actualPosition = Rect()
val isGlobalVisible = getGlobalVisibleRect(actualPosition)
val screenWidth = Resources.getSystem().displayMetrics.widthPixels
val screenHeight = Resources.getSystem().displayMetrics.heightPixels
val screen = Rect(0, 0, screenWidth, screenHeight)
return isGlobalVisible && Rect.intersects(actualPosition, screen)
}
Run Code Online (Sandbox Code Playgroud)
或者你可能只是结果getGlobalVisibleRect(actualPosition)
AA_*_*_PV 15
int[] location = new int[2];
view.getLocationOnScreen(location);
Run Code Online (Sandbox Code Playgroud)
要么
Rect rect = new Rect();
view.getGlobalVisibleRect(rect);
Run Code Online (Sandbox Code Playgroud)
现在使用此位置或矩形来检查它是否在您的可见边界内.如果它只是整个屏幕,请检查getResources().getDisplayMetrics()
.
正如Antek在下面的评论中指出的那样,视图可能仍然消失或不可见,返回值告诉它最后绘制的位置.因此,将上述与边界相关的条件与view.isShown()
或view.getVisibility() == VISIBLE
应该将其结合起来.
sec*_*tlm -31
函数 View.getVisibility() 可以具有以下值:
View.VISIBLE (0):视图可见。
View.INVISIBLE (1):视图不可见,但出于布局目的,它仍然占用空间。
View.GONE (2):视图消失了。完全隐藏,就好像视图没有被添加一样
您可以查看下面的链接以获取更多信息。 如何检查 Android 中的视图是否可见?
归档时间: |
|
查看次数: |
34485 次 |
最近记录: |