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

时间:2019-01-02 08:29:32

标签: android android-livedata swiperefreshlayout

How to set SwipeRefreshLayout refreshing property using android 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,但是进度仍然可见。谁能指出我在做什么错误?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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