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 ?

4
  • Can you add the swipe to delete function as well Commented Jul 21, 2019 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 ? Commented Jul 21, 2019 at 13:44
  • Pls advise what is the purpose of knowing NestedScrollView is scrollable? It sounds confusing. Commented Jul 21, 2019 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 Commented Jul 21, 2019 at 13:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, where should i call it ? (Since in onCreate, getHeight() will return 0)
I would suggest calling it, for example, after your RecyclerView change happens (e.g., item removal or addition)
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 ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.