首页 > 解决方案 > On scrolled 仅在第一次加载时调用,不再调用

问题描述

我正在尝试实现无限滚动,但我无法让它工作,正如我从我放置的日志中看到的那样,该onScrolled方法仅在页面首次加载时调用一次,而不是之后,即使我滚动所有到底部的方式。我尝试上下多次,但它再也没有被调用过。

它是否与它在 swipeRefreshLayout 中有关?

这是我的代码

启动变量

lateinit var lastVisible: DocumentSnapshot
var isScrolling = false
var isLastItemReached = false

从服务器函数中获取数据

fun listenToQuestions() {

    questionsRowLayoutAdapter.clear()
    questionsBlockLayoutAdapter.clear()



    db.collection("questions").whereEqualTo("language", currentLanguage)
        .orderBy("last_interaction", Query.Direction.DESCENDING).limit(10)
        .get().addOnSuccessListener {

            for (document in it) {
                val questionObject = document.toObject(Question::class.java)

                           questionsAdapter.add(
                                (SingleBoardBlock(
                                    questionObject,
                                    activity as MainActivity
                                ))
                            )

            }
            Toast.makeText(this.context, "first batch", Toast.LENGTH_SHORT).show()

            questionsAdapter.notifyDataSetChanged()

            lastVisible = it.documents[it.size() - 1]

            val onScrollListener = object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                    if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                        isScrolling = true
                        Toast.makeText(activity, "scrolling", Toast.LENGTH_SHORT).show()
                        Log.d("itemsvaues", "new state statement true")
                    } else {
                        Log.d("itemsvaues", "new state statement false")
                    }
                }

                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)

                    val thisLayoutManager = recyclerView.layoutManager as LinearLayoutManager

                    val firstVisibleItem = thisLayoutManager.findFirstVisibleItemPosition()
                    val visibleItemCount = thisLayoutManager.childCount
                    val totalItemCount = thisLayoutManager.itemCount

                    val countCheck = firstVisibleItem + visibleItemCount == totalItemCount
                    Log.d("itemsvaues", "$firstVisibleItem $visibleItemCount $totalItemCount $countCheck")
                    Log.d("itemsvaues", "is scrolling $isScrolling")
                    Log.d("itemsvaues", "last item reached $isLastItemReached")


                    if (isScrolling && countCheck && !isLastItemReached) {
                        isScrolling = false
                        Log.d("itemsvaues", "if happened")

                        db.collection("questions").whereEqualTo("language", currentLanguage)
                            .orderBy("last_interaction", Query.Direction.DESCENDING).startAfter(lastVisible)
                            .limit(10).get()
                            .addOnSuccessListener { querySnapshot ->

                                for (document in querySnapshot) {
                                    val questionObject = document.toObject(Question::class.java)

                           questionsAdapter.add(
                                (SingleBoardBlock(
                                    questionObject,
                                    activity as MainActivity
                                ))
                            )

                                    }
                                }
                                Toast.makeText(activity, "next batch", Toast.LENGTH_SHORT).show()

                                questionsAdapter.notifyDataSetChanged()

                                lastVisible = querySnapshot.documents[querySnapshot.size() - 1]
                                Log.d("itemsvaues", "next query happened")

                                if (querySnapshot.size() < 10) {
                                    isLastItemReached = true
                                    Toast.makeText(activity, "reached last", Toast.LENGTH_SHORT).show()
                                    Log.d("itemsvaues", "last item reached")

                                }
                            }
                    } else {
                        Log.d("itemsvaues", "if didn't happen")
                    }
                }
            }
            questionsRecycler.addOnScrollListener(onScrollListener)
        }
}

这些是日志:

2019-07-20 12:48:49.149 29888-29888/io.poolclub D/itemsvaues: 0 10 10 true
2019-07-20 12:48:49.149 29888-29888/io.poolclub D/itemsvaues: is scrolling false
2019-07-20 12:48:49.149 29888-29888/io.poolclub D/itemsvaues: last item reached false
2019-07-20 12:48:49.149 29888-29888/io.poolclub D/itemsvaues: if didn't happen
2019-07-20 12:48:52.181 29888-29888/io.poolclub D/itemsvaues: new state statement true
2019-07-20 12:48:52.328 29888-29888/io.poolclub D/itemsvaues: new state statement false
2019-07-20 12:48:52.530 29888-29888/io.poolclub D/itemsvaues: new state statement false
2019-07-20 12:48:53.481 29888-29888/io.poolclub D/itemsvaues: new state statement true
2019-07-20 12:48:53.645 29888-29888/io.poolclub D/itemsvaues: new state statement false

标签: androidkotlinandroid-recyclerviewinfinite-scrollonscrolllistener

解决方案


可能对原始海报不再有用,但对遇到相同问题的其他人来说:

在我的情况下,问题是我忘记了我已经把RecyclerView里面的 aNestedScrollView设置为android:nestedScrollingEnabled="false",这确实让监听器只触发一次。您可以将侦听器添加到NestedScrollViewthen,或者查看以下问题中讨论的某些选项是否可以帮助您: Recyclerview onscrolllistener not working when setNestedScrollingEnabled to false

当然可能还有其他原因,但这是最容易遇到的原因之一。


推荐阅读