停止监听更多的监听器事件

编程入门 行业动态 更新时间:2024-10-25 10:26:41
本文介绍了停止监听更多的监听器事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我听 onGlobalLayoutListener 如下code如图4所示。我要听它只有一次。一旦 onGlobalLayout()叫我想停下来倾听。

I listen to onGlobalLayoutListener as shown in below code. I want to listen it only once. Once onGlobalLayout() is called I want to stop listening to it.

我试着用 removeOnGlobalLayoutListener()方法,但给出的警告需要调用API 16级(当前分钟为14)。

I tried using removeOnGlobalLayoutListener() method but that gives warning that call required API level 16 (current min is 14).

我也试过用 removeGlobalOnLayoutListener 。但它去precated。

I also tried using removeGlobalOnLayoutListener. But it is deprecated.

code:

searchWebView.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // Do something // Remove listener } });

如何删除 onGlobalLayoutListener ?

编辑:

推荐答案

您可以使用以下code删除 OnGlobalLayoutListener 。仍然会有一个删除线在 removeGlobalOnLayoutListener(),这一直是德precated。但是,皮棉警告将使用注解来照顾。

You can remove the OnGlobalLayoutListener using the following code. There will still be a strikethrough on removeGlobalOnLayoutListener(), which has been deprecated. But, the lint warning will be taken care of by using the annotations.

@SuppressWarnings("deprecation") @SuppressLint("NewApi") searchWebView.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { searchWebView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { searchWebView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } } });

更多推荐

停止监听更多的监听器事件

本文发布于:2023-11-12 18:49:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:监听器   事件   更多

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!