Recyclerview addOnScrollListener проблема - PullRequest
0 голосов
/ 06 октября 2018

Вот моя функция, которая обнаруживает завершение RecyclerView, которое будет использоваться при нумерации страниц позже.

private fun setOnScrollListener(categoryRecyclerView: RecyclerView, categoryPresenter: EntertainmentPresenter){
    categoryRecyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener(){

        override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            val totalItemCount:Int = categoryLayoutManager.itemCount
            val firstVisibleItemPosition:Int = categoryLayoutManager.findFirstVisibleItemPosition()
            val visibleItemCount:Int
            val lastVisibleItemPosition: Int = categoryLayoutManager.findLastVisibleItemPosition()

            visibleItemCount = lastVisibleItemPosition - firstVisibleItemPosition

            Log.e("q", lastVisibleItemPosition.toString())
            Log.e("q", dy.toString())

            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false
                    previousTotal = totalItemCount
                }
            }

            if (!loading && totalItemCount - visibleItemCount  <= firstVisibleItemPosition) {
                 Log.e("q", lastVisibleItemPosition.toString())
                loading = true
            }
        }
    })
}

Я использовал его раньше на RecyclerView, который имеет линейный LayoutManger, и все было замечательно.

Но когда я использовал его с RecyclerView, который имеет GridLayoutManager, все стало странно.

  1. Это реализовано только один раз и не работает, когда я прокручиваю.
  2. Последнее видимое возвращение позиции элемента totalItemCount-1.

Последнее: это я вызываю эту функцию в onCreate, и я попробовал ее с RecyclerView, которая имеет linear layout manager, и все было замечательно

Вот мой logcat,

2018-10-06 06:10:15.919 11033-11033/com.example.karem.moviesdatabase E/q: 0  
2018-10-06 06:10:15.919 11033-11033/com.example.karem.moviesdatabase E/q: 18

Он вызывается только один раз, и при прокрутке ничего не происходит.

1 Ответ

0 голосов
/ 06 октября 2018

высота моего RecyclerView была установлена ​​равной match_parent

, но после того, как я изменил высоту на 500dp, все работает хорошо

я не знаю почему, но в любом случае вот мой старый макет

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/layoutBackground"
tools:context=".fragments.AllItemsFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/categoryRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:listitem="@layout/entertainment_recycler_view_item" />

<ProgressBar
    android:id="@+id/loadingCategoryList"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@+id/categoryRecyclerView"
    app:layout_constraintEnd_toEndOf="@+id/categoryRecyclerView"
    app:layout_constraintStart_toStartOf="@+id/categoryRecyclerView"
    app:layout_constraintTop_toTopOf="@+id/categoryRecyclerView" />

</android.support.constraint.ConstraintLayout>

Я только изменяю высоту окна просмотра реселлера на 500dp, и это работает

отвечаю более странно, чем вопрос

...