I've defined the following code, so if an user is on top of the scrollview a swiperefreshlayout works.
recyclerView.getViewTreeObserver().addOnScrollChangedListener(ViewTreeObserver.OnScrollChangedListener {
if (recyclerView.scrollY == 0) {
binding.refreshed!!.isEnabled = true
} else {
binding.refreshed!!.isEnabled = false
}
})
But this code only reacts first time I move the recyclerview (which, by the way is in position 0 at start, and considers it to be at position 0 when move is performed), rest of the time the listener is never fired.
What may I be doing wrong?
PD: It's possible to achieve the wanted behavior with this code:
val layoutManager = LinearLayoutManager(this.context);
recyclerView.setLayoutManager(layoutManager);
recyclerView.getViewTreeObserver().addOnScrollChangedListener(ViewTreeObserver.OnScrollChangedListener {
if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
binding.refreshed!!.isEnabled = true
} else {
binding.refreshed!!.isEnabled = false
}
})
you can try this to get top position on recyclerview
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
if(layoutManager.findFirstCompletelyVisibleItemPosition()==0) {
binding.refreshed!!.isEnabled = true
}
else {
binding.refreshed!!.isEnabled = false
}