带有数据绑定的AndroidX SwipeRefreshLayout

问题描述:

我正在尝试使用AndroidX库实现滑动刷新功能: androidx.swiperefreshlayout.widget.SwipeRefreshLayout

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

我的应用程序正在使用Android数据绑定,因此我想使用可观察的字段来控制此小部件的状态.

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

过去,我曾经使用过 AppCompat一个 -至今已不推荐使用.

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>

但是,这似乎不再起作用-使用等效的AndroidX. 我想念什么吗?或是否有解决方案/解决方法来实现这一目标?

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?

我的项目已经完全迁移到AndroidX,没有错误或依赖项冲突.

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

这称为 androidx.swiperefreshlayout.widget.SwipeRefreshLayout ...

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

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

例如:

<?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>

然后onRefresh(),可以从数据绑定中获取RecyclerView.Adapter.

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