Kotlin-带回调的dialogfragment简单使用

效果图:

Kotlin-带回调的dialogfragment简单使用_第1张图片

1.

activity_fragment_dialog 弹框的自定义布局



    

        

        

        

            

2.

activity_main 主界面的布局,这里仅仅是测试程序,所以就两个控件,button用于触发弹框,text用于接收回调的监听显示



    

3.

MainActivity 主界面,用于触发弹框,设置弹框的内容,接收回调方法
package com.dev.ipati.fragmentdialog

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), FragmentDialog.DialogOnClickListener {
    override fun onDialogPositiveClickListener(v: View, res: String) {
        textView.text = res
    }

    override fun onDialogNegativeClickListener(v: View, res: String) {
        textView.text = res
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btDialog.setOnClickListener {
            val fragmentDialog: FragmentDialog = FragmentDialog.Builder()
                .header("HeaderDialogFragment")
                .message("I'm Fragment Dialog Kotlin")
                .build()
            fragmentDialog.isCancelable = false
            fragmentDialog.show(supportFragmentManager, "fragmentDialog")
        }
    }
}

4.

FragmentDialog  主要就是加载弹框布局,然后设置点击回调 
package com.dev.ipati.fragmentdialog

import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.activity_fragment_dialog.*

class FragmentDialog : DialogFragment() {

    var header: String? = DEFAULT_DIALOG_HEADER
    var message: String? = DEFAULT_DIALOG_MESSAGE

    var dialogClickListener: DialogOnClickListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (savedInstanceState == null) {
            arguments?.let(this::onInitInstance)
        } else {
            onRestoreInstanceState(savedInstanceState)
        }
    }

    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?, 
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.activity_fragment_dialog, 
                container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initInstance()
    }

    private fun initInstance() {
        dialogClickListener = if (parentFragment != null) {
            parentFragment as DialogOnClickListener
        } else {
            activity as DialogOnClickListener
        }

        tvHeader.text = header
        tvDescription.text = message

        btSubmit.setOnClickListener { view ->
            dialogClickListener?.onDialogPositiveClickListener(view,"取消")
            dismiss()
        }

        btCancel?.setOnClickListener { view ->
            dialogClickListener?.onDialogNegativeClickListener(view,"确定")
            dismiss()
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putString(ARGS_HEADER, header)
        outState.putString(ARGS_MESSAGE, message)
    }

    private fun onInitInstance(arguments: Bundle) {
        header = arguments.getString(ARGS_HEADER)
        message = arguments.getString(ARGS_MESSAGE)
    }

    private fun onRestoreInstanceState(bundle: Bundle?) {
        header = bundle?.getString(ARGS_HEADER)
        message = bundle?.getString(ARGS_MESSAGE)
    }

    interface DialogOnClickListener {
        fun onDialogPositiveClickListener(v: View,res:String)
        fun onDialogNegativeClickListener(v: View,res:String)
    }

    companion object {
        fun newInstance(header: String, Msg: String): FragmentDialog {
            val fragmentDialog = FragmentDialog()
            val bundle = Bundle().apply {
                putString(ARGS_HEADER, header)
                putString(ARGS_MESSAGE, Msg)
            }
            fragmentDialog.arguments = bundle
            return fragmentDialog
        }
    }

    class Builder {

        private var header: String? = null
        private var message: String? = null

        fun header(header: String) = apply { this.header = header }

        fun message(message: String) = apply { this.message = message }

        fun build() = FragmentDialog.newInstance(
                header = header ?: throw NullPointerException("Header must not be null."),
                Msg = message ?: throw NullPointerException("Message must not be null.")
        )
    }

}

private const val ARGS_HEADER: String = "dialog_header"
private const val ARGS_MESSAGE: String = "dialog_message"
private const val DEFAULT_DIALOG_HEADER = "HelloDialogFragment"
private const val DEFAULT_DIALOG_MESSAGE = "I'm a DialogFragment"

下载地址:https://github.com/Brianmj/DialogFragmentTest

end

你可能感兴趣的:(android,学习)