我听 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); } } });更多推荐
停止监听更多的监听器事件
发布评论