С моей Recylerview Adapter
, есть несколько кнопок в каждом Items, То, что я хочу, чтобы показать DialogFragment as confirmation
когда они щелкают и выполнять различные методы/действия, когда positiveButton
нажата.Реализация обратного вызова для DialogFragment
Проблема заключается в том, что я не знаю, как я могу или как реализовать обратный вызов при щелчке на positiveButton
и может выполнять различные методы или действия в соответствии с тем, какую кнопку нужно щелкнуть по каждому элементу.
e.g.
На моем классе адаптер
@Override
public void onBindViewHolder(final PageOnlineAdapter.TheViewHolder holder, final int position) {
holder.btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);
holder.btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);
holder.btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);
holder.btn_remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);
}
Вот мой DialogFragment Класс
public class ConfirmationDialog extends DialogFragment {
public static ConfirmationDialog newInstance(String title, String description) {
ConfirmationDialog frag = new ConfirmationDialog();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("description", description);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String description = getArguments().getString("description");
return new AlertDialog.Builder(getActivity())
//.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setMessage(description)
.setPositiveButton("CONTINUE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//How to tell when this positiveButton is clicked
}
}
)
.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//How to tell when this negativeButton is clicked
}
}
)
.create();
}
}
Вы планируете выполнять разные операции, когда в каждой кнопке нажата положительная кнопка, или вы будете делать то же самое во всех них? –
Да, я планирую сделать другую операцию, когда щелкнут postiveButton. – Polar
Я удивлен, если ниже решение не будет бросать нулевой указатель, если вы повернули экран с открытым диалогом и попытались выполнить обратный вызов. Это диалог FRAGMENT, поскольку он имеет свой собственный жизненный цикл, и вы можете реализовать методы onAttach onDetach, как и фрагмент, - здесь вы должны внедрять обратный вызов интерфейса.Обратный вызов для операции, которая предоставила ссылку FragmentManager. –