Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 556
Jan Slominski Avatar asked Jan 03 '20 11:01

Jan Slominski


2 Answers

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.

like image 157
Martin Zeitler Avatar answered Nov 18 '22 06:11

Martin Zeitler


You can create your own BindingAdapter for that

https://developer.android.com/topic/libraries/data-binding/binding-adapters

like image 1
thehrlein Avatar answered Nov 18 '22 08:11

thehrlein