封装了一个dialogfragment工具

dialogfragment 代替dilog的好处就不说了

/**
 * 基础DialogFragment
 * 主要是封装了一些基础的参数
 * 也可以根据自己的项目添加公有参数
 */
 public FragmentActivity mActivity;

    public abstract int getLayoutId();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (FragmentActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //无标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置背景透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(getLayoutId(), container);
        return view;
    }


    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        if (window != null) {
            //设置窗体背景色透明
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //设置宽高
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            layoutParams.width = (int) (dm.widthPixels * 0.75);
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(layoutParams);
        }
    }





/**
 * 用于dialog创建view后 将我view回传到当前activity 或者fragment中
 */
public interface BaseDialogListener {

    void onDialogStart(BaseDialog dialog ,View view);
}
**
 * 一个正常的dialog 
 */
public class NormalDialog extends BaseDialog {

    public TextView mTitleTv;
    public TextView mContentTv;
    public Button mCancelBtn;
    public Button mOkBtn;
    private BaseDialogListener mListenerDialog;

    @Override
    public int getLayoutId() {
        return R.layout.normal_dialog;
    }

    public static NormalDialog newInstance() {
        Bundle args = new Bundle();
        NormalDialog fragment = new NormalDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.e(TAG, "text123 onViewCreated: ");
        mTitleTv = view.findViewById(R.id.normal_dialog_title_tv);
        mContentTv = view.findViewById(R.id.normal_dialog_content_tv);
        mCancelBtn = view.findViewById(R.id.normal_dialog_cancel_btn);
        mOkBtn = view.findViewById(R.id.normal_dialog_ok_btn);
        if (mListenerDialog!=null) {
            mListenerDialog.onDialogStart(NormalDialog.this,view);
        }
        mCancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NormalDialog.this.dismiss();
            }
        });
    }

    public void addClickListener(BaseDialogListener listenerDialog) {
        mListenerDialog = listenerDialog;
    }


    @Override
    public void onStart() {
        super.onStart();
        this.getDialog().setCancelable(false);
        this.getDialog().setCanceledOnTouchOutside(false);
    }
 * dialog 管理者
 * 还可以添加其他功能
 */
public class DialogManager {
    private BaseDialog mDialog;
    private FragmentActivity mActivity;

    public DialogManager Builder(BaseDialog dialog) {
        Log.e(TAG, "Builder: ");
        mDialog = dialog;
        return this;
    }

    public DialogManager setContext(FragmentActivity activity) {
        mActivity = activity;
        return this;
    }



    public void show(String tag) {

        if (mActivity == null) {
            Log.e(TAG, "show: mActivity == null");
            return;
        }
        FragmentManager supportFragmentManager = mActivity.getSupportFragmentManager();
        mDialog.show(supportFragmentManager, tag);
    }


    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

}

/**+
 * 基础使用
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DialogManager dialogManager = new DialogManager();
        NormalDialog normalDialog = NormalDialog.newInstance();
        normalDialog.addClickListener(mSetDialogListener);
        dialogManager.Builder(normalDialog).setContext(this).show("text");
    }

    BaseDialogListener mSetDialogListener = new BaseDialogListener() {
        @Override
        public void onDialogStart(BaseDialog dialog, View view) {
            final NormalDialog mDialog = (NormalDialog) dialog;
            mDialog.mTitleTv.setText("text123");
            mDialog.mContentTv.setText("我是内容");
            mDialog.mOkBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.mContentTv.setText("我是内容 text123");
                }
            });
        }
    };
}

最后除去 normalDialog还可以自定义其他dialog 比如waitDiaglog 只要集成baseDialog就可以了

你可能感兴趣的:(封装了一个dialogfragment工具)