【问题标题】:The addOnScrollListener for my RecyclerView not working as expected我的 RecyclerView 的 addOnScrollListener 没有按预期工作
【发布时间】:2021-11-07 13:24:34
【问题描述】:

我想知道我何时位于回收站视图的底部,但我不明白为什么即使我位于回收站视图的底部,recyclerView.canScrollVertically(1) 总是返回 true。

回收器视图位于带有 AppBarLayout 的 CoordinatorLayout 内。

您拥有 Kotlin 代码和 XML 代码,可以更全面地了解全局

谢谢。

getDataBinding().shipmentRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        super.onScrollStateChanged(recyclerView, newState)
        Log.e(TAG,
            "onScrollStateChanged 1: This is the bottom shipmentRecyclerView $newState <> ${RecyclerView.SCROLL_STATE_IDLE} ${
                recyclerView.canScrollVertically(1)
            }")
        if (!getDataBinding().shipmentRecyclerView.canScrollVertically(1) && newState == RecyclerView.SCROLL_STATE_IDLE) {
            Log.e(TAG, "onScrollStateChanged 2: This is the bottom shipmentRecyclerView")
            if (viewModel.currentPage.get()!! < viewModel.totalPages.get()!!) {
                viewModel.getShipmentByPage(
                    viewModel.currentPage.get()!! + 1,
                    viewModel.request.get()
                )
            }
        }
    }
})
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<layout 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">

    <data>

        <import type="android.view.View" />

        <variable
            name="viewModel"
            type="com.technifyit.jibheli.presentation.main.fragment.search.SearchViewModel" />
    </data>

    <com.sothree.slidinguppanel.SlidingUpPanelLayout
        android:id="@+id/sliding_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        app:umanoPanelHeight="0dp"
        app:umanoShadowHeight="@dimen/margin_4dp"
        tools:context=".presentation.main.fragment.search.SearchFragment">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/shipment_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="@dimen/margin_12dp"
                android:clipToPadding="true"
                android:fastScrollEnabled="true"
                app:layout_anchor="@id/app_bar_layout"
                app:layout_anchorGravity="bottom"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:shipment_adapter="@{viewModel}" />

        


            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/dark_color_purple"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true">
                    ...........
                </com.google.android.material.appbar.CollapsingToolbarLayout>

            </com.google.android.material.appbar.AppBarLayout>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/dark_color_purple"
                android:paddingLeft="0dp"
                android:paddingRight="0dp"
                android:visibility="gone"
                app:contentInsetStart="0dp"
                app:layout_anchor="@id/app_bar_layout"
                app:layout_anchorGravity="bottom"
                app:layout_collapseMode="pin"
                app:theme="@style/ThemeOverlay.AppCompat.Dark"
                app:title="">

.........

          </androidx.appcompat.widget.Toolbar>


        </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/custom_web_page_browser"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/margin_40dp"
            android:background="@drawable/background_rounded_top"
            android:backgroundTint="@color/grey_clair">

            <com.technifyit.jibheli.presentation.customView.MontserratBoldTextView
                android:id="@+id/url_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_20dp"
                android:layout_marginEnd="@dimen/margin_6dp"
                android:text="@string/activity_item_details_send_request_button_text"
                android:textColor="@color/dark_color_purple"
                android:textSize="@dimen/title_size_sign_up"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@id/close"
                app:layout_constraintEnd_toStartOf="@id/use_link_image_view"
                app:layout_constraintStart_toEndOf="@id/close"
                app:layout_constraintTop_toTopOf="@id/close" />

            <ImageView
                android:id="@+id/close"
                android:layout_width="@dimen/icon_24dp"
                android:layout_height="@dimen/icon_24dp"
                android:layout_margin="@dimen/margin_6dp"
                android:background="@drawable/ic_close_gold"
                android:backgroundTint="@color/dark_color_purple"
                android:padding="@dimen/margin_4dp"
                android:src="@drawable/ic_close_native"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:tint="@color/white" />

            <com.technifyit.jibheli.presentation.customView.MontserratBoldTextView
                android:id="@+id/use_link_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_6dp"
                android:background="@drawable/button_rounded_left_only_no_icon"
                android:backgroundTint="@color/dark_color_purple"
                android:drawableEnd="@drawable/ic_check_native"
                android:drawablePadding="@dimen/margin_6dp"
                android:gravity="center"
                android:paddingStart="@dimen/margin_8dp"
                android:paddingEnd="@dimen/margin_8dp"
                android:text="@string/select"
                android:textAllCaps="false"
                android:textColor="@color/white"
                app:drawableTint="@color/white"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="@dimen/margin_4dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@id/close">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <include
                        android:id="@+id/no_item_found_suggested_layout"
                        layout="@layout/no_item_found_layout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/requested_shipments_recycler_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/margin_6dp"
                        android:fastScrollEnabled="true"
                        app:layout_constraintTop_toTopOf="parent"
                        app:requested_shipment_adapter="@{viewModel}" />

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/requested_trip_recycler_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/margin_6dp"
                        android:fastScrollEnabled="true"
                        app:layout_constraintTop_toTopOf="parent"
                        app:requested_trip_adapter="@{viewModel}" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </ScrollView>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.sothree.slidinguppanel.SlidingUpPanelLayout>
</layout>

【问题讨论】:

  • 你能提供完整的布局吗?我看到 &lt;/ScrollView&gt; 但没有开始标签。
  • 我认为主要问题是ScrollView 内部有RecyclerView,它不是RecyclerView 滚动,而是ScrollView
  • 由于超过了 30000 字符,我无法放入所有代码,但我尝试将其缩小一点并立即更新,谢谢。
  • 我可以看出你是在一个活动中运行它,但我是在一个片段中运行它,这是我看到的唯一区别,我认为这不会导致问题一个听众,这很奇怪
  • 感谢您的分析,我确实找到了完成工作的其他解决方案,所以也许对以后的任何人都有帮助,谢谢 xinaiz

标签: kotlin android-recyclerview onscrolllistener


【解决方案1】:

感谢您的回复,我确实通过在 xml
addOnScrolledToBottomListener="@{viewModel}" 然后在 bindingAdapter 中添加到我的回收站视图中找到了解决方案

        @BindingAdapter("addOnScrolledToBottomListener")
        fun addOnScrolledToBottomListener(recyclerView: RecyclerView, viewModel: SearchViewModel) {
            recyclerView.addOnScrollListener(object :
                RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                    if (recyclerView.canScrollVertically(1) && newState == RecyclerView.SCROLL_STATE_IDLE) {
                        if (viewModel.currentPage.get()!! < viewModel.totalPages.get()!!) {
                            viewModel.getShipmentByPage(
                                viewModel.currentPage.get()!! + 1,
                                viewModel.request.get()
                            )
                        }
                    }
                }
            })

        }`


and like that it worked like a charm, Thank you @xinaiz for your help

【讨论】:

    猜你喜欢
    • 2016-12-12
    • 2015-11-17
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    相关资源
    最近更新 更多