【问题标题】:AndroidX SwipeRefreshLayout with DataBinding带有数据绑定的 AndroidX SwipeRefreshLayout
【发布时间】:2020-01-03 11:37:33
【问题描述】:

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

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

过去我使用过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,没有错误或冲突的依赖项。

【问题讨论】:

  • 您在使用 android x 相当于 swiperefreshlayout 时遇到什么问题?
  • @Kushal 我无权访问应用程序:在 SwipeRefreshLayout 中刷新。

标签: android data-binding android-databinding androidx swiperefreshlayout


【解决方案1】:

那叫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 文件中正确自动完成它,我错过了它......
【解决方案2】:

【讨论】:

  • 是的,我认为自定义绑定适配器会在这种情况下解决它。其实我想我以前用过 AppCompat。
猜你喜欢
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 2021-06-27
相关资源
最近更新 更多