Ich versuche zu hören oder Daten von einem BotomSheetDialogFragment in Fragment zu übergeben, um etwas am Fragment zu ändern (genau wie ein Picker).Daten von BottomSheetDialogFragment an Fragment übergeben oder abhören
Ich habe mit getTargetFragment versucht, den Hörer zu instanziieren aber ein Compiler Fehler entdeckt zu werden: 'MyFragment', erforderlich: weniger 'android.support.v4.app.Fragment' ..
Irgendwelche Ideen oder i‘ Machst du den falschen Ansatz?
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;
}
}}
Und das 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());
}
}
Danke, dass Sie Recht haben. Ich brauche viel Erfahrung und lernen, um besser zu werden. Und danke, dass du mir hilfst, die Antwort zu sehen. Ich vermisse auch, den Code zu veröffentlichen, um das BottomSheet zu instantiieren. Hier ist, wie ich das aus dem Fragment gelöst habe: 'öffentliche void showPicker() { MyBottomSheetDialogFragment bottomsheetPicker = neue MyBottomSheetDialogFragment(); bottomsheetPicker.setOnChooseReasonListener (this); bottomsheetPicker.show (fm, MyBottomSheetDialogFragment.getTag()); } ' – Xaren