【问题标题】:pass or listen data from BottomSheetDialogFragment to Fragment将数据从 BottomSheetDialogFragment 传递或侦听到 Fragment
【发布时间】:2018-02-16 19:08:24
【问题描述】:

我正在尝试从 BotomSheetDialogFragment 侦听或将数据传递到 Fragment 以更改 Fragment 上的某些内容(就像选择器一样)。

我尝试使用 getTargetFragment 实例化侦听器,但出现编译器错误 Found: 'MyFragment', required: 'android.support.v4.app.Fragment' less..

有什么想法还是我采取了错误的方法?

public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment implements View.OnClickListener {


ReportType reportType;

public interface OnChooseReasonListener {
    void onChooseReason(ReportType reportType);
}

OnChooseReasonListener listener;

@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View contentView = View.inflate(getContext(), R.layout.picker_bottom_sheet_, null);
    dialog.setContentView(contentView);
    CoordinatorLayout.LayoutParams layoutParams =
            (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();

    //get null here!!!:
    listener = (OnChooseReasonListener)  getParentFragment();// or with getTargetFragment();
  }

  @Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.cool_button:
            this.reportType = ReportType.ME;
            //trying to execute the lisstener on null
            listener.onChooseReason(this.reportType);
            dismiss();
            break;
    }
}}

还有片段:

    public class MyFragment extends Fragment 
  implements View.OnClickListener, 
  MyBottomSheetDialogFragment.OnChooseReasonListener {
//....code here
  public void showPicker() {
        //getting and compiler error Wrong 1st argument type. 
        // picker. setTargetFragment(MyFragment.this , 300);
         picker.show(fm, picker.getTag());
     }
   @Override
    public void onChooseReason(ReportType reportType) {
        //not getting here
        Log(TAG, "You choose something" + reportType.getValue());
    }
}

【问题讨论】:

    标签: android android-fragments listener


    【解决方案1】:

    您可以使用界面来解决此问题

    第一 创建接口类

    interface CustomInterfaceClass {
    
        public void callbackMethod(String date);
    }
    

    第二,Activityfragment中初始化接口类 正如我在片段类中使用的那样

     //interface for callback
        private CustomInterface callback;
    

    第三,确保您已在 onCreateViewOnCreate 方法中初始化了 callback 接口对象。

    //如果您在初始化时遇到错误,例如 this 关键字 未分配给回调方法,这意味着您没有实现 接口fragmentAclass。

    callback=this;
    

    第四, 不要忘记在 FragmentAClass 中实现 override 方法

    @Override
        public void callbackMethod(String date) {
            Toast.makeText(getContext(), "Yes"+date, Toast.LENGTH_SHORT).show();
        }
    

    第五, 现在转到 BottomSheetDialogFragmentFragmentBclass

    添加this等回调方法构造函数

      private CustomInterface callback;
    
        public Disconnect_fragment( CustomInterface callback) {
            this.callback=callback;
        }
        public Disconnect_fragment( ) {
    
        }
    

    最后现在您可以使用此方法传递值,并将在 FragmentAclass

    中接收
    callback.callbackMethod("your passing value");
    

    【讨论】:

      【解决方案2】:

      除了它不起作用之外,由于您将MyBottomSheetDialogFragment 与创建它的对象耦合,因此该代码有点味道。

      正确的方法是在MyBottomSheetDialogFragment 上创建一个方法void setOnChooseReasonListener(OnChooseReasonListener listener),并在创建实例时调用它。

      myBottomSheetDialogFragment.setOnChooseReasonListener(this);
      

      【讨论】:

      • 谢谢你是对的。我需要大量的经验和学习才能变得更好。并感谢您帮助我看到答案。我也想发布代码来实例化BottomSheet。这是我从片段中解决这个问题的方法:public void showPicker() { MyBottomSheetDialogFragment bottomsheetPicker = new MyBottomSheetDialogFragment(); bottomsheetPicker.setOnChooseReasonListener(this); bottomsheetPicker.show(fm, MyBottomSheetDialogFragment.getTag()); }
      • @Xaren 你能发布正确的代码吗?我想不通。
      猜你喜欢
      • 2019-05-20
      • 2014-02-15
      • 2020-09-03
      • 2021-05-11
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 2018-01-07
      相关资源
      最近更新 更多