Search code examples
androiddata-bindingandroid-databindingandroidxswiperefreshlayout

AndroidX SwipeRefreshLayout with DataBinding


I'm trying to implement swipe to refresh functionality using AndroidX library: androidx.swiperefreshlayout.widget.SwipeRefreshLayout

My app is using Android Databinding therefore I'd like to use observable fields to control the state of this widget.

In the past I've used AppCompat one - it has as since been deprecated.

Before, I could access the fields in the following way:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:refreshing="@{viewModel.isLoading}">
    ...
    // My RecyclerView here
</android.support.v4.widget.SwipeRefreshLayout>

But, this doesn't seem to work anymore - with the AndroidX equivalent. Am I missing something? or is there any solution/workaround to achieve this?

My project has already been fully migrated to AndroidX, there are no errors or conflicting dependencies.


Solution

  • That's called androidx.swiperefreshlayout.widget.SwipeRefreshLayout ...

    // https://mvnrepository.com/artifact/androidx.swiperefreshlayout/swiperefreshlayout
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
    

    For example:

    <?xml version="1.0" encoding="utf-8"?>
    <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 />
    
        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/swipe_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:refreshing="@{viewModel.isLoading}"
                app:onRefreshListener="@{() -> viewModel.onRefresh()}">
                ...
            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    
        </androidx.appcompat.widget.LinearLayoutCompat>
    
    </layout>
    

    Then onRefresh(), one can get the RecyclerView.Adapter from the data-binding.