Show DialogFragment from another DialogFragment

前端 未结 9 742
春和景丽
春和景丽 2021-02-05 10:33

I have a DialogFragment that displays a list of options to the user, one of these options is \"Delete\" option, when the user presses the delete option I want to sh

9条回答
  •  情话喂你
    2021-02-05 11:02

    Please check this following code. Hope this will help many of you!

    public class SubcategoryFragment extends DialogFragment {
    
    public SubcategoryFragment() {
    
    }
    
    public static SubcategoryFragment newInstance(Integer code, String name) {
        SubcategoryFragment fragment = new SubcategoryFragment();
    
        mCode = code;
        mTitle = name;
        return fragment;
    }
    
    
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        liststring = new ArrayList<>();
    
        getAdapter();
    
    }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.fragment_subcategory, container, false);
        gridView = (GridView) view.findViewById(R.id.sub_grid);
    
    
        return view;
    
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        closeDialog = (ImageView) getDialog().findViewById(R.id.closeDialog);
        title = (TextView) getDialog().findViewById(R.id.dialogTitle);
        gridView = (GridView) getDialog().findViewById(R.id.sub_grid);
    
        title.setText(String.format("Choose %s", mTitle));
        closeDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDialog().dismiss();
            }
        });
    
    
    }
    
    @Override
    public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
    
    
        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    
        //  closeDialog = (ImageView) dialog.findViewById(R.id.closeDialog);
    
        return dialog;
    
    }
    
    
    
    public void getAdapter() {
    
            gridAdapter = new HomeSubGridViewAdapter(getContext(), R.layout.gridview_custom_layout, liststring);
            gridView.setAdapter(gridAdapter);
    
    }
    
    
    }
    

    This is the method for calling dialog from fragment

      fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
                        SubcategoryFragment postalFragment = SubcategoryFragment.newInstance(Integer.valueOf(item.getId()), item.getName());
                        postalFragment.show(fragmentManager, "SubcategoryFragment");
    

    Feel Free to ask if you feel any problem is that

提交回复
热议问题