检查NestedScrollView是否可滚动

时间:2019-07-21 13:34:47

标签: android kotlin android-recyclerview android-nestedscrollview android-scroll

我需要一个函数,如果我的屏幕在NestedScrollView中可滚动,则返回一个布尔值。我目前在NestedScrollView中有一个RecyclerView,并且RecyclerView的内容是动态的(我可以添加或删除项目)。每次我滑动以删除时,我都想调用检查NestedScrollView是否可滚动的函数。我目前正在使用此功能:

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

这有效,但有缺陷。在删除RecyclerView中的最后一个内容(使屏幕无法滚动)后,该函数仍返回true。但是,此后,我执行的每个删除操作都会返回false。但这不是很好,当屏幕无法滚动时,我需要它返回false。你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

我认为您应该能够将此答案check if an android scrollview can scroll调整为您的需要(通过将NestedScrollView替换为ScrollView)。当然,只有在渲染视图后才能使用。