Reputation: 8320
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.
I tried using removeOnGlobalLayoutListener()
method but that gives warning that call required API level 16 (current min is 14).
I also tried using removeGlobalOnLayoutListener
. But it is deprecated.
Code :
searchWebView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Do something
// Remove listener
}
});
How to remove onGlobalLayoutListener
?
Edit :
Upvotes: 2
Views: 6014
Reputation: 51571
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);
}
}
});
Upvotes: 7