LoadingDialogFragment.kt 2.47 KB
Newer Older
1
package com.ydl.ydlcommon.ui
konghaorui committed
2 3 4


import android.os.Bundle
YKai committed
5 6 7
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
konghaorui committed
8 9 10 11 12
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
13 14
import com.ydl.ydlcommon.R
import kotlinx.android.synthetic.main.platform_fragment_loading_dialog.view.*
konghaorui committed
15 16 17 18 19 20 21 22 23 24 25 26 27


/**
 * A simple [Fragment] subclass.
 * Use the [LoadingDialogFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class LoadingDialogFragment : DialogFragment() {

    private var msg: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
YKai committed
28
            msg = arguments?.getString(ARG_MSG)
konghaorui committed
29 30
    }

YKai committed
31 32 33 34 35
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
konghaorui committed
36
        // Inflate the layout for this fragment
YKai committed
37 38
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return inflater.inflate(R.layout.platform_fragment_loading_dialog, container, false)
konghaorui committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (!TextUtils.isEmpty(msg)) {
            view.tvMsg.text = msg
        } else {
            view.tvMsg.visibility = View.GONE
        }
    }

    companion object {
        private val ARG_MSG = "arg_msg"
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param msg 要显示的信息.
         * @return  LoadingDialogFragment 实例
         */
        fun newInstance(msg: String?): LoadingDialogFragment {
            val fragment = LoadingDialogFragment()
            val args = Bundle()
            args.putString(ARG_MSG, msg)
            fragment.arguments = args
            return fragment
        }
    }

    override fun show(manager: FragmentManager, tag: String?) {
        try {
            //在每个add事务前增加一个remove事务,防止连续的add
            manager.beginTransaction().remove(this).commitAllowingStateLoss()
            super.show(manager, tag)
        } catch (e: Exception) {
            //同一实例使用不同的tag会异常,这里捕获一下
            e.printStackTrace()
        }
    }

    fun hide() {
        dismissAllowingStateLoss()
    }
}