T. Boubaker
T. Boubaker

Reputation: 50

The addOnScrollListener for my RecyclerView not working as expected

I want to know when I am at the bottom of my recycler view but I can't get why the recyclerView.canScrollVertically(1) is always returning true even if I am at the bottom of the Recycler View.

The recycler view is inside a CoordinatorLayout with an AppBarLayout.

You have the Kotlin code and the XML code for the bigger picture

Thank you.

enter image description here

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>

Upvotes: 1

Views: 615

Answers (1)

T. Boubaker
T. Boubaker

Reputation: 50

Thank you for your response, i did find a solution by adding to my recycler view in xml
addOnScrolledToBottomListener="@{viewModel}" and then in the 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

Upvotes: 1

Related Questions