From 47c2ded29cbe06cad3891afd19e71600d423c5d1 Mon Sep 17 00:00:00 2001 From: br3ant <1106617567@qq.com> Date: Thu, 9 Sep 2021 16:25:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E5=BA=93,=E6=96=B0=E5=A2=9E=E6=89=A9=E5=B1=95=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/br3ant/base/BaseFragment.kt | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/utils/src/main/java/com/br3ant/base/BaseFragment.kt b/utils/src/main/java/com/br3ant/base/BaseFragment.kt index e32e3eb..daaf336 100644 --- a/utils/src/main/java/com/br3ant/base/BaseFragment.kt +++ b/utils/src/main/java/com/br3ant/base/BaseFragment.kt @@ -1,6 +1,5 @@ package com.br3ant.base -import android.content.Context import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -8,19 +7,14 @@ import android.view.ViewGroup import androidx.annotation.IdRes import com.weikaiyun.fragmentation.SupportFragment -abstract class BaseFragment(val layout: Int = 0) : SupportFragment() { - private lateinit var mRootView: View - protected lateinit var mContext: Context - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - if (!this::mRootView.isInitialized) { - val id = if (layout == 0) getLayoutId() else layout - if (id == 0) throw IllegalAccessException("layoutId 不能为空") - - mRootView = inflater.inflate(id, container, false) - mContext = mRootView.context - } - return mRootView +/** + * layout必须传,使用viewBing也要传 + */ +abstract class BaseFragment(private val layout: Int = 0) : SupportFragment() { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + val id = if (layout == 0) getLayoutId() else layout + return inflater.inflate(id, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -33,10 +27,9 @@ abstract class BaseFragment(val layout: Int = 0) : SupportFragment() { observeData() } - fun findViewById(@IdRes id: Int): T { - return mRootView.findViewById(id) - } + fun findViewById(@IdRes viewId: Int): T? = view?.findViewById(viewId) + @Deprecated("使用构造函数传递或者使用viewBing", ReplaceWith("viewBing")) protected open fun getLayoutId(): Int = 0 protected open fun observeData() {} protected abstract fun initView()