Skip to content

Commit 9bffe5a

Browse files
committed
feat(core): 新增 FastInflaterRecycler 侧加速预热方案
- 新增 FastInflaterRecycler 对象,推荐 RecyclerView 创建侧预热 - FastInflaterRecycler.install 替代旧的 FastRecycledViewPool.install 方法 - 允许只预热指定布局而不替换 RecyclerView 的 RecycledViewPool - 更新示例代码,明确 FastInflater 只加速 onCreateViewHolder 创建流程 - proguard 保留规则新增 FastInflaterRecycler 类 - 旧入口 FastRecycledViewPool.install 兼容新实现,内部调用 FastInflaterRecycler.install
1 parent ba605a0 commit 9bffe5a

4 files changed

Lines changed: 53 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,14 @@ FastInflater.get().inflateAsync(context, R.layout.item_feed, parent) { view ->
7777
### RecyclerView 集成
7878

7979
```kotlin
80-
// 预热 + 安装
81-
FastRecycledViewPool.install(recyclerView, warmUpLayouts = listOf(
80+
// 预热 + 安装创建侧加速
81+
FastInflaterRecycler.install(recyclerView, warmUpLayouts = listOf(
8282
ViewPool.WarmUpEntry(R.layout.item_feed, 4)
8383
))
8484

85+
// 也可以只预热,不替换 RecyclerView.RecycledViewPool
86+
FastInflaterRecycler.preload(recyclerView, R.layout.item_feed, count = 4)
87+
8588
// Adapter 中让 viewType == layoutId
8689
override fun getItemViewType(position: Int) = R.layout.item_feed
8790

@@ -91,7 +94,7 @@ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder
9194
}
9295
```
9396

94-
FastInflater 只在创建侧加速,不参与 RecyclerView 的回收流程,不会出现双池冲突。
97+
FastInflater 只在 `onCreateViewHolder` 创建侧加速,不参与 RecyclerView 的回收流程,不会出现双池冲突。
9598

9699
### DataBinding
97100

‎lib/consumer-rules.pro‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# 保留公开 API
55
-keep class com.github.donglua.fastinflater.FastInflater { *; }
66
-keep class com.github.donglua.fastinflater.FastDataBinding { *; }
7+
-keep class com.github.donglua.fastinflater.FastInflaterRecycler { *; }
78
-keep class com.github.donglua.fastinflater.FastRecycledViewPool { *; }
89
-keep class com.github.donglua.fastinflater.ViewPool { *; }
910
-keep class com.github.donglua.fastinflater.ViewPool$WarmUpEntry { *; }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.github.donglua.fastinflater
2+
3+
import androidx.annotation.LayoutRes
4+
import androidx.recyclerview.widget.RecyclerView
5+
6+
/**
7+
* RecyclerView 场景的推荐入口。
8+
*
9+
* FastInflater 只加速 ViewHolder 创建侧:预热 item View,并在
10+
* Adapter.onCreateViewHolder 中通过 FastInflater.get().inflate(parent, viewType)
11+
* 优先取到预热 View。ViewHolder 创建后的滑动复用仍交给 RecyclerView 自己。
12+
*/
13+
object FastInflaterRecycler {
14+
15+
fun install(
16+
recyclerView: RecyclerView,
17+
warmUpLayouts: List<ViewPool.WarmUpEntry> = emptyList()
18+
) {
19+
recyclerView.setRecycledViewPool(FastRecycledViewPool(recyclerView.context))
20+
if (warmUpLayouts.isNotEmpty()) {
21+
FastInflater.get().warmUp(recyclerView.context, warmUpLayouts)
22+
}
23+
}
24+
25+
fun preload(
26+
recyclerView: RecyclerView,
27+
@LayoutRes layoutId: Int,
28+
count: Int
29+
) {
30+
FastInflater.get().warmUp(recyclerView.context, layoutId, count)
31+
}
32+
33+
fun preload(
34+
recyclerView: RecyclerView,
35+
warmUpLayouts: List<ViewPool.WarmUpEntry>
36+
) {
37+
if (warmUpLayouts.isNotEmpty()) {
38+
FastInflater.get().warmUp(recyclerView.context, warmUpLayouts)
39+
}
40+
}
41+
}

‎lib/src/main/java/com/github/donglua/fastinflater/FastRecycledViewPool.kt‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import androidx.recyclerview.widget.RecyclerView
1616
*
1717
* 使用方式:
1818
* ```
19-
* // 设置 pool
20-
* FastRecycledViewPool.install(recyclerView, warmUpLayouts = listOf(
19+
* // 设置创建侧预热
20+
* FastInflaterRecycler.install(recyclerView, warmUpLayouts = listOf(
2121
* ViewPool.WarmUpEntry(R.layout.item_feed, 4)
2222
* ))
2323
*
@@ -42,7 +42,8 @@ class FastRecycledViewPool(
4242

4343
companion object {
4444
/**
45-
* 便捷方法:为 RecyclerView 设置 FastRecycledViewPool 并预热指定布局。
45+
* 兼容旧入口。新代码建议使用 [FastInflaterRecycler.install],
46+
* 语义更接近“RecyclerView 创建侧预热”。
4647
*
4748
* 预热的 View 会进入 FastInflater 的池,当 Adapter.onCreateViewHolder
4849
* 调用 FastInflater.get().inflate() 时会优先命中这些预热的 View。
@@ -51,12 +52,7 @@ class FastRecycledViewPool(
5152
recyclerView: RecyclerView,
5253
warmUpLayouts: List<ViewPool.WarmUpEntry> = emptyList()
5354
) {
54-
val pool = FastRecycledViewPool(recyclerView.context)
55-
recyclerView.setRecycledViewPool(pool)
56-
57-
if (warmUpLayouts.isNotEmpty()) {
58-
FastInflater.get().warmUp(recyclerView.context, warmUpLayouts)
59-
}
55+
FastInflaterRecycler.install(recyclerView, warmUpLayouts)
6056
}
6157
}
6258
}

0 commit comments

Comments
 (0)