使用底部的界面 sheet
Use interface in bottom sheet
我想在我的片段和我的底部添加一个界面 sheet 当底部 sheet 完成一个动作(比如 select 一个按钮)它调用我的背景片段但是当我调用接口时 returns null 并且当我的操作完成时从不调用!
这是我填充接口的代码,但从未调用过条件:
if(responseListener != null){
responseListener.onData( 200,message);
}
我的代码:
public class FilterBottomSheet extends BottomSheetDialogFragment implements View.OnClickListener {
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override public void onStateChanged(@NonNull View bottomSheet,int newState){
if(newState==BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
}
@Override public void onSlide(@NonNull View bottomSheet,float slideOffset){}};
@SuppressLint("RestrictedApi")
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.bottom_sheet_filter, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent())
.getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnDoAction:
fillActionInterface("TEST");
dismiss();
break;
}
}
public OnResponseListener responseListener;
private void fillActionInterface(String message) {
if (responseListener != null) {
responseListener.onData(200, message);
}
}
}
所以我无法在我的另一个片段中调用接口。 (因为它从不调用)
您应该在 BottomSheet 的 onAttach 方法中初始化您的界面实例:
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
responseListener = (ResponseListener) getParentFragment();
} catch(Exception e) {
//handle exception
}
}
请注意,如果您想从片段中显示 BottomSheet 对话框并在片段中获得回调,您需要使用 getChildFragmentManager() 而不是 getFragmentManager() 调用。
如果你用 getFragmentManager() 显示它,你将在 onAttach 中得到强制转换异常方法。
查看 this link 了解 ChildFragmentManager 和 FragmentManager 之间的区别。
我想在我的片段和我的底部添加一个界面 sheet 当底部 sheet 完成一个动作(比如 select 一个按钮)它调用我的背景片段但是当我调用接口时 returns null 并且当我的操作完成时从不调用!
这是我填充接口的代码,但从未调用过条件:
if(responseListener != null){
responseListener.onData( 200,message);
}
我的代码:
public class FilterBottomSheet extends BottomSheetDialogFragment implements View.OnClickListener {
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override public void onStateChanged(@NonNull View bottomSheet,int newState){
if(newState==BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
}
@Override public void onSlide(@NonNull View bottomSheet,float slideOffset){}};
@SuppressLint("RestrictedApi")
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.bottom_sheet_filter, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent())
.getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnDoAction:
fillActionInterface("TEST");
dismiss();
break;
}
}
public OnResponseListener responseListener;
private void fillActionInterface(String message) {
if (responseListener != null) {
responseListener.onData(200, message);
}
}
}
所以我无法在我的另一个片段中调用接口。 (因为它从不调用)
您应该在 BottomSheet 的 onAttach 方法中初始化您的界面实例:
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
responseListener = (ResponseListener) getParentFragment();
} catch(Exception e) {
//handle exception
}
}
请注意,如果您想从片段中显示 BottomSheet 对话框并在片段中获得回调,您需要使用 getChildFragmentManager() 而不是 getFragmentManager() 调用。
如果你用 getFragmentManager() 显示它,你将在 onAttach 中得到强制转换异常方法。
查看 this link 了解 ChildFragmentManager 和 FragmentManager 之间的区别。