-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit 1.2.7 添加ViewBinding,后续无需重写layoutId()方法
- Loading branch information
hegaojian
committed
Jan 13, 2022
1 parent
5fbc627
commit 977b57d
Showing
59 changed files
with
51,935 additions
and
9,492 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
JetpackMvvm/src/main/java/me/hgj/jetpackmvvm/base/activity/BaseVmVbActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package me.hgj.jetpackmvvm.base.activity | ||
|
||
import android.view.View | ||
import androidx.viewbinding.ViewBinding | ||
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel | ||
import me.hgj.jetpackmvvm.ext.inflateBindingWithGeneric | ||
|
||
/** | ||
* 作者 : hegaojian | ||
* 时间 : 2019/12/12 | ||
* 描述 : 包含 ViewModel 和 ViewBinding ViewModelActivity基类,把ViewModel 和 ViewBinding 注入进来了 | ||
* 需要使用 ViewBinding 的清继承它 | ||
*/ | ||
abstract class BaseVmVbActivity<VM : BaseViewModel, VB : ViewBinding> : BaseVmActivity<VM>() { | ||
|
||
override fun layoutId(): Int = 0 | ||
|
||
lateinit var mViewBind: VB | ||
|
||
/** | ||
* 创建DataBinding | ||
*/ | ||
override fun initDataBind(): View? { | ||
mViewBind = inflateBindingWithGeneric(layoutInflater) | ||
return mViewBind.root | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
JetpackMvvm/src/main/java/me/hgj/jetpackmvvm/base/fragment/BaseVmVbFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package me.hgj.jetpackmvvm.base.fragment | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.viewbinding.ViewBinding | ||
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel | ||
import me.hgj.jetpackmvvm.ext.inflateBindingWithGeneric | ||
|
||
/** | ||
* 作者 : hegaojian | ||
* 时间 : 2019/12/12 | ||
* 描述 : ViewModelFragment基类,自动把ViewModel注入Fragment和 ViewBinding 注入进来了 | ||
* 需要使用 ViewBinding 的清继承它 | ||
*/ | ||
abstract class BaseVmVbFragment<VM : BaseViewModel, VB : ViewBinding> : BaseVmFragment<VM>() { | ||
|
||
override fun layoutId() = 0 | ||
|
||
//该类绑定的 ViewBinding | ||
private var _binding: VB? = null | ||
val mViewBind: VB get() = _binding!! | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
_binding = inflateBindingWithGeneric(inflater,container,false) | ||
return mViewBind.root | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
_binding = null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
JetpackMvvm/src/main/java/me/hgj/jetpackmvvm/ext/ViewBindUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package me.hgj.jetpackmvvm.ext | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.databinding.ViewDataBinding | ||
import androidx.fragment.app.Fragment | ||
import androidx.viewbinding.ViewBinding | ||
import java.lang.reflect.InvocationTargetException | ||
import java.lang.reflect.ParameterizedType | ||
|
||
/** | ||
* 作者 : hegaojian | ||
* 时间 : 2021/12/21 | ||
* 描述 : | ||
*/ | ||
|
||
@JvmName("inflateWithGeneric") | ||
fun <VB : ViewBinding> AppCompatActivity.inflateBindingWithGeneric(layoutInflater: LayoutInflater): VB = | ||
withGenericBindingClass<VB>(this) { clazz -> | ||
clazz.getMethod("inflate", LayoutInflater::class.java).invoke(null, layoutInflater) as VB | ||
}.also { binding -> | ||
if (binding is ViewDataBinding) { | ||
binding.lifecycleOwner = this | ||
} | ||
} | ||
|
||
@JvmName("inflateWithGeneric") | ||
fun <VB : ViewBinding> Fragment.inflateBindingWithGeneric(layoutInflater: LayoutInflater, parent: ViewGroup?, attachToParent: Boolean): VB = | ||
withGenericBindingClass<VB>(this) { clazz -> | ||
clazz.getMethod("inflate", LayoutInflater::class.java, ViewGroup::class.java, Boolean::class.java) | ||
.invoke(null, layoutInflater, parent, attachToParent) as VB | ||
}.also { binding -> | ||
if (binding is ViewDataBinding) { | ||
binding.lifecycleOwner = viewLifecycleOwner | ||
} | ||
} | ||
|
||
private fun <VB : ViewBinding> withGenericBindingClass(any: Any, block: (Class<VB>) -> VB): VB { | ||
var genericSuperclass = any.javaClass.genericSuperclass | ||
var superclass = any.javaClass.superclass | ||
while (superclass != null) { | ||
if (genericSuperclass is ParameterizedType) { | ||
try { | ||
return block.invoke(genericSuperclass.actualTypeArguments[1] as Class<VB>) | ||
} catch (e: NoSuchMethodException) { | ||
} catch (e: ClassCastException) { | ||
} catch (e: InvocationTargetException) { | ||
throw e.targetException | ||
} | ||
} | ||
genericSuperclass = superclass.genericSuperclass | ||
superclass = superclass.superclass | ||
} | ||
throw IllegalArgumentException("There is no generic of ViewBinding.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.