3

I need a function which returns a boolean if my screen is scrollable inside a NestedScrollView. I currently have a RecyclerView inside a NestedScrollView and the content of the RecyclerView is dynamic (i can add or remove items). Everytime i swipe to delete i want to call the function which checks if the NestedScrollView is scrollable. I am currently using this function:

fun isRecyclerScrollable(binding: FragmentToBuyBinding): Boolean {
    val e = requireActivity().findViewById<NestedScrollView>(R.id.nested)
    return e.canScrollVertically(1) || e.canScrollVertically(-1)
}

This works but with a flaw. Upon deleting the last content in my RecyclerView which makes the screen not scrollable, the function still returns true. However, after that, every deletion i perform, it returns false. But this is not good and i need it to return false the moment my screen is not scrollable. Can you help ?

Mervin Hemaraju
  • 1,921
  • 2
  • 22
  • 71
  • Can you add the swipe to delete function as well – Anjana Jul 21 '19 at 13:40
  • The isScrollable function is called after completion of a coroutine. It is inside an Observable livedata, not the swipe function. Can you please help ? – Mervin Hemaraju Jul 21 '19 at 13:44
  • Pls advise what is the purpose of knowing NestedScrollView is scrollable? It sounds confusing. – ror Jul 21 '19 at 13:45
  • To hide/show a certain view. I just need to know if there is a better way to perform this type of function than my function above. Thank you – Mervin Hemaraju Jul 21 '19 at 13:49

1 Answers1

0

I think you should be able to adapt this answer check if an android scrollview can scroll to your needs (by replacing ScrollView with NestedScrollView). Surely this will only work once views are rendered.

ror
  • 3,295
  • 1
  • 19
  • 27
  • Thank you, where should i call it ? (Since in onCreate, getHeight() will return 0) – Mervin Hemaraju Jul 21 '19 at 14:53
  • I would suggest calling it, for example, after your RecyclerView change happens (e.g., item removal or addition) – ror Jul 21 '19 at 14:55
  • I know what's causing the problem. It is returning true on the first time because this function is running before the view of the recyclerview is being cleared. Is there a way to run this function after the recyclerview's view has been cleared ? – Mervin Hemaraju Jul 22 '19 at 14:43