前言
在開發程序中,我們會經常使用對話框,為了提高開發效率,于是我使用DialogFragment封裝了一個通過的對話框,
為什么使用DialogFragment
首先在使用 DialogFragment之前,我們創建對話框一般都會采用Dialog的形式,并且從撰寫代碼的角度來看,Dialog使用起來也較為簡單,但Android 官方推薦使用 DialogFragment 來代替 Dialog,
DialogFragment 有著Dialog 所沒有的特性在手機配置變化,導致Activity需要重新創建時,例如旋屏,基于DialogFragment的對話框將會由FragmentManager自動重建,然而基于Dialog實作的對話框則沒有這樣的能力),可以讓它具有更高的可復用性(降低耦合)和更好的便利性,


通用DialogFragment代碼:
class CommonDialog: DialogFragment() {
private var mLayoutResId = 0
private var mDimAmount = 0.5f //背景昏暗度
private var mShowBottomEnable= false //是否底部顯示
private var mMargin = 0 //左右邊距
private var mAnimStyle = 0 //進入退出影片
private var mOutCancel = true //點擊外部取消
var mContext: Context? = null
private var mWidth = 0
private var mHeight = 0
private var convertListener:ViewConvertListener?=null
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context
}
override fun onCreate(@Nullable savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_TITLE, setDialogStyle())
}
@Nullable
override fun onCreateView(inflater: LayoutInflater, @Nullable container: ViewGroup?, @Nullable savedInstanceState: Bundle?): View? {
if (mLayoutResId>0){
val view: View = inflater.inflate(mLayoutResId, container, false)
convertListener?.convertView(DialogViewHolder.create(view), this)
return view
}
return null
}
override fun onStart() {
super.onStart()
initParams()
}
private fun initParams() {
val window: Window? = dialog!!.window
if (window != null) {
val params: WindowManager.LayoutParams = window.attributes
params.dimAmount = mDimAmount
//設定dialog顯示位置
if (mShowBottomEnable) {
params.gravity = Gravity.BOTTOM
}
//設定dialog寬高
params.width=if(mWidth==0) getScreenWidth(context!!) - 2 * dp2px(context!!, mMargin.toFloat()) else dp2px(context!!, mWidth.toFloat())
params.height=if(mHeight == 0) WindowManager.LayoutParams.WRAP_CONTENT else dp2px(context!!, mHeight.toFloat())
//設定dialog影片
if (mAnimStyle != 0) {
window.setWindowAnimations(mAnimStyle)
}
window.attributes = params
}
//isCancelable = mOutCancel //設定無效
dialog?.setCancelable(mOutCancel)
dialog?.setCanceledOnTouchOutside(mOutCancel)
}
/**
* 設定背景昏暗度
*
* @param dimAmount
* @return
*/
fun setDimAmount(@FloatRange(from = 0.0, to = 1.0) dimAmount: Float): CommonDialog? {
mDimAmount = dimAmount
return this
}
/**
* 是否顯示底部
*
* @param showBottom
* @return
*/
fun setShowBottom(showBottom: Boolean): CommonDialog? {
mShowBottomEnable = showBottom
return this
}
/**
* 設定寬高
*
* @param width
* @param height
* @return
*/
fun setDialogSize(width: Int, height: Int): CommonDialog? {
mWidth = width
mHeight = height
return this
}
/**
* 設定左右margin
*
* @param margin
* @return
*/
fun setDialogMargin(margin: Int): CommonDialog? {
mMargin = margin
return this
}
/**
* 設定進入退出影片
*
* @param animStyle
* @return
*/
fun setAnimStyle(@StyleRes animStyle: Int): CommonDialog? {
mAnimStyle = animStyle
return this
}
/**
* 設定是否點擊外部取消
*
* @param outCancel
* @return
*/
fun setOutCancel(outCancel: Boolean): CommonDialog? {
mOutCancel = outCancel
return this
}
fun show(manager: FragmentManager): CommonDialog? {
super.show(manager, System.currentTimeMillis().toString())
return this
}
/**
* 設定dialog布局
*
* @return
*/
fun setDialogLayoutId(layoutId: Int=0){
mLayoutResId=layoutId
}
/**
* 設定dialog樣式
*
* @return
*/
fun setDialogStyle(style:Int=R.style.fullScreenDialog): Int{
return style
}
fun setConvertListener(convertListener: ViewConvertListener): CommonDialog? {
this.convertListener = convertListener
return this
}
/**
* 獲取螢屏寬度
*
* @param context
* @return
*/
fun getScreenWidth(context: Context): Int {
val displayMetrics: DisplayMetrics = context.resources.displayMetrics
return displayMetrics.widthPixels
}
fun dp2px(context: Context, dipValue: Float): Int {
val scale: Float = context.resources.displayMetrics.density
return (dipValue * scale + 0.5f).toInt()
}
interface ViewConvertListener{
fun convertView(holder: DialogViewHolder, dialog: CommonDialog)
}
}
DialogViewHolder
class DialogViewHolder(view: View) {
private var views: SparseArray<View>? = null
private var convertView: View? = null
init {
convertView = view
views = SparseArray()
}
companion object{
fun create(view: View): DialogViewHolder {
return DialogViewHolder(view)
}
}
/**
* 獲取View
* @param viewId
* @param <T>
* */
fun <T : View> getView(@IdRes viewId: Int): T {
var view: View? = views!![viewId]
if (view == null) {
view = convertView?.findViewById(viewId)
views!!.put(viewId, view)
}
return view as T
}
/**
* 設定文本
*
* @param viewId
* @param text
*/
fun setText(viewId: Int, text: String?) {
val textView: TextView = getView(viewId)
textView.text = text
}
/**
* 設定字體顏色
*
* @param viewId
* @param colorId
*/
fun setTextColor(viewId: Int, colorId: Int) {
val textView: TextView = getView(viewId)
textView.setTextColor(colorId)
}
/**
* 設定背景圖片
*
* @param viewId
* @param resId
*/
fun setBackgroundResource(viewId: Int, resId: Int) {
val view: View = getView(viewId)
view.setBackgroundResource(resId)
}
/**
* 設定背景顏色
*
* @param viewId
* @param colorId
*/
fun setBackgroundColor(viewId: Int, colorId: Int) {
val view: View = getView(viewId)
view.setBackgroundColor(colorId)
}
/**
* 設定點擊事件
*
* @param viewId
* @param listener
*/
fun setOnClickListener(viewId: Int, callback: () -> Unit) {
val view: View = getView(viewId)
view.setOnClickListener {
callback()
}
}
/**
* 顯示隱藏View
* @param viewId
* @param visibility
*/
fun setVisibility(@IdRes viewId: Int, visibility: Int){
val view: View = getView(viewId)
if (visibility!=view.visibility){
view.visibility = visibility
}
}
}
使用CommonDialog
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv_sure.setOnClickListener {
confirm()
}
tv_comment.setOnClickListener {
comment()
}
}
private fun confirm(){
CommonDialog().run {
setDialogLayoutId(R.layout.layout_dialog_confirm)
setOutCancel(false)
setDialogMargin(30)
setConvertListener(object :CommonDialog.ViewConvertListener{
override fun convertView(holder: DialogViewHolder, dialog: CommonDialog) {
holder.setOnClickListener(R.id.tv_confirm) {
dialog.dismissAllowingStateLoss()
}
holder.setOnClickListener(R.id.tv_cancel) {
dialog.dismissAllowingStateLoss()
}
}
})
show(supportFragmentManager)
}
}
private fun comment(){
CommonDialog().run {
setDialogLayoutId(R.layout.layout_dialog_comment)
setOutCancel(false)
setConvertListener(object :CommonDialog.ViewConvertListener{
override fun convertView(holder: DialogViewHolder, dialog: CommonDialog) {
val tvPublish=holder.getView<TextView>(R.id.tv_publish)
val etComment=holder.getView<EditText>(R.id.et_comment)
tvPublish.setOnClickListener {
val content=etComment.text.toString()
if (TextUtils.isEmpty(content)){
Toast.makeText(mContext,"評論內容不能為空",Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(mContext,content,Toast.LENGTH_SHORT).show()
dialog.dismissAllowingStateLoss()
}
}
}
})
show(supportFragmentManager)
}
}
}
Github代碼地址.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/239077.html
標籤:其他
