【问题标题】:How to observe ViewModel LiveData in a DialogFragment?如何在 DialogFragment 中观察 ViewModel LiveData?
【发布时间】:2019-11-24 17:49:32
【问题描述】:

我想在片段和对话框片段之间共享一个 ViewModel。在启动 DialogFragment 之前,我使用 setter 方法更新 ViewModel liveData。当我尝试观察 DialogFragment 中的值时,该值为空。我尝试通过捆绑包的 parcelable 发送值,并且成功了。

这就是我从片段中调用的方式,

myViewModel.setBitmap(myResult.getBitmap());
            BottomSheetFragment bottomSheetFragment = BottomSheetFragment.getNewInstance(args);
            bottomSheetFragment.setTargetFragment(this, EDIT_FROM_OVERLAY);
            bottomSheetFragment.setListener(this);
            bottomSheetFragment.show(fragmentManager, BottomSheetFragment.TAG);

对话框片段:

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        myViewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
        myViewModel.getBitMap().observe(this, bitmap ->
        {
            dialogBitmap = bitmap;
        });

        imageView = view.findViewById(R.id.overlay_image);
        imageView.setImageBitmap(dialogBitmap);
    }

我也尝试在 onCreateDialog 方法中初始化 ViewModel。结果还是一样。我想通过 ViewModel 从片段向 dialogFragment 发送位图。我在这里想念什么?为什么我在 dialogFragment 中的 fragment 中设置的位图图像无法获取?任何指针都会有所帮助。

谢谢

更新: 添加视图模型代码,

public class MyViewModel extends AndroidViewModel
{
    private final MutableLiveData<Bitmap> bitmap = new MutableLiveData<>();

    public BitmapViewModel(@NonNull Application application)
    {
        super(application);
    }

    public LiveData<Bitmap> getBitmap()
    {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap)
    {
        bitmap.setValue(bitmap);
    }

    public void clear()
    {
        bitmap.setValue(null);
    }
}

【问题讨论】:

    标签: android android-fragments android-dialogfragment


    【解决方案1】:

    这是因为您在创建 dialogFragment 之前设置了 liveData 值。

    这样做 myViewModel.setBitmap(myResult.getBitmap());bottomSheetFragment.show(fragmentManager, BottomSheetFragment.TAG);

    【讨论】:

    • 感谢您的指点。我试过了,没有太大区别。但是你所说的让我意识到我可能没有在正确的地方设置值。所以我移动了``` imageView = view.findViewById(R.id.overlay_image); imageView.setImageBitmap(dialogBitmap); ``` 里面观察 lambda 表达式,它起作用了。
    • 是的,你是对的,注意你只需要将 setImageBitmap 移动到观察者 lambda 中。不是findviewById
    【解决方案2】:

    @Ali Rezaiyan 的建议让我意识到我没有在片段中设置价值。 所以我在对话框片段中的观察中移动了设置位图。

    bitmapViewModel.getBitmap().observe(this, bitmap ->
            {
                imageView.setImageBitmap(bitmap);
            });
    

    在此处添加以供将来参考。

    【讨论】:

      【解决方案3】:

      你这样做是错误的。使用您的方法,当您执行imageView.setImageBitmap(dialogBitmap) 时,bitmap/dialogBitmap 的值将始终为空。

      更好的方法是将 setImageBitmap 调用放在 LiveData 观察调用中。此外,请记住始终检查 null。这是一个代码 sn-p 说明了我的意思:

      // initialize your imageView
      imageView = view.findViewById(R.id.overlay_image);
      
      // observe your data
      myViewModel.getBitMap().observe(this, bitmap ->
          {
              // check for null and set your imageBitmap accordingly
              if(bitmap != null) imageView.setImageBitmap(bitmap);
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-10
        • 1970-01-01
        • 1970-01-01
        • 2020-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多