Android在NavigationDrawer中单击选项时显示DialogFragment

时间:2015-02-28 19:31:14

标签: android navigation-drawer android-dialogfragment

当在NavigationDrawer中选择“logout”选项时,我需要显示DialogFragment。目前,我已经实现了以下代码,但它不起作用。如果有人知道该怎么做,请告诉我。

交换机中的牵连案例是第三个。

我有以下代码:

MainActivity扩展ActionBarActivity

@Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();

        switch (position) {
            case 0:
                LinearLayout tecladoLayout = new LinearLayout(MainActivity.this);
                tecladoLayout.setOrientation(LinearLayout.VERTICAL);
                tecladoLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
                tecladoLayout.setGravity(Gravity.CENTER);

                String nombreMesa = ListaElectoresHolder.getInstance().getMesaAlias() == null ? ListaElectoresHolder.getInstance().getMesaId() : ListaElectoresHolder.getInstance().getMesaAlias();


                TextView textViewTeclado = new TextView(MainActivity.this);
                textViewTeclado.setTextSize(18);
                textViewTeclado.setGravity(Gravity.CENTER);
                textViewTeclado.setTextColor(Color.WHITE);
                textViewTeclado.setText(getString(R.string.etiqueta_mesa_asignada) + " " + nombreMesa);

                tecladoLayout.addView(textViewTeclado);
                getSupportActionBar().setCustomView(tecladoLayout);
                fragment = new TecladoFragment();
                break;
            case 1:
                getSupportActionBar().setCustomView(R.layout.custom_aciion_bar_censo);
                fragment = new Listado();
                break;
            case 2:
                getSupportActionBar().setCustomView(R.layout.custom_action_bar_mesa);
                fragment = new Contacto();
                break;
            case 3:
                ManageSharedPreferences manageSharedPreferences = new ManageSharedPreferences(this);
                DatabaseHelper databaseHelper = new DatabaseHelper(this);
                if (true) {
                    showCancelVoteDialog(manageSharedPreferences, fragmentManager);
                } else {
                    manageSharedPreferences.removeAuthenticationData();
                    AppConstants.goToActivity(this, LoginActivity.class);
                }

                break;
        }
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }

和showCancelDialog方法:

private void showCancelVoteDialog(final ManageSharedPreferences manageSharedPreferences, FragmentManager fragmentManager) {
        String message = this.getString(R.string.warning_not_empty_pile);
        CancelVoteDialogFragment cancelVoteDialogFragment = CancelVoteDialogFragment.newInstance(new CancelVoteDialogFragment.CancelVoteDialogListener() {
            @Override
            public void onOkButtonClick() {
                manageSharedPreferences.removeAuthenticationData();
                AppConstants.goToActivity(thisActivity, LoginActivity.class);
                thisActivity.finish();
            }

            @Override
            public void onCancelButtonClick() {
                // No hace nada
            }
        }, String.valueOf(message));
        cancelVoteDialogFragment.show(fragmentManager, getString(R.string.not_empty_pile_dialog));
    }

最后,自定义DialogFragment:

public class CancelVoteDialogFragment extends DialogFragment {

    private static CancelVoteDialogListener cancelVoteDialogListener;
    private Typeface cabinRegular;


    public interface CancelVoteDialogListener {
        public void onOkButtonClick();

        public void onCancelButtonClick();
    }

    public static CancelVoteDialogFragment newInstance(CancelVoteDialogListener cancelVoteDialogListener, String message) {
        CancelVoteDialogFragment cancelVoteDialogFragment = new CancelVoteDialogFragment();
        CancelVoteDialogFragment.cancelVoteDialogListener = cancelVoteDialogListener;
        Bundle arguments = new Bundle();
        arguments.putString(AppConstants.BODYDIALOGMESSAGE, message);
        cancelVoteDialogFragment.setArguments(arguments);
        return cancelVoteDialogFragment;
    }

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

        // Elimina el título
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        // Obtiene los botones de acción
        View view = inflater.inflate(R.layout.cancel_vote_dialog, container, false);
        RelativeLayout okButton = (RelativeLayout) view.findViewById(R.id.btnAceptVoteDialog);
        RelativeLayout cancelButton = (RelativeLayout) view.findViewById(R.id.btnCancelVoteDialog);
        TextView textViewMessage = (TextView) view.findViewById(R.id.lblMessageWarningCancelVote);
        TextView lblTitleDialog = (TextView) view.findViewById(R.id.lblTitleCancelVote);
        cabinRegular = Typeface.createFromAsset(this.getActivity().getAssets(), "fonts/Cabin-Regular.ttf");
        String message = getArguments().getString(AppConstants.BODYDIALOGMESSAGE);
        textViewMessage.setText(message);
        textViewMessage.setTypeface(cabinRegular);
        lblTitleDialog.setTypeface(cabinRegular);


        // Listeners
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelVoteDialogListener.onOkButtonClick();
                dismiss();
            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelVoteDialogListener.onCancelButtonClick();
                dismiss();
            }
        });
        return view;
    }
}

感谢!!!!

0 个答案:

没有答案