【问题标题】:How can a BottomSheetDialogFragment communicate with its host fragment?BottomSheetDialogFragment 如何与其宿主片段通信?
【发布时间】:2021-04-24 00:32:50
【问题描述】:

我的片段中有一个按钮,可以打开一个BottomSheetDialogFragment。如果用户在 BottomSheetDialogFragment 上选择了一个项目,我想通知主机片段。为了实现这一点,我在我的 BottomSheetDialogFragment 中做了一个接口。但是,该接口仅与宿主活动通信,而不与片段通信。如何将对话框中的信息发送到片段?

这是我的界面:

public interface BottomSheetListener {
        void onButtonClicked(int index);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        try {
            mListener = (BottomSheetListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement BottomSheetListener");
        }
    }

【问题讨论】:

    标签: android android-fragments android-bottomsheetdialog bottomsheetdialogfragment


    【解决方案1】:

    getParentFragment 将返回父片段,如果当前片段附加到片段,否则如果它直接附加到活动,则返回 null

    @Override
        public void onAttach(@NonNull Context context) {
            super.onAttach(context);
            try {
                mListener = (BottomSheetListener) getParentFragment();
            } catch (ClassCastException e) {
                throw new ClassCastException(context.toString() + " must implement BottomSheetListener");
            }
        }
    

    【讨论】:

      【解决方案2】:

      当您使用大量片段、嵌套片段或对话片段时,它们之间的通信会变得混乱。我建议使用 ViewModel 和 LiveData 来传递和更新数据。

      首先添加这个来构建 gradle:

      implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
      

      然后创建 ViewModel 类:

      公共类 YourViewModel 扩展 AndroidViewModel {

      private MutableLiveData<Integer> yourMutableLiveData=new MutableLiveData<>();
      
      public YourViewModel(@NonNull Application application) {
          super(application);
      }
      
      public MutableLiveData<Integer> getYourMutableLiveData() {
          return yourMutableLiveData;
      }
      

      }

      这是你想要设置值的片段:

         public class FragmentA extends Fragment{
      
              @Override
              public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                  super.onActivityCreated(savedInstanceState);
      
                  YourViewModel yourViewModel =new ViewModelProvider(getActivity()).get(YourViewModel.class);
      
                  yourViewModel.getYourMutableLiveData().setValue(0);
              }
          }
      

      这是您希望在更新时获得价值的片段:

       public class FragmentB extends Fragment{
      
              @Override
              public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                  super.onActivityCreated(savedInstanceState);
      
                  YourViewModel yourViewModel =new ViewModelProvider(getActivity()).get(YourViewModel.class);
      
                  yourViewModel.getYourMutableLiveData().observe(getViewLifecycleOwner(), new Observer<Integer>() {
                      @Override
                      public void onChanged(Integer integer) {
      
                      }
                  });
              }
          }
      

      它可以像我测试的那样在对话框片段上工作。

      注意事项: - 不要将上下文或任何视图传递到视图模型中。

      -记住 onActivityCreated 在 onCreateView 之后。

      -不要将此键设置为

       YourViewModel yourViewModel =new ViewModelProvider(this).get(YourViewModel.class);
      

      如果您想将数据片段传递给片段,则在片段中,但您可以在活动中传递。

      -您可以为数据设置多个观察者。

      【讨论】:

      • 非常感谢!
      • 如果有任何问题请告诉我。处理 ViewModel 在您第一次尝试时可能会遇到问题,但是当您习惯它时它会非常容易使用,并且绝对值得您费心。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多