【问题标题】:My ViewModel has null values when my View is loaded, how to delay loading of the View until the ViewModel populates the data?加载 View 时,我的 ViewModel 具有空值,如何延迟加载 View 直到 ViewModel 填充数据?
【发布时间】:2018-02-01 03:47:32
【问题描述】:

这是我的片段 xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>
    <variable
        name="viewModel"
        type="UserProfileViewModel" />
</data>

<ImageView
     android:id="@+id/image_user_avatar"
     app:userAvatarUrl="@{viewModel.currentUser.avatarUrl}" />

它有一个 viewModel 变量,它是下面的 Java 类:

public class UserProfileViewModel extends BaseViewModel {

private User mCurrentUser;

public void setSignedInUser(User signedInUser) {
    mCurrentUser = signedInUser;
}

public User getCurrentUser() { return mCurrentUser; }

在我的片段中:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mUserId = getArguments().getString(Strings.KEY_USER_ID);
    }
    mViewModel = UserProfileViewModel.getInstance();
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_user_profile, container, false);
    mBinding.setViewModel(mViewModel);
    mBinding.buttonSignOut.setOnClickListener((View view) -> signOutUser());
    mBinding.fragmentUserProfileSwipeRefreshContainer.setOnRefreshListener(this);
    return mBinding.getRoot();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getUserProfile();
}

public void getUserProfile() {
    GetUserProfileRequest request = new GetUserProfileRequest(mUserId);
    mGetUserProfileDisposable = mViewModel
            .getUserProfile(request)
            .doOnSubscribe(__ -> mBinding.fragmentUserProfileSwipeRefreshContainer.setRefreshing(true))
            .doOnError((Throwable ex) -> mBinding.fragmentUserProfileSwipeRefreshContainer.setRefreshing(false))
            .subscribe((Response<GetUserProfileResponseBody> response) -> {
                if (response.body() != null) {
// this user object has the avatarUrl field needed in the xml                       
                    mViewModel.setSignedInUser(response.body().getSignedInUser());
                }
            }, (Throwable ex) -> {
            });
}

到调用 setSignedInUser 时,Fragment 已经加载完毕,并且 XML 中的 viewModel 变量还没有被填充,所以 avatarUrl 为空,所以没有加载图像。如何防止这种情况发生?

【问题讨论】:

  • 必须在填充数据之前加载视图。您可以做的是隐藏视图,直到加载数据。隐藏视图 > 显示进度指示器 > 加载数据 > 隐藏进度指示器 > 显示视图

标签: android android-fragments mvvm


【解决方案1】:

试试这个

if (viewModel!= null) {

} else {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {

            }
        }

【讨论】:

  • 你能解释一下吗?
猜你喜欢
  • 2015-05-19
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
相关资源
最近更新 更多