18034727885
2026-04-14 370014cbb9ec1ef64e7e42bb4f9a12671461d8e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.xw.common.base
 
 
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.xw.common.utils.BindUtils.enableBindingLogging
import java.lang.reflect.ParameterizedType
 
abstract class BaseFragment<V : ViewDataBinding, VM : BaseViewModel> : BaseLazyloadFragment() {
 
    var TAG = "zmc-${this.javaClass.simpleName}"
 
    lateinit var viewModel: VM
    lateinit var binding: V
    private var dialog: LoadingDialog? = null
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initDataBinding()
        initViewModel()
        initUiObservable()
 
    }
 
    private fun initViewModel() {
        viewModel = createViewModel()
        binding.setVariable(getBindingVariable(), viewModel)
        binding.lifecycleOwner = this
        //让ViewModel拥有View的生命周期感应
        lifecycle.addObserver(viewModel)
    }
 
    /**
     * 创建dataBingding,并自动设置布局
     */
    private fun initDataBinding() {
        var dbClass = genericTypeBinding()
        var method = dbClass.getMethod("inflate", LayoutInflater::class.java)
        binding = method.invoke(null, layoutInflater) as V
        binding.enableBindingLogging()
    }
 
    private fun initUiObservable() {
        viewModel.dialogEvent.observe(this) {
            if (it == "dismiss") {
                dismissDialog()
            } else {
                showDialog(it)
            }
        }
        viewModel.onBackEvent.observe(this) {
            if (it == "back") {
                if (requireActivity() is BaseActivity<*, *>) {
                    (requireActivity() as BaseActivity<*, *>).onBackPressed()
                } else if (requireActivity() is BaseActivityNoVM<*>) {
                    (requireActivity() as BaseActivityNoVM<*>).onBackPressed()
                }
            } else if (it == "finish") {
                if (requireActivity() is BaseActivity<*, *>) {
                    (requireActivity() as BaseActivity<*, *>).finish()
                } else if (requireActivity() is BaseActivityNoVM<*>) {
                    (requireActivity() as BaseActivityNoVM<*>).finish()
                }
            }
        }
        viewModel.closeEvent.observe(this) {
            if (it == "close") {
                if (requireActivity() is BaseActivity<*, *>) {
                    (requireActivity() as BaseActivity<*, *>).closeKeyboard()
                } else if (requireActivity() is BaseActivityNoVM<*>) {
                    (requireActivity() as BaseActivityNoVM<*>).closeKeyboard()
                }
            }
        }
        viewModel.startActivityEvent.observe(this) { params ->
            val clz = params["CLASS"] as Class<*>
            var bundle: Bundle? = null
            try {
                bundle = params["BUNDLE"] as Bundle
            } catch (_: Exception) {
 
            }
            if (params["requestCode"] == null) {
                startActivity(clz, bundle)
                return@observe
            }
            val mIntent = Intent(activity, clz)
            bundle?.let {
                mIntent.putExtras(it)
            }
            startActivityForResult(mIntent, (params["requestCode"] as Int))
        }
    }
 
    /**
     * 跳转页面
     *
     * @param clz    所跳转的目的Activity类
     * @param bundle 跳转所携带的信息
     */
    open fun startActivity(clz: Class<*>?, bundle: Bundle? = null) {
        val intent = Intent(activity, clz)
        bundle?.let {
            intent.putExtras(it)
        }
        startActivity(intent)
    }
 
 
    override fun getContentView(inflater: LayoutInflater, container: ViewGroup?): View {
        return binding.root
    }
 
    private fun createViewModel(): VM {
        return ViewModelProvider(this).get(genericTypeViewModel())
    }
 
    /**
     * 获取参数Variable
     */
    protected abstract fun getBindingVariable(): Int
 
    /**
     * 获取当前类泛型viewmodel的Class类型
     * @return
     */
    fun genericTypeViewModel(): Class<VM> {
        return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
    }
 
    fun genericTypeBinding(): Class<V> {
        return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<V>
    }
 
    fun showDialog(title: String = "加载中...") {
        if (requireActivity().isFinishing) {
            return
        }
        if (dialog != null) {
            dialog?.show(title)
        } else {
            dialog = LoadingDialog(requireActivity())
            dialog?.show(title)
        }
    }
 
    fun dismissDialog() {
        if (dialog != null && dialog!!.isShowing) {
            dialog!!.dismiss()
        }
    }
 
    // 关闭默认局部刷新动画
    fun closeRvAnim(rv: RecyclerView) {
        rv.itemAnimator?.addDuration = 0
        rv.itemAnimator?.changeDuration = 0
        rv.itemAnimator?.moveDuration = 0
        rv.itemAnimator?.removeDuration = 0
        (rv.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
    }
}