滚动时仅在首次加载时调用,而不会再次调用

时间:2019-07-20 02:54:40

标签: android kotlin android-recyclerview infinite-scroll onscrolllistener

我正在尝试实现无限滚动,但正如我从日志中看到的那样,我无法使其工作,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

2 个答案:

答案 0 :(得分:0)

您可以尝试此代码

listview.addOnScrollListener(new RecyclerView.OnScrollListener() {

            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);


                visibleItemCount = manager.getChildCount();
                totalItemCount = manager.getItemCount();
                pastVisiblesItems = manager.findFirstVisibleItemPosition();

                if (dy > 0 && pastVisiblesItems >= totalItemCount && !isLoading) {
                    isLoading = true;
                    LoadMoreData();
                }

            }

        });

使用此代码,您可以滚动到列表末尾并加载新信息 如果您需要从项目10到列表末尾的信息,请加载

更改

if (dy > 0 && pastVisiblesItems >= totalItemCount && !isLoading) {
   isLoading = true;
   LoadMoreData();
}

if (dy > 0 && pastVisiblesItems >= totalItemCount - 10 && !isLoading) {
   isLoading = true;
   LoadMoreData();
}

答案 1 :(得分:0)

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

就我而言,问题是我忘记将 RecyclerView 放入 NestedScrollView 并将其设置为 android:nestedScrollingEnabled="false",这确实使侦听器仅触发一次。然后,您可以将侦听器添加到 NestedScrollView,或者查看以下问题中讨论的某些选项是否可以帮助您: Recyclerview onscrolllistener not working when setNestedScrollingEnabled to false

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