gpt4 book ai didi

android - 将数据从 BottomSheetDialogFragment 传递或监听到 Fragment

转载 作者:行者123 更新时间:2023-11-30 00:22:03 53 4
gpt4 key购买 nike

我正在尝试监听或将数据从 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;
}
}}

和 fragment :

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

最佳答案

除了它不起作用之外,该代码还有些味道,因为您将 MyBottomSheetDialogFragment 与创建它的对象耦合在一起。

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

myBottomSheetDialogFragment.setOnChooseReasonListener(this);

关于android - 将数据从 BottomSheetDialogFragment 传递或监听到 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46106607/

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com