英文:
AndroidX SwipeRefreshLayout with DataBinding
问题
我正试图使用AndroidX库实现下拉刷新功能:**androidx.swiperefreshlayout.widget.SwipeRefreshLayout**
由于我的应用正在使用Android Databinding,因此我想使用可观察字段来控制此小部件的状态。
在过去,我曾使用[AppCompat版本][1] - 但它已经被弃用了。
以前,我可以以以下方式访问字段:
<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}">
...
// 这里是我的RecyclerView
</android.support.v4.widget.SwipeRefreshLayout>
但是,使用AndroidX的等效部分似乎不再起作用。
我是否漏掉了什么?或者是否有任何解决方案/变通方法可以实现此功能?
我的项目已经完全迁移到了AndroidX,没有错误或冲突的依赖项。
[1]: https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout
英文:
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.
答案1
得分: 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
。
英文:
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.
答案2
得分: 1
你可以为此创建自己的BindingAdapter。
https://developer.android.com/topic/libraries/data-binding/binding-adapters
英文:
You can create your own BindingAdapter for that
https://developer.android.com/topic/libraries/data-binding/binding-adapters
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论