I have RecyclerView
and, in some cases, I show another view on top of it - ProgressDialog
, AlertDialog
or DialogFragment
.
Is there some way to notify me about cases when my RecyclerView
is at front or another view is above it now?
I tried to add onFocusChangeListener()
to my RecyclerView
, but no luck.
PS. Definitely, I can in my RecyclerView
create some method isOnFront(boolean onFront)
and in all my other views call it, but maybe there is some more elegance way?
ProgressDialog
, AlertDialog
and DialogFragment
would lay their content on the Window
, not to your activity's/fragment's view hierarchy. Which means, that as soon as either of them is shown, the focus of Window
is changed. Hence, you can make use of ViewTreeObserver#addOnWindowFocusChangeListener()
API:
contentView.getViewTreeObserver().addOnWindowFocusChangeListener(
new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override public void onWindowFocusChanged(boolean hasFocus) {
// Remove observer when no longer needed
// contentView.getViewTreeObserver().removeOnWindowFocusChangeListener(this);
if (hasFocus) {
// Your view hierarchy is in the front
} else {
// There is some other view on top of your view hierarchy,
// which resulted in losing the focus of window
}
}
});