因此,我实现了一个继承DialogFragment的自定义类。我想将此对话框用于两种类型的操作:删除和编辑。为此,我对其进行了自定义,以便为每个按钮设置标题,描述和按钮文本。我要做的是创建一个接口,并有两个演示者:DeletePresenter和实现该接口的EditPresenter。界面应具有单击操作和对话框文本的设置。对此我很陌生,我不知道该如何连接所有这些对象(dialogFragment,presenters和接口)?如果有人能给我一个例子,那就太好了。
MyDialogFragment
public class MyDialogFragment extends DialogFragment implements View.OnClickListener {
private DialogPresenterContract dialogPresenterContract;
private String title, description, buttonTxt;
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.dialog_btn:
dialogPresenterContract.onActionClick(editText.getText().toString().trim(), onDialogActionListener);
break;
}
}
DialogPresenterContract
public interface DialogPresenterContract {
void onActionClick(String reason, MyDialogFragment.OnDialogActionListener onDialogActionListener);
String getDialogTitle();
String getDialogDescription();
String getDialogButtonTxt();
}
DeletePresenter
public class DeletePresenter implements DialogPresenterContract {
//implement all methods
}
EditPresenter
public class EditPresenter implements DialogPresenterContract {
//implement all methods
}
[如果您想让MVP尝试一下,但是如果您不符合逻辑,那么我建议您在视图中写她的名字(MyDialogFragment)。
public class MyDialogFragment extends DialogFragment implements View.OnClickListener, MyView {
public static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putInt("type", type);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter.setType(getArguments().getInt("type"));
...
}
private Presenter presenter;
private String title, description, buttonTxt;
@override
void setStrings(String title, String description, String buttonTitle) {
......
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.dialog_btn:
dialogPresenterContract.onActionClick(editText.getText().toString().trim());
break;
}
}
}
MyView界面
interface MyView {
void setStrings(String title, String description, String buttonTitle);
}
演示者
class Presenter {
private MyView view;
private int type;
public Presenter(MyView view) {
this.view = view;
}
void setType(int type) {
this.type = type;
}
void onClick(String text) {
if (type == delete_type) {
.....
} else {
......
}
}
}