使用DataBinding的AndroidX SwipeRefreshLayout

6
我将使用AndroidX库的androidx.swiperefreshlayout.widget.SwipeRefreshLayout来实现下拉刷新功能。我的应用程序使用Android Databinding,因此我希望使用可观察字段来控制此小部件的状态。
以前,我使用了AppCompat one,但它已经被废弃了。
在过去,我可以通过以下方式访问字段:
<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 中不再起作用了。 我错过了什么吗?或者有什么解决方法/解决方案可以实现这一点吗?

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


你在使用AndroidX中的swiperefreshlayout等价组件时遇到了什么问题? - Kushal
@ Kushal我在SwipeRefreshLayout中没有访问app:refreshing的权限。 - Jan Slominski
2个回答

18

那被称为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


SwipeRefreshLayout暴露了setRefreshing()方法,但Android Studio在布局XML文件中没有正确自动完成它,我错过了它... - Jan Slominski

1

是的,我认为自定义绑定适配器在这种情况下会解决问题。实际上,我之前在AppCompat中使用过一个。 - Jan Slominski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接