How to set SwipeRefreshLayout to update properties using Android data binding?

I am using Android data binding library. If I want to make the 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}" />

Can I associate the refresh swipeRefreshLayout property with a similar (xml) method?

I am currently installing it in code by calling setRefreshing (true / false), but would like to make it consistent in xml.

+4
source share
4 answers

You can achieve this with a little hack (I think): create your own attribute refreshing, and then just bind it to booleans.

, attrs.xml res/values, , :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SwipeRefreshLayout">
        <attr name="refreshing" format="boolean"/>
    </declare-styleable>
</resources>

:

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

! , @{true} @{false} @{habitListViewModel.isLoading}. , .

+4

. SwipeRefreshLayout. Databinding set. . :

OnRefreshListener

OnRefreshListener - , , , :

app:onRefreshListener="@{() -> viewModel.onRefresh()}"

, :

app:refreshing="@{viewModel.isLoading}"

, :

<data>
    <variable name="viewModel" type="ViewModel" />
</data>
<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:refreshing="@{viewModel.isLoading}"
    app:onRefreshListener="@{() -> viewModel.onRefresh()}">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"          
        android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>

ViewModel:

public class ViewModel implements DataProvider.Callback {
    public ObservableBoolean isLoading = new ObservableBoolean();
    private DataProvider provider;

    MasterViewModel(@NonNull final DataProvider provider) {
        this.provider = provider;
    }

    /* Needs to be public for Databinding */
    public void onRefresh() {
        isLoading.set(true);
        provider.fetch(this);
    }

    public void onReady(List results){
        isLoading.set(false);
    } 

    public void onError(Exception oops){
        isLoading.set(false);
        Log.e("Stack", oops);
    }
}
+13

, , .

@Entreco , attrs.xml.

, , ViewModel ObservableField<Boolean>.

ObservableBoolean ( Entreco - ), . , .

0

app:refreshing="@{booleanValueHere}". isRefreshing.

-1
source

Source: https://habr.com/ru/post/1662774/


All Articles