AndroidX SwipeRefreshLayout与DataBinding

huangapple go评论115阅读模式
英文:

AndroidX SwipeRefreshLayout with DataBinding

问题

  1. 我正试图使用AndroidX库实现下拉刷新功能:**androidx.swiperefreshlayout.widget.SwipeRefreshLayout**
  2. 由于我的应用正在使用Android Databinding,因此我想使用可观察字段来控制此小部件的状态。
  3. 在过去,我曾使用[AppCompat版本][1] - 但它已经被弃用了。
  4. 以前,我可以以以下方式访问字段:
  5. <android.support.v4.widget.SwipeRefreshLayout
  6. xmlns:app="http://schemas.android.com/apk/res-auto"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. app:refreshing="@{viewModel.isLoading}">
  10. ...
  11. // 这里是我的RecyclerView
  12. </android.support.v4.widget.SwipeRefreshLayout>
  13. 但是,使用AndroidX的等效部分似乎不再起作用。
  14. 我是否漏掉了什么?或者是否有任何解决方案/变通方法可以实现此功能?
  15. 我的项目已经完全迁移到了AndroidX,没有错误或冲突的依赖项。
  16. [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:

  1. <android.support.v4.widget.SwipeRefreshLayout
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. app:refreshing="@{viewModel.isLoading}">
  6. ...
  7. // My RecyclerView here
  8. </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...

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

例如:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools">
  6. <data />
  7. <androidx.appcompat.widget.LinearLayoutCompat
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:orientation="vertical">
  11. <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
  12. android:id="@+id/swipe_container"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. app:refreshing="@{viewModel.isLoading}"
  16. app:onRefreshListener="@{() -> viewModel.onRefresh()}">
  17. ...
  18. </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
  19. </androidx.appcompat.widget.LinearLayoutCompat>
  20. </layout>

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

英文:

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

  1. // https://mvnrepository.com/artifact/androidx.swiperefreshlayout/swiperefreshlayout
  2. implementation &quot;androidx.swiperefreshlayout:swiperefreshlayout:1.0.0&quot;

For example:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;layout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  5. xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
  6. &lt;data /&gt;
  7. &lt;androidx.appcompat.widget.LinearLayoutCompat
  8. android:layout_width=&quot;match_parent&quot;
  9. android:layout_height=&quot;match_parent&quot;
  10. android:orientation=&quot;vertical&quot;&gt;
  11. &lt;androidx.swiperefreshlayout.widget.SwipeRefreshLayout
  12. android:id=&quot;@+id/swipe_container&quot;
  13. android:layout_width=&quot;match_parent&quot;
  14. android:layout_height=&quot;match_parent&quot;
  15. app:refreshing=&quot;@{viewModel.isLoading}&quot;
  16. app:onRefreshListener=&quot;@{() -&gt; viewModel.onRefresh()}&quot;&gt;
  17. ...
  18. &lt;/androidx.swiperefreshlayout.widget.SwipeRefreshLayout&gt;
  19. &lt;/androidx.appcompat.widget.LinearLayoutCompat&gt;
  20. &lt;/layout&gt;

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

huangapple
  • 本文由 发表于 2020年1月3日 19:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定