当Android View在App中可见时,是否会触发任何事件? [英] Is there any event fired when android View becomes visible within App?

查看:465
本文介绍了当Android View在App中可见时,是否会触发任何事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用包含多个视图(可滚动),其中一个是CustomView(扩展了View),当该View进入可见区域时是否触发了任何Android事件.

My App contains multiple views (scrollable), one of them is CustomView ( extends View), is there any android event fired when this View comes within visible area.

我在onAttachedToWindowonDetachedToWindow的时间段内使用后台线程的方法 该线程将使用customView.getGlobalVisibleRect(rectangle)来检查视图的任何部分在屏幕上是否可见,但这是自旋锁方法,是否有更好的方法来检测可见性,是否会触发任何可以调用getGlobalVisibleRect的事件 >

The approach i though of using background thread during the timeframe of onAttachedToWindow to onDetachedToWindow This thread will use customView.getGlobalVisibleRect(rectangle) to check whether any portion of the view is visible on the screen but this is spin lock approach, Is there any better way to detect visibility, any event which gets fired where I can call getGlobalVisibleRect

推荐答案

以下是使用 ViewTreeObserver 的一个示例代码:

Here is one sample code using ViewTreeObserver:

final View viewTemp = convertView;

convertView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
      int rowHeight = viewTemp.getGlobalVisibleRect();
      ...
   });

注意:

  • 我不知道您的代码,也没有发布.代码viewTemp = convertView只是一个示例,来自使用适配器的代码.
  • 由于多种原因绘制布局时,这是一个侦听器.
  • I don't know your code and it is not posted. The code viewTemp = convertView is just an example, code from using an Adapter.
  • This is a listener when the layout is drawn for whatever reason which is many.

这篇关于当Android View在App中可见时,是否会触发任何事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆