SwipeRefreshLayout progress not hiding when using data binding

Viewed 1617

Following How to set SwipeRefreshLayout refreshing property using android data binding? I'm trying to bind SwipeRefreshLayout using data binding but SwipeRefreshLayout's progress is not getting hide. Below is my code that i'm using it:

ViewModel

public class StateViewModel extends BaseViewModel {
 public MutableLiveData<Boolean> isLoading; 

 public void getItemStateDetails() {
    isLoading.setValue(true);
    scanTypePosition.setValue(0);
    getCompositeDisposable().add(
            getRepositoryManager().getItemStates()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this::onSuccess, this::onError));
} 

private void onError(Throwable throwable) {
    isLoading.setValue(false);
    errorMsg.setValue(throwable.getMessage());
}

  private void onSuccess(List<State> states) {
        isLoading.setValue(false);
        lstState.setValue(states);
    }
}

Layout

     <variable
            name="stateModel"
            type="story.stateupdate.stateselection.StateViewModel" />

     <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                apps:cardUseCompatPadding="true">

                <android.support.v4.widget.SwipeRefreshLayout
                    android:id="@+id/pullToRefresh"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:onRefreshListener="@{() -> stateModel.getItemStateDetails()}"
                    app:refreshing="@{stateModel.isLoading}">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/stateRecyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </android.support.v4.widget.SwipeRefreshLayout>
            </android.support.v7.widget.CardView>

In the above code, the variable isLoading is getting changed from true to false but the progress is still visible. Can anyone point out what mistake I'm doing?

2 Answers

I think you should try to update SwipeRefreshLayout through the SwipeRefreshLayout.post() method:

SwipeRefreshLayout.post(new Runnable() {
       @Override
       public void run() {
            SwipeRefreshLayout.setRefreshing(isLoading);
       }
});

Or you can try to create a custom view that extend from SwipeRefreshLayout as below:

public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {
    public CustomSwipeRefreshLayout(@NonNull Context context) {
        super(context);
    }

    public CustomSwipeRefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setRefreshing(final boolean refreshing) {
        post(new Runnable() {
            @Override
            public void run() {
                CustomSwipeRefreshLayout.super.setRefreshing(refreshing);
            }
        });
    }
}
public class StateViewModel extends BaseViewModel implements SwipeRefreshLayout.OnRefreshListener {
 SwipeRefreshLayout swipeRefreshLayout;

 public void getItemStateDetails() {
    swipeRefreshLayout.setRefreshing ( true );
    scanTypePosition.setValue(0);
    getCompositeDisposable().add(
            getRepositoryManager().getItemStates()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this::onSuccess, this::onError));
} 

private void onError(Throwable throwable) {
    swipeRefreshLayout.setRefreshing ( false);
    errorMsg.setValue(throwable.getMessage());
}

  private void onSuccess(List<State> states) {
        swipeRefreshLayout.setRefreshing ( false);
        lstState.setValue(states);
    }
}
Related