Search code examples
androiddata-bindingswiperefreshlayoutandroid-layout

How to set SwipeRefreshLayout refreshing property using android data binding?


I am using android data binding library. If I want to make a view visible I can write something like this:

<TextView
            android:id="@+id/label_status"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@{habitListViewModel.message}"
            app:visibility="@{habitListViewModel.hasError ? View.VISIBLE : View.GONE}" />

Is there an option to bind to a refresh property of swipeRefreshLayout in a similar (xml) way?

Currently I am setting it in code by calling setRefreshing(true/false) but would love to make it in xml to be consistent.


Solution

  • UPDATED: As databinding maps from xml attribute name to set{AttributeName}, you can just use app:refreshing, as databinding will successfully supply the value to setRefreshing method of SwipeRefreshLayout (which luckily for us exists and is public):

    <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="@{habitListViewModel.isLoading}">
        ...
        //ListView,RecyclerView or something
    </android.support.v4.widget.SwipeRefreshLayout>
    

    That's it! Note, that you can simply use @{true} or @{false} instead of @{habitListViewModel.isLoading}. Hope that helps.