0

Im ussing a bottomSheet inside a fragment. Im receiving the must implement FilterListenerBottomSheet error, even though im acctually implementing the interface as you can see bellow. May the error be on the onAttach function?

What am i missing here?

Is it possible to implement BottomSheet inside a fragment rigth? Thanks.

FilterBottomSheet.java :

public class FilterBottomSheet extends BottomSheetDialogFragment {

    private FilterListenerBottomSheet filterListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bottom_sheet_filter, container, false);

        Button btnTodos = v.findViewById(R.id.btn_all);
        Button btnCuchillos = v.findViewById(R.id.btn_cuchillo);
        Button btnCuchillas = v.findViewById(R.id.btn_cuchillas);
        Button btnTenedores = v.findViewById(R.id.btn_tenedores);
        Button btnChairas = v.findViewById(R.id.btn_chairas);
        Button btnTrinchetes = v.findViewById(R.id.btn_trinchetes);

        btnTodos.setOnClickListener(v12 -> {
            filterListener.onButtonClickedFilter("Todos");
            dismiss();
        });

        btnCuchillos.setOnClickListener(v12 -> {
            filterListener.onButtonClickedFilter("Cuchillos");
            dismiss();
        });

        btnCuchillas.setOnClickListener(v1 -> {
            filterListener.onButtonClickedFilter("Cuchillas");
            dismiss();
        });

        btnTenedores.setOnClickListener(v1 -> {
            filterListener.onButtonClickedFilter("Tenedores");
            dismiss();
        });

        btnChairas.setOnClickListener(v1 -> {
            filterListener.onButtonClickedFilter("Cuchillas");
            dismiss();
        });

        btnTrinchetes.setOnClickListener(v1 -> {
            filterListener.onButtonClickedFilter("Trinchetes");
            dismiss();
        });

        return v;
    }

    public interface FilterListenerBottomSheet {
        void onButtonClickedFilter(String filterProduct);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            filterListener = (FilterListenerBottomSheet) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement FilterListenerBottomSheet");
        }
    }
}

HomeFragment:

public class HomeFragment extends Fragment implements ProductsOnCustomClickListener, FilterBottomSheet.FilterListenerBottomSheet {


    private TextView tvFilter;

    public HomeFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  
        getData();
    }

    public void getData(){...}

    private void rvproducts(){...}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_home, container, false);
        rvProducts = v.findViewById(R.id.rv_products);
        ivUserPhoto = v.findViewById(R.id.iv_user_photo);
        tvFilter = v.findViewById(R.id.tv_filter);

        rvproducts();

        tvFilter.setOnClickListener(v1 -> {
            FilterBottomSheet filterBottomSheet = new FilterBottomSheet();
            filterBottomSheet.show(getActivity().getSupportFragmentManager(),"filterBottomSheet");
        });
return v;
    }

    @Override
    public void onItemClick (Product product, int position) {}

    @Override
    public void onButtonClickedFilter(String filterProduct) {
        filter = filterProduct;
        tvFilter.setText(filter);
    }
}
  • The `Context` passed into `onAttach()` is the `Activity`, not the calling `Fragment`, since a `Fragment` is not a `Context`. If you do mean for `HomeFragment` to be the listener, then you need to cast `getParentFragment()` instead of the `context` parameter, and you need to use `getChildFragmentManager()` instead of `getActivity().getSupportFragmentManager()` in the `show()` call. – Mike M. Jul 26 '21 at 11:22
  • @MikeM. I apologize for my ignorance, first time on it. I did this and im been capable to open the bottomSheet but im getting null pointer exepction now at the Listener at `OnClickListener` on the BottomSheet buttons: ```homeFragment = (HomeFragment) getParentFragment(); homeFragment.getView().findViewById(R.id.fmt_home);``` and: ```public void onAttach(Context context) {super.onAttach(context); try {filterListener = (FilterListenerBottomSheet) homeFragment ;} catch {...}} ``` – Agustín Rabini Jul 26 '21 at 15:31
  • I'm not sure where that code is from, but following from the code in the question, my suggestion was to first change the casting line in `onAttach()` to `filterListener = (FilterListenerBottomSheet) getParentFragment();`, and then change the `show()` call in `HomeFragment` to `filterBottomSheet.show(getChildFragmentManager(),"filterBottomSheet");`. – Mike M. Jul 26 '21 at 15:32
  • 1
    @MikeM. I see, i was misunderstanding your comment, now i could solved the problem. Thanks for your time, really apreciate your help. Exitos! – Agustín Rabini Jul 26 '21 at 15:37

0 Answers0