简体   繁体   中英

Use interface in bottom sheet

I want to add a interface in my fragment and my bottom sheet and when bottom sheet done an action(like select a button) it call in my background fragment but when i call interface it returns null and never call when my action done!

this is my code to fill interface , but the condition never called:

if(responseListener != null){
  responseListener.onData( 200,message);
}

my code:

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

So I can't call interface in my another fragment. (because its never calls)

You should initialize your interface instance in onAttach method of a BottomSheet:

@Override 
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        responseListener = (ResponseListener) getParentFragment();
    } catch(Exception e) {
       //handle exception
    }
}

Please be aware that if you want to show BottomSheet dialog from fragment and get a callback in the fragment you need to do it with a getChildFragmentManager() instead of getFragmentManager() call.

If you show it with getFragmentManager() you will get cast exception in onAttach method.

Take a look at this link to see difference between ChildFragmentManager and FragmentManager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM