AndroidX SwipeRefreshLayout with DataBinding

久未见 提交于 2020-05-27 06:28:48

问题


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:


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:


You can create your own BindingAdapter for that

https://developer.android.com/topic/libraries/data-binding/binding-adapters



来源:https://stackoverflow.com/questions/59577951/androidx-swiperefreshlayout-with-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!