使用数据绑定时 SwipeRefreshLayout 进度不隐藏

SwipeRefreshLayout progress not hiding when using data binding

之后,我正在尝试使用数据绑定来绑定 SwipeRefreshLayout,但 SwipeRefreshLayout 的进度并未隐藏。 下面是我正在使用的代码:

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);
    }
}

布局

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

在上面的代码中,变量 isLoadingtrue 更改为 false 但进度仍然可见。谁能指出我做错了什么?

我认为你应该尝试通过 SwipeRefreshLayout.post() 方法更新 SwipeRefreshLayout:

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

或者您可以尝试创建一个从 SwipeRefreshLayout 扩展的自定义视图,如下所示:

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);
    }
}