AndroidX SwipeRefreshLayout与DataBinding

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

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 &quot;androidx.swiperefreshlayout:swiperefreshlayout:1.0.0&quot;

For example:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;layout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;

    &lt;data /&gt;

    &lt;androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:orientation=&quot;vertical&quot;&gt;

        &lt;androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id=&quot;@+id/swipe_container&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            app:refreshing=&quot;@{viewModel.isLoading}&quot;
            app:onRefreshListener=&quot;@{() -&gt; viewModel.onRefresh()}&quot;&gt;
            ...
        &lt;/androidx.swiperefreshlayout.widget.SwipeRefreshLayout&gt;

    &lt;/androidx.appcompat.widget.LinearLayoutCompat&gt;

&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:

确定