Commit fd94dcc4 authored by wanglei's avatar wanglei

...BaseFragment.kt

parent 2e61c3b6
...@@ -4,11 +4,11 @@ plugins { ...@@ -4,11 +4,11 @@ plugins {
} }
android { android {
namespace = "com.base.localweatherwhite" namespace = "com.base.appzxhy"
compileSdk = 34 compileSdk = 34
defaultConfig { defaultConfig {
applicationId = "com.base.localweatherwhite" applicationId = "com.base.appzxhy"
minSdk = 24 minSdk = 24
targetSdk = 34 targetSdk = 34
versionCode = 1 versionCode = 1
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<application <application
android:name=".MyApplication"
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules" android:fullBackupContent="@xml/backup_rules"
......
@file:Suppress("unused")
package com.base.appzxhy.base
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
/**
*
* @param VB : ViewBinding
* 基本的fragment绑定数据
*/
abstract class BaseFragment<VB : ViewBinding>(
private val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : Fragment() {
private var _binding: VB? = null
private var fragmentInit = false
open val TAG = javaClass.simpleName
//重写binding get()方法不直接暴露真实的可空_binding数据
@Suppress("MemberVisibilityCanBePrivate")
val binding: VB
get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = bindingInflater.invoke(inflater, container, false)
return binding.root
}
open fun onResumeOneShoot() = Unit
override fun onResume() {
if (!fragmentInit) {
onResumeOneShoot()
fragmentInit = true
}
super.onResume()
}
override fun onDestroyView() {
super.onDestroyView()
//要手动置null防止内存泄漏
_binding = null
}
}
inline fun <VB : ViewBinding> BaseFragment<VB>.viewBind(block: VB.() -> Unit) {
binding.apply(block)
}
inline fun Fragment.goToAc(clazz: Class<out Activity>, block: (Intent.() -> Unit) = {}) {
context?.startActivity(Intent(context, clazz).apply(block))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment