val decorView = this.window.decorView
ViewStubTaskManager.instance(decorView)
.addTask(ViewStubTaskContent(decorView))
.addTask(ViewStubTaskTitle(decorView))
.addTask(ViewStubTaskBottom(decorView))
.start()
class ViewStubTaskManager private constructor(val decorView: View) : Runnable {
private var iViewStubTask: IViewStubTask? = null
companion object {
const val TAG = “ViewStubTaskManager”
@JvmStatic
fun instance(decorView: View): ViewStubTaskManager {
return ViewStubTaskManager(decorView)
}
}
private val queue: MutableList = CopyOnWriteArrayList()
private val list: MutableList = CopyOnWriteArrayList()
fun setCallBack(iViewStubTask: IViewStubTask?): ViewStubTaskManager {
this.iViewStubTask = iViewStubTask
return this
}
fun addTask(viewStubTasks: List): ViewStubTaskManager {
queue.addAll(viewStubTasks)
list.addAll(viewStubTasks)
return this
}
fun addTask(viewStubTask: ViewStubTask): ViewStubTaskManager {
queue.add(viewStubTask)
list.add(viewStubTask)
return this
}
fun start() {
if (isEmpty()) {
return
}
iViewStubTask?.beforeTaskExecute()
// 指定 decorView 绘制下一帧的时候会回调里面的 runnable
ViewCompat.postOnAnimation(decorView, this)
}
fun stop() {
queue.clear()
list.clear()
decorView.removeCallbacks(null)
}
private fun isEmpty() = queue.isEmpty() || queue.size == 0
override fun run() {
if (!isEmpty()) {
// 当队列不为空的时候,先加载当前 viewStubTask
val viewStubTask = queue.removeAt(0)
viewStubTask.inflate()
iViewStubTask?.onTaskExecute(viewStubTask)
// 加载完成之后,再 postOnAnimation 加载下一个
ViewCompat.postOnAnimation(decorView, this)
} else {
iViewStubTask?.afterTaskExecute()
}
}
fun notifyOnDetach() {
list.forEach {
it.onDetach()
}
list.clear()
}
fun notifyOnDataReady() {
list.forEach {
it.onDataReady()
}
}
}
interface IViewStubTask {
fun beforeTaskExecute()
fun onTaskExecute(viewStubTask: ViewStubTask)
fun afterTaskExecute()
}
源码地址:github.com/gdutxiaoxu/… ViewStubTask
,ViewStubTaskManager
**, 有兴趣的可以看看
异步加载
==================================================================
异步加载,简单来说,就是在子线程创建 View。在实际应用中,我们通常会先预加载 View,常用的方案有:
AsyncLayoutInflater
官方提供了一个类,可以来进行异步的inflate,但是有两个缺点:
每次都要现场new一个出来
异步加载的view只能通过callback回调才能获得(死穴)
因此,我们可以仿造官方的 AsyncLayoutInflater 进行改造。核心代码在 AsyncInflateManager。主要介绍两个方法。
asyncInflate
方法,在子线程 inflateView,并将加载结果存放到 mInflateMap 里面。
@UiThread
fun asyncInflate(
context: Context,
vararg items: AsyncInflateItem?
) {
items.forEach { item ->
if (item == null || item.layoutResId == 0 || mInflateMap.containsKey(item.inflateKey) || item.isCancelled() || item.isInflating()) {
return
}
mInflateMap[item.inflateKey] = item
onAsyncInflateReady(item)
inflateWithThreadPool(context, item)
}
}
getInflatedView
方法,用来获得异步inflate出来的view,核心思想如下
先从缓存结果里面拿 View,拿到了view直接返回
没拿到view,但是子线程在inflate中,等待返回
如果还没开始inflate,由UI线程进行inflate
/**
用来获得异步inflate出来的view
@param context
@param layoutResId 需要拿的layoutId
@param parent container
@param inflateKey 每一个View会对应一个inflateKey,因为可能许多地方用的同一个 layout,但是需要inflate多个,用InflateKey进行区分
@param inflater 外部传进来的inflater,外面如果有inflater,传进来,用来进行可能的SyncInflate,
@return 最后inflate出来的view
*/
@UiThread
fun getInflatedView(
context: Context?,
layoutResId: Int,
parent: ViewGroup?,
inflateKey: String?,
inflater: LayoutInflater
): View {
if (!TextUtils.isEmpty(inflateKey) && mInflateMap.containsKey(inflateKey)) {
val item = mInflateMap[inflateKey]
val latch = mInflateLatchMap[inflateKey]
if (item != null) {
val resultView = item.inflatedView
if (resultView != null) {
//拿到了view直接返回
removeInflateKey(item)
replaceContextForView(resultView, context)
Log.i(TAG, “getInflatedView from cache: inflateKey is $inflateKey”)
return resultView
}
if (item.isInflating() && latch != null) {
//没拿到view,但是在inflate中,等待返回
try {
latch.await()
} catch (e: InterruptedException) {
Log.e(TAG, e.message, e)
}
removeInflateKey(item)
if (resultView != null) {
Log.i(TAG, “getInflatedView from OtherThread: inflateKey is $inflateKey”)
replaceContextForView(resultView, context)
return resultView
}
}
//如果还没开始inflate,则设置为false,UI线程进行inflate
item.setCancelled(true)
}
}
Log.i(TAG, “getInflatedView from UI: inflateKey is $inflateKey”)
//拿异步inflate的View失败,UI线程inflate
return inflater.inflate(layoutResId, parent, false)
}
简单 Demo 示范
第一步:选择在合适的时机调用 AsyncUtils#asyncInflate
方法预加载 View,
object AsyncUtils {
fun asyncInflate(context: Context) {
val asyncInflateItem =
AsyncInflateItem(
LAUNCH_FRAGMENT_MAIN,
R.layout.fragment_asny,
null,
null
)
AsyncInflateManager.instance.asyncInflate(context, asyncInflateItem)
}
fun isHomeFragmentOpen() =
getSP(“async_config”).getBoolean(“home_fragment_switch”, true)
}
第二步:在获取 View 的时候,先去缓存里面查找 View
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val startTime = System.currentTimeMillis()
val homeFragmentOpen = AsyncUtils.isHomeFragmentOpen()
val inflatedView: View
inflatedView = AsyncInflateManager.instance.getInflatedView(
context,
R.layout.fragment_asny,
container,
LAUNCH_FRAGMENT_MAIN,
inflater
)
Log.i(
TAG,
“onCreateView: homeFragmentOpen is $homeFragmentOpen, timeInstance is ${System.currentTimeMillis() - startTime}, ${inflatedView.context}”
开发是需要一定的基础的,我是08年开始进入Android这行的,在这期间经历了Android的鼎盛时期,和所谓的Android”凉了“。中间当然也有着,不可说的心酸,看着身边朋友,同事一个个转前端,换行业,其实当时我的心也有过犹豫,但是我还是坚持下来了,这次的疫情就是一个好的机会,大浪淘沙,优胜劣汰。再等等,说不定下一个黄金浪潮就被你等到了。
这是我在这行工作10几年积累的一些资料,如果还想继续在这行业走下去的,或者现在打算跳槽,可以**私信【学习】**我愿意把资料免费分享给大家。
或者直接点击下面链接领取
Android学习PDF+架构视频+面试文档+源码笔记
Android进阶系统学习视频
d学习PDF+架构视频+面试文档+源码笔记](https://github.com/a120464/Android-P7/blob/master/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)
330页 PDF Android核心笔记
[外链图片转存中…(img-MvFQBPfA-1645161954937)]
[外链图片转存中…(img-2Wmu92hX-1645161954939)]
[外链图片转存中…(img-PDOKTjqh-1645161954939)]
[外链图片转存中…(img-PIGHPx2H-1645161954940)]