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
|
}
|
}
|