这是练习Demo,主要是学习Bulider设计模式,代码规范不标准
1.DialogFragment的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/color_bg">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:textSize="18sp"
android:textColor="@color/color_title"
android:background="@color/color_msg"
android:gravity="center"
android:text="标题"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="信息"
android:textColor="@color/color_msg"
android:textSize="16sp"
android:background="@color/color_bg"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/small_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="固定信息1"
android:textColor="@color/colot_tip"
android:textSize="14sp"
android:background="@color/color_bg"
app:layout_constraintTop_toBottomOf="@+id/msg"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="可隐藏信息"
android:textColor="@color/color_title"
android:background="@color/color_bg"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="@+id/small_msg"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colot_tip"
app:layout_constraintTop_toBottomOf="@+id/tip"/>
<Button
android:id="@+id/left_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/color_bg"
android:layout_marginTop="1dp"
android:text="确定"
app:layout_constraintTop_toBottomOf="@+id/divider"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintRight_toLeftOf="@id/certen_btn"
/>
<Button
android:id="@+id/certen_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:text="取消"
android:background="@drawable/gray_left_border_rect"
app:layout_constraintTop_toBottomOf="@+id/divider"
app:layout_constraintLeft_toRightOf="@id/left_btn"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintRight_toLeftOf="@id/right_btn"
/>
<Button
android:id="@+id/right_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:text="详情"
android:background="@drawable/gray_left_border_rect"
app:layout_constraintTop_toBottomOf="@+id/divider"
app:layout_constraintLeft_toRightOf="@id/certen_btn"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_weight="1"
/>
</android.support.constraint.ConstraintLayout>
2.DiagFragment的点击回调接口
public interface OnDialogButtonClickListener<T> {
void setOnClickListener(T result);
}
3.DiagFragment的实现
public class CustomerDialogFragment extends DialogFragment {
private static final String TITLE="title";
private static final String MESSAGE="msg";
private static final String SMALLMSG="small_msg";
private static final String TIP="tip";
private static final String LEFTBUTTON="left_btn";
private static final String CENTERBUTTON="center_btn";
private static final String RIGHTBUTTON="right_btn";
private OnDialogButtonClickListener dialogButtonClickListener;
private static CustomerDialogFragment newInstance(Builder builder){
CustomerDialogFragment customerDialogFragment=new CustomerDialogFragment();
Bundle bundle=new Bundle();
bundle.putString(TITLE,builder.title);
bundle.putString(MESSAGE,builder.msg);
bundle.putString(SMALLMSG,builder.smallMsg);
bundle.putString(TIP,builder.tip);
bundle.putString(LEFTBUTTON,builder.btnLeft);
bundle.putString(CENTERBUTTON,builder.btnCenter);
bundle.putString(RIGHTBUTTON,builder.btnRight);
customerDialogFragment.setArguments(bundle);
return customerDialogFragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//无标题
getDialog().requestWindowFeature(STYLE_NO_TITLE);
// 透明背景
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
View view=inflater.inflate(R.layout.dialog_fragment,container,false);
TextView title=view.findViewById(R.id.title);
TextView divider=view.findViewById(R.id.divider);
String mtitle=getArguments().getString(TITLE);
if (TextUtils.isEmpty(mtitle)){
title.setVisibility(View.INVISIBLE);
divider.setVisibility(View.VISIBLE);
}else {
title.setVisibility(View.VISIBLE);
divider.setVisibility(View.GONE);
title.setText(mtitle);
}
TextView msg=view.findViewById(R.id.msg);
if (!TextUtils.isEmpty(getArguments().getString(MESSAGE))){
msg.setText(getArguments().getString(MESSAGE));
}
TextView small_msg=view.findViewById(R.id.small_msg);
if (!TextUtils.isEmpty(getArguments().getString(SMALLMSG))){
small_msg.setText(getArguments().getString(SMALLMSG));
}
TextView tip=view.findViewById(R.id.tip);
String mtip=getArguments().getString(TIP);
if (TextUtils.isEmpty(mtip)){
tip.setVisibility(View.INVISIBLE);
}else {
tip.setVisibility(View.VISIBLE);
tip.setText(mtip);
}
Button leftBtn=view.findViewById(R.id.left_btn);
String mleftBtn=getArguments().getString(LEFTBUTTON);
if (TextUtils.isEmpty(mleftBtn)){
leftBtn.setVisibility(View.GONE);
}else {
leftBtn.setVisibility(View.VISIBLE);
leftBtn.setText(mleftBtn);
leftBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dialogButtonClickListener!=null){
dialogButtonClickListener.setOnClickListener(0);
}
}
});
}
Button rightBtn=view.findViewById(R.id.right_btn);
String mrightBtn=getArguments().getString(RIGHTBUTTON);
if (TextUtils.isEmpty(mrightBtn)){
rightBtn.setVisibility(View.GONE);
}else {
rightBtn.setVisibility(View.VISIBLE);
rightBtn.setText(mrightBtn);
rightBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dialogButtonClickListener!=null){
dialogButtonClickListener.setOnClickListener(1);
}
}
});
}
Button centerBtn=view.findViewById(R.id.certen_btn);
String mcenterBtn=getArguments().getString(CENTERBUTTON);
if (TextUtils.isEmpty(mcenterBtn)){
centerBtn.setVisibility(View.GONE);
}else {
centerBtn.setVisibility(View.VISIBLE);
centerBtn.setText(mcenterBtn);
centerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dialogButtonClickListener!=null){
dialogButtonClickListener.setOnClickListener(2);
}
}
});
}
return view;
}
public CustomerDialogFragment setOnDialogResultListener(OnDialogButtonClickListener onDialogResultListener){
this.dialogButtonClickListener=onDialogResultListener;
return this;
}
public static Builder newCostomerBuilder(){
return new Builder();
}
public static class Builder {
CustomerDialogFragment customerDialogFragment;
private String title;
private String msg;
private String smallMsg;
private String tip;
private String btnLeft;
private String btnCenter;
private String btnRight;
public Builder setTitle(String title) {
this.title = title;
return this;
}
public Builder setMsg(String msg) {
this.msg = msg;
return this;
}
public Builder setSmallMsg(String smallMsg) {
this.smallMsg = smallMsg;
return this;
}
public Builder setTip(String tip) {
this.tip = tip;
return this;
}
public Builder setBtnLeft(String btnLeft) {
this.btnLeft = btnLeft;
return this;
}
public Builder setBtnCenter(String btnCenter) {
this.btnCenter = btnCenter;
return this;
}
public Builder setBtnRight(String btnRight) {
this.btnRight = btnRight;
return this;
}
public CustomerDialogFragment build(){
if (customerDialogFragment==null){
customerDialogFragment=CustomerDialogFragment.newInstance(this);
}
return customerDialogFragment;
}
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
getActivity().finish();
}
}
4.调用DialogFragment
CustomerDialogFragment
.newCostomerBuilder()
.setTitle("提示")
.setMsg("这是一个对话框")
.setSmallMsg("注意:这是一个对话框")
.setTip("注意:这是一个对话框")
.build()
.show(getSupportFragmentManager(),"dialog");
==================================================题外=============================================
这个是V4包的Fragment的关闭方法
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
DialogFragment df = (DialogFragment) prev;
df.dismiss();
}
这个是app包的Fragment的创建
FragmentTransaction mFragTransaction = getFragmentManager().beginTransaction();
Fragment fragment = getFragmentManager().findFragmentByTag("dialog");
if(fragment!=null){
//为了不重复显示dialog,在显示对话框之前移除正在显示的对话框
mFragTransaction.remove(fragment);
}
CustomerDialogFragment dialogFragment = new CustomerDialogFragment();
//显示一个Fragment并且给该Fragment添加一个Tag,可通过findFragmentByTag找到该Fragment
dialogFragment.show(mFragTransaction, "dialog");
要在onStop或DialogFragmentonDestroy中关闭
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
这篇博客通过一个练习Demo详细介绍了如何运用Builder设计模式来实现DialogFragment。内容包括DialogFragment的布局设计、点击回调接口的实现、DialogFragment的具体实现以及如何在应用中调用它。同时,文中还提及了V4包与app包中Fragment的相关操作。
630

被折叠的 条评论
为什么被折叠?



