本文精读 Launcher3 应用抽屉(All Apps)的全部源码实现。核心数据流是:后台 Model 线程把 LauncherApps 收集到的应用封装为 AppInfo[](预排序、预计算 sectionName),经 AllAppsStore 推送到前台,AlphabeticalAppsList 监听 Store 后完成二次排序(本地化 Collator)、按字母分 section、生成 AdapterItem 列表与 FastScrollSectionInfo 段表,再由 BaseAllAppsAdapter 配合 DiffUtil 增量刷新到 AllAppsRecyclerView。容器层 ActivityAllAppsContainerView<T> 用三套 AdapterHolder(MAIN/WORK/SEARCH)统一管理个人、工作、搜索三个互斥视图态,AllAppsTransitionController 用单一 mProgress ∈ [0,1] 驱动 Workspace ↔ All Apps 的位移与 alpha。每个设计取舍(泛型为何、为何用 ICU AlphabeticIndex、为何用位掩码 ViewType、为何 DiffUtil、为何延迟更新防抖)都在对应章节给出源码级解释。
源码路径:packages/apps/Launcher3/src/com/android/launcher3/allapps/,对照版本为 aosp-r4。
1. 整体类结构与数据流 1.1 目录分层与职责 allapps/ 下 30 余个类,按职责分为五层:
容器与状态层
类
职责
ActivityAllAppsContainerView<T extends Context & ActivityContext>
抽象泛型总容器,继承 SpringRelativeLayout。持有三套 AdapterHolder、Header、搜索框、快速滚动条。所有交互的协调中心。
LauncherAllAppsContainerView
ActivityAllAppsContainerView<Launcher> 的 Launcher 专用子类,覆写 isInAllApps()、浮动搜索条 margin(按 LauncherState 决定)。
SecondaryLauncherAllAppsContainerView
二级 Launcher(Taskbar 的 All Apps 入口)容器。
AllAppsTransitionController
Workspace ↔ All Apps 位移动画,实现 StateHandler<LauncherState>,核心 mProgress ∈ [0,1]。
SearchTransitionController
A‑Z ↔ 搜索结果过渡动画,继承 RecyclerViewAnimationController。
数据层
类
职责
AllAppsStore
前台应用数据中枢。持有 AppInfo[] mApps(按 COMPONENT_KEY_COMPARATOR 二分排序),通过 OnUpdateListener 通知下游。
AlphabeticalAppsList
排序 + 分组引擎。监听 AllAppsStore,做字母排序、生成 AdapterItem 与 FastScrollSectionInfo。
AppInfoComparator
应用比较器:先按 title(本地化 Collator),再按 componentName,再按 user serial。
列表渲染层
类
职责
BaseAllAppsAdapter
抽象适配器基类,定义全部 VIEW_TYPE_* 位掩码、AdapterItem、ViewHolder,onCreate/onBind 分发。
AllAppsGridAdapter
网格实现,含 AppsGridLayoutManager 与 GridSpanSizer(跨列控制)。
AllAppsRecyclerView
带快速滚动的 RecyclerView,继承 FastScrollRecyclerView。
AllAppsFastScrollHelper
触摸 fraction → section 的平滑滚动辅助。
FloatingHeaderView
悬浮 section header(滚动时吸顶),同时承载预测应用行。
Profile 管理层
类
职责
WorkProfileManager
工作资料过滤、静音切换、教育卡片。
PrivateProfileManager
私密空间(Android 15)解锁/锁定/动画。
UserProfileManager
两者基类状态机 STATE_DISABLED / TRANSITION / ENABLED。
搜索层(search/ 子目录)
类
职责
SearchUiManager
搜索 UI 控制接口(非类)。
AllAppsSearchBarController
搜索框控制器,实现 TextWatcher,把输入转给 SearchAlgorithm。
DefaultAppSearchAlgorithm
默认本地搜索算法,标题前缀匹配,最多 5 条。
AppsSearchContainerLayout
搜索框 + 容器,实现 SearchUiManager 与 SearchCallback。
SearchAdapterProvider
搜索结果 View 提供者抽象(AOSP 里可被插件替换)。
DefaultSearchAdapterProvider
默认空实现,AOSP 默认无插件。
1.2 数据流(端到端) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 PackageManager / LauncherApps │ (Model 后台线程,AllAppsList 收集,AlphabeticIndexCompat 算 sectionName) ▼ AppInfo[](预排序 COMPONENT_KEY_COMPARATOR,预填 sectionName) │ AllAppsStore.setApps(apps, flags, map) ▼ notifyUpdate() → OnUpdateListener 回调 │ ├──► AlphabeticalAppsList.onAppsUpdated() │ │ filter → sort(AppInfoComparator) → 中文 section 归并 → 生成 AdapterItem + FastScrollSectionInfo │ ▼ DiffUtil.calculateDiff() → dispatchUpdatesTo(adapter) │ └──► ActivityAllAppsContainerView.onAppsUpdated() │ mHasWorkApps / mHasPrivateApps 重算 → rebindAdapters() ▼ AllAppsRecyclerView 渲染(Main/Work 两个 RV,或 Search RV)
设计要点:sectionName 在后台已算好(AllAppsList 里调 AlphabeticIndexCompat),前台只做二次排序和分组,避免 UI 线程做 ICU 调用。AllAppsStore 不持锁,用 CopyOnWriteArrayList 承载监听器,保证后台推送与前台回调的线程安全。
2. 容器层:ActivityAllAppsContainerView 与 LauncherAllAppsContainerView 2.1 泛型基类的设计意图 1 2 3 4 5 6 7 public class ActivityAllAppsContainerView <T extends Context & ActivityContext> extends SpringRelativeLayout implements DragSource , Insettable, OnDeviceProfileChangeListener, PersonalWorkSlidingTabStrip.OnActivePageChangedListener, ScrimView.ScrimDrawingController { protected final T mActivityContext;
T extends Context & ActivityContext 是交叉类型(intersection type)约束。Context 提供 Android 资源、ActivityContext 提供 Launcher 的领域能力(getDeviceProfile()、getStatsLogManager()、getDragLayer()、getStateManager())。
为什么这么设计:
基类要在多个宿主复用——Launcher、Taskbar、Secondary Launcher 三套宿主各自实现 ActivityContext,但都不是同一个类。泛型让基类持有强类型的 mActivityContext,调用 mActivityContext.getDeviceProfile() 时编译期类型确定,无需强转。
若不用泛型而用接口 ActivityContext,则无法调用 Context 方法(getResources() 等);若用 Activity,则无法给 Taskbar 用。交叉类型是唯一能同时满足两者且类型安全的写法。
子类用具体类型具现化:
1 2 3 public class LauncherAllAppsContainerView extends ActivityAllAppsContainerView <Launcher> {
SecondaryLauncherAllAppsContainerView 同理具现化为 ActivityAllAppsContainerView<SecondaryLauncher>。
2.2 构造链与三套 AdapterHolder 构造器三参数重载最终走到 (Context, AttributeSet, int):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public ActivityAllAppsContainerView (Context context, AttributeSet attrs, int defStyleAttr) { super (context, attrs, defStyleAttr); mActivityContext = ActivityContext.lookupContext(context); mAllAppsStore = mActivityContext.getActivityComponent().getAppsStore(); mScrimColor = Themes.getAttrColor(context, R.attr.allAppsScrimColor); mHeaderThreshold = getResources().getDimensionPixelSize( R.dimen.dynamic_grid_cell_border_spacing); mHeaderProtectionColor = Themes.getAttrColor(context, R.attr.allappsHeaderProtectionColor); mWorkManager = new WorkProfileManager (this , mActivityContext.getStatsLogManager(), UserCache.INSTANCE.get(mActivityContext)); mPrivateProfileManager = new PrivateProfileManager (this , mActivityContext.getStatsLogManager(), UserCache.INSTANCE.get(mActivityContext)); mPrivateSpaceBottomExtraSpace = context.getResources().getDimensionPixelSize( R.dimen.ps_extra_bottom_padding); mAH = Arrays.asList(null , null , null ); mNavBarScrimPaint = new Paint (); mNavBarScrimPaint.setColor(Themes.getNavBarScrimColor(mActivityContext)); AllAppsStore.OnUpdateListener onAppsUpdated = this ::onAppsUpdated; mAllAppsStore.addUpdateListener(onAppsUpdated); setOnFocusChangeListener((v, hasFocus) -> { if (hasFocus && getActiveRecyclerView() != null ) { getActiveRecyclerView().requestFocus(); } }); mSearchUiDelegate = createSearchUiDelegate(); initContent(); mSearchTransitionController = new SearchTransitionController (this ); }
initContent() 创建三套 AdapterHolder 并 inflate 布局:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 protected void initContent () { mMainAdapterProvider = mSearchUiDelegate.createMainAdapterProvider(); mAH.set(AdapterHolder.MAIN, new AdapterHolder (AdapterHolder.MAIN, new AlphabeticalAppsList (mActivityContext, mAllAppsStore, null , mPrivateProfileManager))); mAH.set(AdapterHolder.WORK, new AdapterHolder (AdapterHolder.WORK, new AlphabeticalAppsList (mActivityContext, mAllAppsStore, mWorkManager, null ))); mAH.set(SEARCH, new AdapterHolder (SEARCH, new AlphabeticalAppsList (mActivityContext, null , null , null ))); getLayoutInflater().inflate(R.layout.all_apps_content, this ); mHeader = findViewById(R.id.all_apps_header); mSearchContainer = inflateSearchBar(); if (!isSearchBarFloating()) { addView(mSearchContainer); mSearchContainer.setFocusedByDefault(true ); } mSearchUiManager = (SearchUiManager) mSearchContainer; }
三套 AdapterHolder 的职责分工:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class AdapterHolder { public static final int MAIN = 0 ; public static final int WORK = 1 ; public static final int SEARCH = 2 ; private final int mType; public final BaseAllAppsAdapter mAdapter; final RecyclerView.LayoutManager mLayoutManager; final AlphabeticalAppsList mAppsList; final Rect mPadding = new Rect (); AllAppsRecyclerView mRecyclerView; private OnFocusChangeListener mOnFocusChangeListener; AdapterHolder(int type, AlphabeticalAppsList appsList) { mType = type; mAppsList = appsList; mAdapter = createAdapter(mAppsList); mAppsList.setAdapter(mAdapter); mLayoutManager = mAdapter.getLayoutManager(); }
为什么用三套独立 holder 而不是一个 holder 切数据源:
三个视图态是互斥 的(任意时刻只有一个可见),但它们的滚动位置、过滤条件、数据集各不相同 。用三套 holder 各持独立 AlphabeticalAppsList + RecyclerView,切换时只需切可见性,不必重新排序、重新绑定。这是空间换时间的典型取舍——多一份对象,换来切换零延迟。
SEARCH 的 AlphabeticalAppsList 构造时 appsStore = null,不注册 Store 监听,结果完全由 setSearchResults() 外部推送,避免 A‑Z 列表变动污染搜索结果。
2.3 onCreate / onResume 对应:onFinishInflate / onAttachedToWindow ActivityAllAppsContainerView 是 View,没有 onCreate/onResume。源码注释明确给出 View 生命周期与 Activity 的对应关系:
1 2 3 initContent -> Activity.onPreCreate constructor/init -> Activity.onCreate onFinishInflate -> Activity.onPostCreate
onFinishInflate(对应 onCreate 末尾):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Override protected void onFinishInflate () { super .onFinishInflate(); mAH.get(SEARCH).setup(mSearchRecyclerView, itemInfo -> false ); rebindAdapters(true ); float cornerRadius = Themes.getDialogCornerRadius(getContext()); mBottomSheetCornerRadii = new float []{ cornerRadius, cornerRadius, 0 , 0 , 0 , 0 , 0 , 0 }; updateBackgroundVisibility(mActivityContext.getDeviceProfile()); mSearchUiManager.initializeSearch(this ); }
onAttachedToWindow(对应 onResume 首次):
1 2 3 4 5 6 7 8 9 10 11 @Override protected void onAttachedToWindow () { super .onAttachedToWindow(); if (isSearchBarFloating()) { mActivityContext.getDragLayer().addView(mSearchContainer); mSearchUiDelegate.onInitializeSearchBar(); } mActivityContext.addOnDeviceProfileChangeListener(this ); }
注意 onResume 的真正入口不在 View 里,而在 Launcher.onResume() → StateManager → AllAppsTransitionController.setState()。View 自身只负责 inflate 和绑定,状态切换由 LauncherState 体系驱动。
2.4 rebindAdapters 与 Tab 切换 rebindAdapters 决定显示单 RV 还是双 Tab(个人/工作),是整个容器的布局重建入口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 protected void rebindAdapters (boolean force) { if (mSearchTransitionController.isRunning()) { mRebindAdaptersAfterSearchAnimation = true ; return ; } updateSearchResultsVisibility(); boolean showTabs = shouldShowTabs(); if (showTabs == mUsingTabs && !force) { return ; } replaceAppsRVContainer(showTabs); mUsingTabs = showTabs; mAllAppsStore.unregisterIconContainer(mAH.get(AdapterHolder.MAIN).mRecyclerView); mAllAppsStore.unregisterIconContainer(mAH.get(AdapterHolder.WORK).mRecyclerView); mAllAppsStore.unregisterIconContainer(mAH.get(AdapterHolder.SEARCH).mRecyclerView); if (mUsingTabs) { mainRecyclerView = (AllAppsRecyclerView) mViewPager.getChildAt(0 ); workRecyclerView = (AllAppsRecyclerView) mViewPager.getChildAt(1 ); mAH.get(AdapterHolder.MAIN).setup(mainRecyclerView, mPersonalMatcher); mAH.get(AdapterHolder.WORK).setup(workRecyclerView, mWorkManager.getItemInfoMatcher()); } else { mainRecyclerView = findViewById(R.id.apps_list_view); mAH.get(AdapterHolder.WORK).mRecyclerView = null ; mAH.get(AdapterHolder.MAIN).setup(mainRecyclerView, mPersonalMatcher); } setUpCustomRecyclerViewPool(mainRecyclerView, workRecyclerView, mActivityContext.getActivityComponent().getSharedAppsPool()); setupHeader(); mAllAppsStore.registerIconContainer(mAH.get(AdapterHolder.MAIN).mRecyclerView); mAllAppsStore.registerIconContainer(mAH.get(AdapterHolder.WORK).mRecyclerView); mAllAppsStore.registerIconContainer(mAH.get(AdapterHolder.SEARCH).mRecyclerView); }
replaceAppsRVContainer 负责实际替换布局:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 private void replaceAppsRVContainer (boolean showTabs) { for (int i = AdapterHolder.MAIN; i <= AdapterHolder.WORK; i++) { AdapterHolder adapterHolder = mAH.get(i); if (adapterHolder.mRecyclerView != null ) { adapterHolder.mRecyclerView.setLayoutManager(null ); adapterHolder.mRecyclerView.setAdapter(null ); } } View oldView = getAppsRecyclerViewContainer(); int index = indexOfChild(oldView); removeView(oldView); int layout = showTabs ? R.layout.all_apps_tabs : R.layout.all_apps_rv_layout; final View rvContainer = getLayoutInflater().inflate(layout, this , false ); addView(rvContainer, index); if (showTabs) { mViewPager = (AllAppsPagedView) rvContainer; mViewPager.initParentViews(this ); mViewPager.getPageIndicator().setOnActivePageChangedListener(this ); } else { mWorkManager.detachWorkUtilityViews(); mViewPager = null ; } }
shouldShowTabs() 仅返回 mHasWorkApps:
1 2 3 4 public boolean shouldShowTabs () { return mHasWorkApps; }
mHasWorkApps 在 onAppsUpdated 中扫描得出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @VisibleForTesting public void onAppsUpdated () { mHasWorkApps = Stream.of(mAllAppsStore.getApps()) .anyMatch(mWorkManager.getItemInfoMatcher()); mHasPrivateApps = Stream.of(mAllAppsStore.getApps()) .anyMatch(mPrivateProfileManager.getItemInfoMatcher()); if (!isSearching()) { rebindAdapters(); } if (mHasWorkApps) mWorkManager.reset(); if (mHasPrivateApps) mPrivateProfileManager.reset(); mActivityContext.getStatsLogManager().logger() .withCardinality(mAllAppsStore.getApps().length) .log(LAUNCHER_ALLAPPS_COUNT); }
Tab 切换(个人 ↔ 工作)由 AllAppsPagedView 触发 onActivePageChanged:
1 2 3 4 5 6 7 8 9 10 11 12 13 @Override public void onActivePageChanged (int currentActivePage) { if (mSearchTransitionController.isRunning()) return ; if (currentActivePage != SEARCH) mActivityContext.hideKeyboard(); if (mAH.get(currentActivePage).mRecyclerView != null ) { mAH.get(currentActivePage).mRecyclerView.bindFastScrollbar(mFastScroller, ALL_APPS_SCROLLER); } mHeader.setActiveRV(currentActivePage); reset(true , !isSearching() , false ); mWorkManager.onActivePageChanged(currentActivePage); }
设计意图:快速滚动条 mFastScroller 是全局唯一 的,Tab 切换时必须重新 bindFastScrollbar 到新 RV。Header 也是唯一的,需 setActiveRV 让它知道当前在算谁的 section 高亮。
2.5 RecycledViewPool 共享 1 2 3 4 5 6 7 8 9 10 11 private static void setUpCustomRecyclerViewPool ( @NonNull AllAppsRecyclerView mainRecyclerView, @Nullable AllAppsRecyclerView workRecyclerView, @NonNull AllAppsRecyclerViewPool recycledViewPool) { mainRecyclerView.setRecycledViewPool(recycledViewPool); if (workRecyclerView != null ) { workRecyclerView.setRecycledViewPool(recycledViewPool); } mainRecyclerView.updatePoolSize(); }
updatePoolSize 限定每种 ViewType 的缓存上限:
1 2 3 4 5 6 protected void updatePoolSize () { RecyclerView.RecycledViewPool pool = getRecycledViewPool(); pool.setMaxRecycledViews(AllAppsGridAdapter.VIEW_TYPE_EMPTY_SEARCH, 1 ); pool.setMaxRecycledViews(AllAppsGridAdapter.VIEW_TYPE_ALL_APPS_DIVIDER, 1 ); }
为什么共享:个人页和工作页的图标 ViewType 相同(VIEW_TYPE_ICON),布局完全一致。共享池让两个 RV 互相复用回收的 ViewHolder,减少 inflate 次数。注释解释:AllApps RV 的可见性从 INVISIBLE 改成 GONE,无法靠 layout pass 自动更新池大小,故手动 updatePoolSize。
2.6 LauncherAllAppsContainerView 的覆写 1 2 3 4 5 @Override public boolean isInAllApps () { return mActivityContext.getStateManager().isInStableState(LauncherState.ALL_APPS); }
isInAllApps() 决定触摸事件是否拦截给快速滚动条。基类默认返回 true(Taskbar 场景 All Apps 总是可见),Launcher 子类则查询 StateManager,确保在 Workspace 态不拦截滚动事件。
浮动搜索条的 margin 也由 LauncherState 决定:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Override public int getFloatingSearchBarRestingMarginBottom () { if (!isSearchBarFloating()) return super .getFloatingSearchBarRestingMarginBottom(); Launcher launcher = mActivityContext; StateManager<LauncherState, Launcher> stateManager = launcher.getStateManager(); int currentStateMarginBottom = stateManager.getCurrentStableState() .getFloatingSearchBarRestingMarginBottom(launcher); int targetStateMarginBottom = -1 ; if (stateManager.isInTransition() && stateManager.getTargetState() != null ) { targetStateMarginBottom = stateManager.getTargetState() .getFloatingSearchBarRestingMarginBottom(launcher); if (targetStateMarginBottom < 0 ) return targetStateMarginBottom; } return Math.max(targetStateMarginBottom, currentStateMarginBottom); }
为什么取较大值:过渡动画期间搜索条位置应保持稳定,取 max 确保它停在两者中更高的位置,避免在状态切换中途搜索条突兀下移。
面试深问
为什么 AdapterHolder 用 Arrays.asList(null, null, null) 而不是 new AdapterHolder[3]? Arrays.asList 返回固定大小 List,元素可 set 替换但不可 add/remove,语义上正好表达”三个固定槽位”。数组无法直接用 forEach,且 Arrays.asList 的不可变大小特性防止误扩容。
SEARCH 的 AdapterHolder 为什么构造时传 appsStore = null? 搜索结果由 setSearchResults() 外部推送,不应监听全局 App 集合变化。若监听 Store,用户增删 app 时会清空当前搜索结果,体验断裂。null 让 AlphabeticalAppsList 不注册监听,保持搜索结果的独立性。
rebindAdapters 为什么先 unregisterIconContainer 再 register? RV 重建后旧的 mRecyclerView 引用失效,若不先解绑,Store 的图标更新仍会广播到已废弃的 RV,导致 updateNotificationDots 等回调操作已分离的 View。先解绑再重绑,保证图标更新只作用于当前活跃 RV。
3. 数据中枢:AllAppsStore 3.1 数据结构与初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @ActivityContextSingleton public class AllAppsStore { private static final String TAG = "AllAppsStore" ; public static final int DEFER_UPDATES_NEXT_DRAW = 1 << 0 ; public static final int DEFER_UPDATES_TEST = 1 << 1 ; private PackageUserKey mTempKey = new PackageUserKey (null , null ); private AppInfo mTempInfo = new AppInfo (); private @NonNull AppInfo[] mApps = EMPTY_ARRAY; private final List<OnUpdateListener> mUpdateListeners = new CopyOnWriteArrayList <>(); private final ArrayList<ViewGroup> mIconContainers = new ArrayList <>(); private Map<PackageUserKey, Integer> mPackageUserKeytoUidMap = Collections.emptyMap(); private int mModelFlags; private int mDeferUpdatesFlags = 0 ; private boolean mUpdatePending = false ; @Inject AllAppsStore() { }
@ActivityContextSingleton 让每个 ActivityContext(如 Launcher 实例)共享同一个 Store 实例,mActivityContext.getActivityComponent().getAppsStore() 在容器构造时取出。mApps 是核心数组,要求按 COMPONENT_KEY_COMPARATOR 排序 ,这是 getApp(ComponentKey) 能用 Arrays.binarySearch 的前提。
为什么用数组而不是 List:
数组内存连续,CPU 缓存友好,遍历 200+ 个 app 时性能更好。
二分查找 Arrays.binarySearch 直接可用,无需装箱。
数组引用不可变(只能整体替换 mApps = apps),天然线程安全快照语义。
CopyOnWriteArrayList 承载监听器的原因:监听器注册/注销发生在 UI 线程(rebindAdapters),回调发生在 Model 推送线程,CopyOnWriteArrayList 的读无锁、写复制,避免回调时并发修改异常。
3.2 数据来源链路:AllAppsList → AllAppsStore 数据从后台 Model 推送的入口:
1 2 3 4 5 6 7 8 public void setApps (@Nullable AppInfo[] apps, int flags, Map<PackageUserKey, Integer> map) { mApps = apps == null ? EMPTY_ARRAY : apps; Log.d(TAG, "setApps: apps.length=" + mApps.length); mModelFlags = flags; notifyUpdate(); mPackageUserKeytoUidMap = map; }
apps 在后台已由 AllAppsList 排序(COMPONENT_KEY_COMPARATOR)并预计算 sectionName:
1 2 3 4 private AlphabeticIndexCompat mIndex;mIndex = new AlphabeticIndexCompat (LocaleList.getDefault()); info.sectionName = mIndex.computeSectionName(info.title);
notifyUpdate 是广播入口,带延迟逻辑:
1 2 3 4 5 6 7 8 9 10 private void notifyUpdate () { if (mDeferUpdatesFlags != 0 ) { mUpdatePending = true ; return ; } for (OnUpdateListener listener : mUpdateListeners) { listener.onAppsUpdated(); } }
3.3 延迟更新防抖(Defer Updates) 1 2 3 4 5 6 7 8 9 10 11 12 public void enableDeferUpdates (int flag) { mDeferUpdatesFlags |= flag; } public void disableDeferUpdates (int flag) { mDeferUpdatesFlags &= ~flag; if (mDeferUpdatesFlags == 0 && mUpdatePending) { notifyUpdate(); mUpdatePending = false ; } }
为什么需要延迟更新:
Launcher 在状态切换动画期间(如从 All Apps 滑回 Workspace),若中途收到 setApps(比如后台刚装完一个 app),立即重建列表会导致动画卡顿或视觉跳变。
DEFER_UPDATES_NEXT_DRAW 把更新推迟到下一帧绘制完成后,确保当前动画帧不受干扰。
DEFER_UPDATES_TEST 给测试用,避免更新与断言竞态。
位掩码而非布尔值:多个延迟来源(动画、测试、将来扩展)可能同时存在,用掩码可独立置位/清位,互不干扰。disableDeferUpdatesSilently 清位但不触发通知,用于异常恢复。
3.4 getApp:二分查找 1 2 3 4 5 6 7 8 9 10 11 12 13 @Nullable public AppInfo getApp (ComponentKey key) { return getApp(key, COMPONENT_KEY_COMPARATOR); } @Nullable public AppInfo getApp (ComponentKey key, Comparator<AppInfo> comparator) { mTempInfo.componentName = key.componentName; mTempInfo.user = key.user; int index = Arrays.binarySearch(mApps, mTempInfo, comparator); return index < 0 ? null : mApps[index]; }
mTempInfo 是成员变量复用,避免每次查找 new 对象。这是合法的,因为 getApp 只在 UI 线程调用,无并发问题。
COMPONENT_KEY_COMPARATOR 在 AppInfo 中定义,按 componentName + user 排序,保证唯一性——同一个组件在不同 user(如工作账户)下是两条记录。
3.5 图标更新:updateAllIcons Store 不直接操作 Adapter,而是通过注册的 mIconContainers(即各 RV)广播:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public void updateNotificationDots (Predicate<PackageUserKey> updatedDots) { updateAllIcons((child) -> { if (child.getTag() instanceof ItemInfo) { ItemInfo info = (ItemInfo) child.getTag(); if (mTempKey.updateFromItemInfo(info) && updatedDots.test(mTempKey)) { child.applyDotState(info, true ); } } }); } private void updateAllIcons (Consumer<BubbleTextView> action) { for (int i = mIconContainers.size() - 1 ; i >= 0 ; i--) { ViewGroup parent = mIconContainers.get(i); int childCount = parent.getChildCount(); for (int j = 0 ; j < childCount; j++) { View child = parent.getChildAt(j); if (child instanceof BubbleTextView) { action.accept((BubbleTextView) child); } } } }
为什么倒序遍历:允许 action 内部触发 unregisterIconContainer(如 RV 被销毁),倒序遍历避免 ConcurrentModificationException。
为什么不通过 Adapter.notifyItemChanged:通知圆点、进度条这类纯视觉更新 ,走 RV 的 notify 会触发完整 rebind(包括 DiffUtil),开销大且可能打断滚动。直接遍历可见的 BubbleTextView 调 applyDotState 是最小开销路径。
面试深问
mApps 为什么要预排序?直接 List.contains 不行吗?getApp(ComponentKey) 在拖拽、点击、通知更新时被高频调用(每次点击图标都要查 AppInfo)。二分查找 O(log n),线性查找 O(n),200 个 app 时差 8 倍。预排序是空间换时间的标准做法。
updateAllIcons 为什么遍历 RV 的子 View 而不是 Adapter 的数据集? Adapter 数据集是全量,但屏幕上只有十几个图标可见。遍历 RV 子 View 只更新可见图标,未可见的会在滚动 rebind 时自然应用新状态。这是”按需更新”的性能优化。
延迟更新的 mUpdatePending 为什么用布尔而不是计数? 延迟期间可能有多次 setApps(如批量安装),但语义上只需要知道”是否有待处理更新”,不需要知道几次。多次更新合并为一次 notifyUpdate,避免重复刷新列表。
4. 排序与分组引擎:AlphabeticalAppsList 4.1 字段与初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public class AlphabeticalAppsList implements AllAppsStore .OnUpdateListener { private final ActivityContext mActivityContext; private final List<AppInfo> mApps = new ArrayList <>(); private final List<AppInfo> mPrivateApps = new ArrayList <>(); @Nullable private final AllAppsStore mAllAppsStore; private int mAccessibilityResultsCount = 0 ; private final ArrayList<AdapterItem> mAdapterItems = new ArrayList <>(); private final List<FastScrollSectionInfo> mFastScrollerSections = new ArrayList <>(); private final ArrayList<AdapterItem> mSearchResults = new ArrayList <>(); private BaseAllAppsAdapter mAdapter; private AppInfoComparator mAppNameComparator; private int mNumAppsPerRowAllApps; private int mNumAppRowsInAdapter; private Predicate<ItemInfo> mItemFilter; public AlphabeticalAppsList (ActivityContext activityContext, @Nullable AllAppsStore appsStore, WorkProfileManager workProfileManager, PrivateProfileManager privateProfileManager) { mAllAppsStore = appsStore; mActivityContext = activityContext; Context context = activityContext.asContext(); mAppNameComparator = new AppInfoComparator (context); mWorkProviderManager = workProfileManager; mPrivateProviderManager = privateProfileManager; mNumAppsPerRowAllApps = mActivityContext.getDeviceProfile().numShownAllAppsColumns; if (mAllAppsStore != null ) { mAllAppsStore.addUpdateListener(this ); } mPrivateProfileAppScrollerBadge = new SpannableString (" " ); mPrivateProfileAppScrollerBadge.setSpan(new ImageSpan (context, R.drawable.ic_private_profile_app_scroller_badge, ImageSpan.ALIGN_CENTER), 0 , 1 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
4.2 onAppsUpdated 完整流程 onAppsUpdated 是核心重建逻辑,Store 变更时触发:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 @Override public void onAppsUpdated () { if (mAllAppsStore == null || (mPrivateProviderManager != null && mPrivateProviderManager.getAnimationRunning())) { return ; } mApps.clear(); mPrivateApps.clear(); Stream<AppInfo> appSteam = Stream.of(mAllAppsStore.getApps()).filter( info -> !isPrivateSpaceApp(info)); Stream<AppInfo> privateAppStream = Stream.of(mAllAppsStore.getApps()); if (!hasSearchResults() && mItemFilter != null ) { appSteam = appSteam.filter(mItemFilter); if (mPrivateProviderManager != null ) { privateAppStream = privateAppStream .filter(mPrivateProviderManager.getItemInfoMatcher()); } } appSteam = appSteam.sorted(mAppNameComparator); privateAppStream = privateAppStream.sorted(mAppNameComparator); Locale curLocale = mActivityContext.asContext().getResources().getConfiguration().locale; boolean localeRequiresSectionSorting = curLocale.equals(Locale.SIMPLIFIED_CHINESE); if (localeRequiresSectionSorting) { appSteam = appSteam.collect(Collectors.groupingBy( info -> info.sectionName, () -> new TreeMap <>(new LabelComparator ()), Collectors.toCollection(ArrayList::new ))) .values().stream().flatMap(ArrayList::stream); } appSteam.forEachOrdered(mApps::add); privateAppStream.forEachOrdered(mPrivateApps::add); if (mSearchResults.isEmpty()) { updateAdapterItems(); } }
4.3 sectionName 的计算:ICU AlphabeticIndex sectionName 在后台 AllAppsList 中用 AlphabeticIndexCompat 计算:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 public class AlphabeticIndexCompat { private static final String MID_DOT = "\u2219" ; private final String mDefaultMiscLabel; private final AlphabeticIndex.ImmutableIndex mBaseIndex; public AlphabeticIndexCompat (LocaleList locales) { int localeCount = locales.size(); Locale primaryLocale = localeCount == 0 ? Locale.ENGLISH : locales.get(0 ); AlphabeticIndex indexBuilder = new AlphabeticIndex (primaryLocale); for (int i = 1 ; i < localeCount; i++) { indexBuilder.addLabels(locales.get(i)); } indexBuilder.addLabels(Locale.ENGLISH); mBaseIndex = indexBuilder.buildImmutableIndex(); if (primaryLocale.getLanguage().equals(Locale.JAPANESE.getLanguage())) { mDefaultMiscLabel = "\u4ed6" ; } else { mDefaultMiscLabel = MID_DOT; } } public String computeSectionName (@NonNull CharSequence cs) { String s = Utilities.trim(cs); String sectionName = mBaseIndex.getBucket(mBaseIndex.getBucketIndex(s)).getLabel(); if (Utilities.trim(sectionName).isEmpty() && s.length() > 0 ) { int c = s.codePointAt(0 ); boolean startsWithDigit = Character.isDigit(c); if (startsWithDigit) { return "#" ; } else if (Character.isLetter(c)) { return mDefaultMiscLabel; } else { return MID_DOT; } } return sectionName; } }
为什么用 ICU AlphabeticIndex 而不是自己写拼音库:
ICU(International Components for Unicode)是业界标准,覆盖所有 locale 的字母分桶规则。中文场景下,ICU 内置拼音索引(zh locale),能把”微信”归到 “W” 桶,无需自己维护拼音映射表。
自己写拼音库需要:汉字→拼音表(几万条)、多音字处理、声调、繁简转换。维护成本极高且容易出错。
ICU 的 addLabels(Locale.ENGLISH) 兜底:主 locale 无法归类时回退到英文桶,保证任何字符都有归属。
buildImmutableIndex 返回不可变索引,线程安全且查找 O(log buckets),性能远优于每次调用 Collator。
computeSectionName 的兜底逻辑:ICU 对某些字符(表情、组合符)可能返回空 label,此时手动归类——数字到 #、字母到 ·、符号到 ·。这保证快速滚动条上每个 app 都能定位到某个 section。
4.4 AppInfoComparator + LabelComparator + Collator 本地化排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class AppInfoComparator implements Comparator <AppInfo> { private final UserCache mUserManager; private final UserHandle mMyUser; private final LabelComparator mLabelComparator; public AppInfoComparator (Context context) { mUserManager = UserCache.INSTANCE.get(context); mMyUser = Process.myUserHandle(); mLabelComparator = new LabelComparator (); } @Override public int compare (AppInfo a, AppInfo b) { int result = mLabelComparator.compare(getSortingTitle(a), getSortingTitle(b)); if (result != 0 ) return result; result = a.componentName.compareTo(b.componentName); if (result != 0 ) return result; if (mMyUser.equals(a.user)) return -1 ; Long aUserSerial = mUserManager.getSerialNumberForUser(a.user); Long bUserSerial = mUserManager.getSerialNumberForUser(b.user); return aUserSerial.compareTo(bUserSerial); } private String getSortingTitle (AppInfo info) { if (!TextUtils.isEmpty(info.appTitle)) return info.appTitle.toString(); if (info.title != null ) return info.title.toString(); return "" ; } }
LabelComparator 的核心是 Collator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class LabelComparator implements Comparator <String> { private final Collator mCollator = Collator.getInstance(); @Override public int compare (String titleA, String titleB) { boolean aStartsWithLetter = (titleA.length() > 0 ) && Character.isLetterOrDigit(titleA.codePointAt(0 )); boolean bStartsWithLetter = (titleB.length() > 0 ) && Character.isLetterOrDigit(titleB.codePointAt(0 )); if (aStartsWithLetter && !bStartsWithLetter) return -1 ; else if (!aStartsWithLetter && bStartsWithLetter) return 1 ; return mCollator.compare(titleA, titleB); } }
三层比较的设计意图:
LabelComparator:本地化字符串比较。Collator.getInstance() 在 Android N+ 用 ICU 实现,对中文按拼音排序(”百度” < “淘宝” 因为 B < T),对德文按 ä 排序规则,完全跟随系统 locale。
非字母数字降权:以符号/表情开头的 app(如 “♡Chat”)排到列表末尾,避免污染字母序。
componentName 兜底:两个 app 同名(如两个浏览器都叫”浏览器”)时,按包名确定性排序,保证顺序稳定。
user 优先级:同名同包名跨 user(个人与工作账户的同一个 app),当前用户的排前面。
4.5 简体中文 section 归并特殊处理 回到 onAppsUpdated 的中文分支:
1 2 3 4 5 6 7 8 9 boolean localeRequiresSectionSorting = curLocale.equals(Locale.SIMPLIFIED_CHINESE);if (localeRequiresSectionSorting) { appSteam = appSteam.collect(Collectors.groupingBy( info -> info.sectionName, () -> new TreeMap <>(new LabelComparator ()), Collectors.toCollection(ArrayList::new ))) .values().stream().flatMap(ArrayList::stream); }
为什么中文要特殊归并:
ICU 对简体中文生成的 sectionName 可能包含拼音桶 (如 “B”、”W”)和笔画/部首桶 (如某些字归到非字母桶)。如果直接用 sectionName 顺序,同一个字母桶的 app 可能被非字母桶打断。
Collectors.groupingBy 按 sectionName 聚合,TreeMap(LabelComparator) 保证组间按 LabelComparator(Collator 拼音序)排序,组内保持 AppInfoComparator 已排好的顺序。
展平后,所有 “B” 桶的 app 连续排列,”W” 桶的 app 连续排列,快速滚动的 section 跳转才能定位到连续区块。
其他语言不需要:英文 locale 下 ICU 的 sectionName 已经是连续字母(A/B/C…),AppInfoComparator 排序后天然连续,无需二次归并。
4.6 updateAdapterItems:生成最终列表与 DiffUtil 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 public void updateAdapterItems () { List<AdapterItem> oldItems = new ArrayList <>(mAdapterItems); mFastScrollerSections.clear(); mAdapterItems.clear(); mAccessibilityResultsCount = 0 ; if (hasSearchResults()) { mAdapterItems.addAll(mSearchResults); } else { int position = 0 ; boolean addApps = true ; if (mWorkProviderManager != null ) { position += mWorkProviderManager.addWorkItems(mAdapterItems); addApps = mWorkProviderManager.shouldShowWorkApps(); } if (addApps) { if (position == 1 ) { mFastScrollerSections.add(new FastScrollSectionInfo ( mActivityContext.asContext().getResources() .getString(R.string.work_profile_edu_section), 0 )); } position = addAppsWithSections(mApps, position); } if (Flags.enablePrivateSpace()) { position = addPrivateSpaceItems(position); } if (!mFastScrollerSections.isEmpty()) { mAdapterItems.add(new AdapterItem (VIEW_TYPE_BOTTOM_VIEW_TO_SCROLL_TO)); mFastScrollerSections.add(new FastScrollSectionInfo ( mFastScrollerSections.get(mFastScrollerSections.size() - 1 ).sectionName, position++)); } } mAccessibilityResultsCount = (int ) mAdapterItems.stream() .filter(AdapterItem::isCountedForAccessibility).count(); if (mNumAppsPerRowAllApps != 0 ) { int numAppsInSection = 0 , numAppsInRow = 0 , rowIndex = -1 ; for (AdapterItem item : mAdapterItems) { item.rowIndex = 0 ; if (BaseAllAppsAdapter.isDividerViewType(item.viewType) || BaseAllAppsAdapter.isPrivateSpaceHeaderView(item.viewType) || BaseAllAppsAdapter.isPrivateSpaceSysAppsDividerView(item.viewType)) { numAppsInSection = 0 ; } else if (BaseAllAppsAdapter.isIconViewType(item.viewType)) { if (numAppsInSection % mNumAppsPerRowAllApps == 0 ) { numAppsInRow = 0 ; rowIndex++; } item.rowIndex = rowIndex; item.rowAppIndex = numAppsInRow; numAppsInSection++; numAppsInRow++; } } mNumAppRowsInAdapter = rowIndex + 1 ; } if (mAdapter != null ) { DiffUtil.calculateDiff(new MyDiffCallback (oldItems, mAdapterItems), false ) .dispatchUpdatesTo(mAdapter); } }
addAppsWithSections 是 section 创建的核心:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 private int addAppsWithSections (List<AppInfo> appList, int startPosition) { String lastSectionName = null ; int position = startPosition; for (int i = 0 ; i < appList.size(); i++) { AppInfo info = appList.get(i); if (hasPrivateApps) { mAdapterItems.add(AdapterItem.asAppWithDecorationInfo(info, new SectionDecorationInfo (mActivityContext.asContext(), getRoundRegions(i, appList.size())), isPrivateSpaceApp(info))); } else { mAdapterItems.add(AdapterItem.asApp(info)); } String sectionName = info.sectionName; if (!sectionName.equals(lastSectionName)) { lastSectionName = sectionName; FastScrollSectionInfo sectionInfo = new FastScrollSectionInfo ( usePrivateAppScrollerBadge ? mPrivateProfileAppScrollerBadge : sectionName, position); mFastScrollerSections.add(sectionInfo); } position++; } return position; }
设计要点:sectionName 在 AppInfo 中预计算(后台 ICU),这里只做”与前一个不同则新建 section”的连续判断,O(n) 单次遍历。FastScrollSectionInfo.position 记录的是 adapter position,快速滚动时直接 scrollToPosition。
getRoundRegions 决定私密区 app 的圆角:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @VisibleForTesting int getRoundRegions (int appIndex, int appListSize) { int numberOfAppRows = (int ) Math.ceil((double ) appListSize / mNumAppsPerRowAllApps); int roundRegion = ROUND_NOTHING; if ((appIndex / mNumAppsPerRowAllApps) == numberOfAppRows - 1 ) { if ((appIndex % mNumAppsPerRowAllApps) == 0 ) { roundRegion = ROUND_BOTTOM_LEFT; } else if ((appIndex % mNumAppsPerRowAllApps) == mNumAppsPerRowAllApps - 1 ) { roundRegion = ROUND_BOTTOM_RIGHT; } if (appIndex == appListSize - 1 ) { roundRegion |= ROUND_BOTTOM_RIGHT; } } return roundRegion; }
4.7 DiffUtil 增量刷新 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 private static class MyDiffCallback extends DiffUtil .Callback { private final List<AdapterItem> mOldList; private final List<AdapterItem> mNewList; @Override public boolean areItemsTheSame (int oldItemPosition, int newItemPosition) { return mOldList.get(oldItemPosition).isSameAs(mNewList.get(newItemPosition)); } @Override public boolean areContentsTheSame (int oldItemPosition, int newItemPosition) { return mOldList.get(oldItemPosition).isContentSame(mNewList.get(newItemPosition)); } }
AdapterItem.isSameAs 与 isContentSame:
1 2 3 4 5 6 7 8 public boolean isSameAs (AdapterItem other) { return (other.viewType == viewType) && (other.getClass() == getClass()); } public boolean isContentSame (AdapterItem other) { return itemInfo == null && other.itemInfo == null ; }
为什么用 DiffUtil 而不是 notifyDataSetChanged:
notifyDataSetChanged 会重建所有可见 ViewHolder,触发全量 onBindViewHolder,200 个 app 时明显卡顿。
DiffUtil 用 Eugene W. Myers 差分算法(O(ND))计算最小变更集,只对变化的 item 发 notifyItemInserted/Removed/Changed,未变化的复用旧 ViewHolder。
calculateDiff(callback, false) 第二参数 false 表示不检测移动(move),因为 app 列表是排序后的,移动等价于删除+插入,省去移动检测开销。
areItemsTheSame 只比 viewType 和 getClass(),不比 itemInfo:这是 DiffUtil 的”身份”判断,相同身份才会进一步比 areContentsTheSame。两个 VIEW_TYPE_ICON 的 item 被视为同一身份(因为都是图标行),内容比较 itemInfo == null 这里实际是宽松判断——Launcher 默认 setItemAnimator(null)(见 AdapterHolder.setup),不做 item 动画,DiffUtil 主要用于精确的 insert/remove 定位。
1 2 3 4 5 6 7 8 9 10 11 public static class FastScrollSectionInfo { public final CharSequence sectionName; public final int position; public int id = -1 ; public FastScrollSectionInfo (CharSequence sectionName, int position) { this .sectionName = sectionName; this .position = position; } }
sectionName 是 CharSequence 而非 String,因为私密空间的 section 用 ImageSpan(图标徽章)而非文字。id 在 setLettersToScrollLayout 中由 View.generateViewId() 赋值,用于 ConstraintLayout 的链式约束。
面试深问
为什么 sectionName 在后台算好而不是前台 onAppsUpdated 里算? ICU AlphabeticIndex.getBucketIndex 虽然是 O(log),但 200 个 app 累计仍是可观开销。后台 Model 线程算好后存入 AppInfo.sectionName,前台只做比较和分组,UI 线程零 ICU 调用。这也是 sectionName 是 AppInfo 字段而非运行时计算的原因。
DiffUtil 的 areContentsTheSame 为什么只判 itemInfo == null,不比 AppInfo 内容? Launcher setItemAnimator(null),不依赖内容变更触发局部刷新动画。DiffUtil 的主要价值是计算 insert/remove 的精确位置,避免全量刷新。内容变化(如标题改变)由 Store 的图标更新路径(updateAllIcons)单独处理,不走 DiffUtil。
简体中文归并为什么用 TreeMap(LabelComparator) 而不是 HashMap? HashMap 无序,展平后 section 顺序不可预测。TreeMap 按 key 排序,key 是 sectionName,用 LabelComparator(Collator)排序保证拼音字母序(B 在 W 前)。展平后连续的字母区块才能正确支持快速滚动定位。
5. 适配器:BaseAllAppsAdapter 与 AllAppsGridAdapter 5.1 位掩码 ViewType 体系 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public static final int VIEW_TYPE_ICON = 1 << 1 ; public static final int VIEW_TYPE_EMPTY_SEARCH = 1 << 2 ; public static final int VIEW_TYPE_ALL_APPS_DIVIDER = 1 << 3 ; public static final int VIEW_TYPE_WORK_EDU_CARD = 1 << 4 ; public static final int VIEW_TYPE_WORK_DISABLED_CARD = 1 << 5 ; public static final int VIEW_TYPE_PRIVATE_SPACE_HEADER = 1 << 6 ; public static final int VIEW_TYPE_PRIVATE_SPACE_SYS_APPS_DIVIDER = 1 << 7 ; public static final int VIEW_TYPE_BOTTOM_VIEW_TO_SCROLL_TO = 1 << 8 ; public static final int VIEW_TYPE_PRIVATE_SPACE_APP_ICON = 1 << 9 ; public static final int NEXT_ID = 10 ; public static final int VIEW_TYPE_MASK_DIVIDER = VIEW_TYPE_ALL_APPS_DIVIDER;public static final int VIEW_TYPE_MASK_ICON = VIEW_TYPE_ICON | VIEW_TYPE_PRIVATE_SPACE_APP_ICON;public static final int VIEW_TYPE_MASK_PRIVATE_SPACE_HEADER = VIEW_TYPE_PRIVATE_SPACE_HEADER;public static final int VIEW_TYPE_MASK_PRIVATE_SPACE_SYS_APPS_DIVIDER = VIEW_TYPE_PRIVATE_SPACE_SYS_APPS_DIVIDER;
为什么用位掩码(1 << n)而不是连续整数(0,1,2,3…):
单个 ViewType 是 2 的幂,可以用按位或 组合成掩码。VIEW_TYPE_MASK_ICON = VIEW_TYPE_ICON | VIEW_TYPE_PRIVATE_SPACE_APP_ICON 一次判断”是不是任何图标”。
isIconViewType 等判断用 (viewType & mask) != 0,一次按位运算覆盖多个类型。
新增类型只需 1 << NEXT_ID,不破坏既有掩码语义。
RecyclerView 的 getItemViewType 返回 int,位掩码天然兼容。
判断方法:
1 2 3 4 5 6 7 8 9 10 public static boolean isDividerViewType (int viewType) { return isViewType(viewType, VIEW_TYPE_MASK_DIVIDER); } public static boolean isIconViewType (int viewType) { return isViewType(viewType, VIEW_TYPE_MASK_ICON); } protected static boolean isViewType (int viewType, int viewTypeMask) { return (viewType & viewTypeMask) != 0 ; }
5.2 AdapterItem 包装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public static class AdapterItem { public final int viewType; public int rowIndex; public int rowAppIndex; public AppInfo itemInfo = null ; public SectionDecorationInfo decorationInfo = null ; public AdapterItem (int viewType) { this .viewType = viewType; } public static AdapterItem asApp (AppInfo appInfo) { AdapterItem item = new AdapterItem (VIEW_TYPE_ICON); item.itemInfo = appInfo; return item; } public static AdapterItem asAppWithDecorationInfo (AppInfo appInfo, SectionDecorationInfo decorationInfo, boolean isPrivateSpaceApp) { AdapterItem item = new AdapterItem (isPrivateSpaceApp ? VIEW_TYPE_PRIVATE_SPACE_APP_ICON : VIEW_TYPE_ICON); item.itemInfo = appInfo; item.decorationInfo = decorationInfo; return item; } protected boolean isCountedForAccessibility () { return viewType == VIEW_TYPE_ICON; } }
工厂方法的设计意图:构造 AdapterItem 只通过 asApp / asAppWithDecorationInfo,保证 viewType 与 itemInfo 的搭配正确(图标类必有 itemInfo,header 类可有可无)。VIEW_TYPE_PRIVATE_SPACE_APP_ICON 与 VIEW_TYPE_ICON 分离,因为私密空间图标的点击行为不同(弹 PopupContainerWithArrow 而非直接启动)。
5.3 onCreateViewHolder / onBindViewHolder 类型分发 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 @Override public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) { switch (viewType) { case VIEW_TYPE_ICON: return new ViewHolder (getIconOnCreateSetup(parent)); case VIEW_TYPE_PRIVATE_SPACE_APP_ICON: BubbleTextView icon = getIconOnCreateSetup(parent); icon.setOnClickListener(v -> PopupContainerWithArrow.showForPrivateSpaceApp(icon)); icon.setOnLongClickListener(v -> { PopupContainerWithArrow.showForPrivateSpaceApp(icon); return true ; }); return new ViewHolder (icon); case VIEW_TYPE_EMPTY_SEARCH: return new ViewHolder (mLayoutInflater.inflate(R.layout.all_apps_empty_search, parent, false )); case VIEW_TYPE_ALL_APPS_DIVIDER, VIEW_TYPE_PRIVATE_SPACE_SYS_APPS_DIVIDER: return new ViewHolder (mLayoutInflater.inflate(R.layout.private_space_divider, parent, false )); case VIEW_TYPE_WORK_EDU_CARD: return new ViewHolder (mLayoutInflater.inflate(R.layout.work_apps_edu, parent, false )); case VIEW_TYPE_WORK_DISABLED_CARD: return new ViewHolder (mLayoutInflater.inflate(R.layout.work_apps_paused, parent, false )); case VIEW_TYPE_PRIVATE_SPACE_HEADER: return new ViewHolder (mLayoutInflater.inflate(R.layout.private_space_header, parent, false )); case VIEW_TYPE_BOTTOM_VIEW_TO_SCROLL_TO: return new ViewHolder (new View (mActivityContext.asContext())); default : if (mAdapterProvider.isViewSupported(viewType)) { return mAdapterProvider.onCreateViewHolder(mLayoutInflater, parent, viewType); } throw new RuntimeException ("Unexpected view type" + viewType); } }
onBindViewHolder 按类型分发绑定逻辑:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 @Override public void onBindViewHolder (ViewHolder holder, int position) { holder.itemView.setVisibility(View.VISIBLE); switch (holder.getItemViewType()) { case VIEW_TYPE_PRIVATE_SPACE_APP_ICON: case VIEW_TYPE_ICON: { AdapterItem adapterItem = mApps.getAdapterItems().get(position); BubbleTextView icon = (BubbleTextView) holder.itemView; icon.reset(); icon.applyFromApplicationInfo(adapterItem.itemInfo); icon.setOnFocusChangeListener(mIconFocusListener); icon.configureMinimalPopup(holder.getItemViewType() == VIEW_TYPE_PRIVATE_SPACE_APP_ICON); PrivateProfileManager ppm = mApps.getPrivateProfileManager(); if (ppm != null ) { boolean isPrivateSpaceItem = ppm.isPrivateSpaceItem(adapterItem); if (icon.getAlpha() == 0 || icon.getAlpha() == 1 ) { icon.setAlpha(isPrivateSpaceItem && ppm.isStateTransitioning() && (ppm.isScrolling() || ppm.getReadyToAnimate()) && ppm.getCurrentState() == STATE_ENABLED ? 0 : 1 ); } if (ppm.getCurrentState() == STATE_DISABLED && isPrivateSpaceItem) { adapterItem.decorationInfo = null ; icon.setVisibility(GONE); } } break ; } case VIEW_TYPE_EMPTY_SEARCH: { AppInfo info = mApps.getAdapterItems().get(position).itemInfo; if (info != null ) { ((TextView) holder.itemView).setText(mActivityContext.asContext().getString( R.string.all_apps_no_search_results, info.title)); } break ; } case VIEW_TYPE_PRIVATE_SPACE_HEADER: { RelativeLayout psHeaderLayout = holder.itemView.findViewById(R.id.ps_header_layout); mApps.getPrivateProfileManager().bindPrivateSpaceHeaderViewElements(psHeaderLayout); AdapterItem adapterItem = mApps.getAdapterItems().get(position); int roundRegions = ROUND_TOP_LEFT | ROUND_TOP_RIGHT; if (mApps.getPrivateProfileManager().getCurrentState() == STATE_DISABLED) { roundRegions |= (ROUND_BOTTOM_LEFT | ROUND_BOTTOM_RIGHT); } adapterItem.decorationInfo = new SectionDecorationInfo (mActivityContext.asContext(), roundRegions); break ; } case VIEW_TYPE_BOTTOM_VIEW_TO_SCROLL_TO: case VIEW_TYPE_ALL_APPS_DIVIDER: case VIEW_TYPE_WORK_DISABLED_CARD: break ; case VIEW_TYPE_WORK_EDU_CARD: ((WorkEduCard) holder.itemView).setPosition(position); break ; default : if (mAdapterProvider.isViewSupported(holder.getItemViewType())) { mAdapterProvider.onBindView(holder, position); } } }
图标 View 的创建:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 private BubbleTextView getIconOnCreateSetup (ViewGroup parent) { int layout = mActivityContext.getDeviceProfile().inv.enableTwoLinesInAllApps ? R.layout.all_apps_icon_twoline : R.layout.all_apps_icon; BubbleTextView icon = (BubbleTextView) mLayoutInflater.inflate(layout, parent, false ); icon.setLongPressTimeoutFactor(1f ); icon.setOnFocusChangeListener(mIconFocusListener); icon.setOnClickListener(mOnIconClickListener); icon.setOnLongClickListener(mOnIconLongClickListener); icon.getLayoutParams().height = mActivityContext.getDeviceProfile().getAllAppsProfile().getCellHeightPx(); return icon; }
onFailedToRecycleView 强制返回 true:
1 2 3 4 5 @Override public boolean onFailedToRecycleView (ViewHolder holder) { return true ; }
设计意图:默认 RecyclerView 在 ViewHolder 状态不确定时会丢弃而非回收。这里强制回收,靠 icon.reset() 在 onBindViewHolder 清理状态,减少 inflate。
5.4 GridSpanSizer 跨列布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class GridSpanSizer extends GridLayoutManager .SpanSizeLookup { public GridSpanSizer () { super (); setSpanIndexCacheEnabled(true ); } @Override public int getSpanSize (int position) { int totalSpans = mGridLayoutMgr.getSpanCount(); List<AdapterItem> items = mApps.getAdapterItems(); if (position >= items.size()) return totalSpans; int viewType = items.get(position).viewType; if (isIconViewType(viewType)) { return totalSpans / mAppsPerRow; } else { if (mAdapterProvider.isViewSupported(viewType)) { return totalSpans / mAdapterProvider.getItemsPerRow(viewType, mAppsPerRow); } return totalSpans; } } }
totalSpans 的计算考虑插件的多列需求:
1 2 3 4 5 6 7 8 9 10 11 12 @Override public void setAppsPerRow (int appsPerRow) { mAppsPerRow = appsPerRow; int totalSpans = mAppsPerRow; for (int itemPerRow : mAdapterProvider.getSupportedItemsPerRowArray()) { if (totalSpans % itemPerRow != 0 ) { totalSpans *= itemPerRow; } } mGridLayoutMgr.setSpanCount(totalSpans); }
为什么取最小公倍数:假设每行 4 个 app,插件搜索结果想每行 2 个。若 totalSpans=4,插件的 getSpanSize 返回 4/2=2,app 返回 4/4=1,都能整除。但如果插件想每行 3 个,4%3!=0,totalSpans=12,app 返回 12/4=3,插件返回 12/3=4,都能整除。最小公倍数保证所有行类型都能正确分配 span。
setSpanIndexCacheEnabled(true):SpanSizeLookup.getSpanIndex 计算复杂(需回溯累加 span),缓存后避免每次布局重算。
5.5 AppsGridLayoutManager 的无障碍处理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class AppsGridLayoutManager extends ScrollableLayoutManager { @Override public void onInitializeAccessibilityEvent (AccessibilityEvent event) { super .onInitializeAccessibilityEvent(event); final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event); record.setItemCount(mApps.getNumFilteredApps()); record.setFromIndex(Math.max(0 , record.getFromIndex() - getRowsNotForAccessibility(record.getFromIndex()))); record.setToIndex(Math.max(0 , record.getToIndex() - getRowsNotForAccessibility(record.getToIndex()))); } private int getRowsNotForAccessibility (int adapterPosition) { List<AdapterItem> items = mApps.getAdapterItems(); int extraRows = 0 ; for (int i = 0 ; i <= adapterPosition && i < items.size(); i++) { if (!isViewType(items.get(i).viewType, VIEW_TYPE_MASK_ICON)) { extraRows++; } } return extraRows; } @Override protected int incrementTotalHeight (Adapter adapter, int position, int heightUntilLastPos) { AllAppsGridAdapter.AdapterItem item = mApps.getAdapterItems().get(position); return (isIconViewType(item.viewType) && item.rowAppIndex != 0 ) ? heightUntilLastPos : (heightUntilLastPos + mCachedSizes.get(item.viewType)); } }
设计意图:无障碍服务(TalkBack)报告”第 3 个项目,共 50 个”时,用户感知的是第 3 个 app,但实际 adapter position 可能因 header/divider 而偏移。getRowsNotForAccessibility 减去非 app 行数,让报告的索引与用户心智模型一致。incrementTotalHeight 避免同行 icon 重复累加高度,用于快速滚动的总高度估算。
面试深问
VIEW_TYPE_ICON 和 VIEW_TYPE_PRIVATE_SPACE_APP_ICON 为什么分开? 两者布局相同(都是 BubbleTextView),但点击行为不同:私密图标需先解锁才能启动,点击/长按都弹 PopupContainerWithArrow.showForPrivateSpaceApp。分开 ViewType 让 onCreateViewHolder 能挂不同监听器,且 VIEW_TYPE_MASK_ICON 掩码让两者共享同一套”是图标”的判断逻辑。
setSpanIndexCacheEnabled(true) 有什么副作用? 缓存假设 span size 不随位置变化(除非数据集变了)。DiffUtil 增量更新后,新增/删除 item 会导致 span index 失效,但 RecyclerView 在 notifyItemInserted/Removed 时会自动失效缓存。只要不绕过 Adapter 通知直接改数据集,缓存是安全的。
为什么 onBindViewHolder 里要对私密图标设 alpha 0? 私密空间从锁定展开到解锁时,图标应有”渐显”动画。onBindViewHolder 在过渡期间把 alpha 设为 0,由 PrivateProfileManager 的动画逻辑渐变到 1。这是”绑定即准备动画初始态”的模式,避免动画开始前图标已可见。
6. RecyclerView 与快速滚动 6.1 AllAppsRecyclerView 结构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class AllAppsRecyclerView extends FastScrollRecyclerView { protected final int mNumAppsPerRow; private final AllAppsFastScrollHelper mFastScrollHelper; private int mCumulativeVerticalScroll; private ConstraintLayout mLetterList; protected AlphabeticalAppsList mApps; public AllAppsRecyclerView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super (context, attrs, defStyleAttr); mNumAppsPerRow = LauncherAppState.getIDP(context).numColumns; mFastScrollHelper = new AllAppsFastScrollHelper (this ); } public void setApps (AlphabeticalAppsList apps) { mApps = apps; }
onSearchResultsChanged 搜索结果变化时滚到顶部:
1 2 3 4 public void onSearchResultsChanged () { scrollToTop(); }
滚动状态埋点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @Override public void onScrollStateChanged (int state) { super .onScrollStateChanged(state); StatsLogManager mgr = ActivityContext.lookupContext(getContext()).getStatsLogManager(); switch (state) { case SCROLL_STATE_DRAGGING: mCumulativeVerticalScroll = 0 ; requestFocus(); mgr.logger().sendToInteractionJankMonitor(LAUNCHER_ALLAPPS_VERTICAL_SWIPE_BEGIN, this ); ActivityContext.lookupContext(getContext()).hideKeyboard(); break ; case SCROLL_STATE_IDLE: mgr.logger().sendToInteractionJankMonitor(LAUNCHER_ALLAPPS_VERTICAL_SWIPE_END, this ); logCumulativeVerticalScroll(); break ; } } @Override public void onScrolled (int dx, int dy) { super .onScrolled(dx, dy); mCumulativeVerticalScroll += dy; }
快速滚动的核心映射:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Override public CharSequence scrollToPositionAtProgress (float touchFraction) { int rowCount = mApps.getNumAppRows(); if (rowCount == 0 ) return "" ; List<AlphabeticalAppsList.FastScrollSectionInfo> fastScrollSections = mApps.getFastScrollerSections(); int count = fastScrollSections.size(); if (count == 0 ) return "" ; int index = Utilities.boundToRange((int ) (touchFraction * count), 0 , count - 1 ); AlphabeticalAppsList.FastScrollSectionInfo section = fastScrollSections.get(index); mFastScrollHelper.smoothScrollToSection(section); return section.sectionName; }
映射原理:touchFraction 是手指在快速滚动条上的相对位置(0=顶部,1=底部)。section 数量固定(A-Z 约 26 个),直接线性映射 index = fraction * count。每个 section 记录了 position(adapter position),smoothScrollToSection 滚到该 position。
为什么按 section 索引线性映射而非按 app 数量:
section 数量少(约 26),手指移动 1/26 屏幕高度就跳一个字母,符合直觉。
若按 app 数量映射,app 多的 section(如 S 开头几十个 app)占快速滚动条比例过大,手指需大幅移动才能跳过稀疏字母,体验差。按 section 映射保证每个字母占等量快速滚动条空间。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class AllAppsFastScrollHelper { private static final int NO_POSITION = -1 ; private int mTargetFastScrollPosition = NO_POSITION; private AllAppsRecyclerView mRv; private ViewHolder mLastSelectedViewHolder; public void smoothScrollToSection (FastScrollSectionInfo info) { if (mTargetFastScrollPosition == info.position) { return ; } mTargetFastScrollPosition = info.position; mRv.getLayoutManager().startSmoothScroll(new MyScroller (mTargetFastScrollPosition)); } public void onFastScrollCompleted () { mTargetFastScrollPosition = NO_POSITION; setLastHolderSelected(false ); mLastSelectedViewHolder = null ; } private void setLastHolderSelected (boolean isSelected) { if (mLastSelectedViewHolder != null ) { mLastSelectedViewHolder.itemView.setActivated(isSelected); mLastSelectedViewHolder.setIsRecyclable(!isSelected); } }
MyScroller 是自定义 LinearSmoothScroller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 private class MyScroller extends LinearSmoothScroller { private final int mTargetPosition; public MyScroller (int targetPosition) { super (mRv.getContext()); mTargetPosition = targetPosition; setTargetPosition(targetPosition); } @Override protected int getVerticalSnapPreference () { mRv.performHapticFeedback(CLOCK_TICK); return SNAP_TO_ANY; } @Override protected void onStop () { super .onStop(); if (mTargetPosition != mTargetFastScrollPosition) { return ; } ViewHolder currentHolder = mRv.findViewHolderForAdapterPosition(mTargetPosition); if (currentHolder == mLastSelectedViewHolder) return ; setLastHolderSelected(false ); mLastSelectedViewHolder = currentHolder; setLastHolderSelected(true ); } @Override protected void onStart () { super .onStart(); if (mTargetPosition != mTargetFastScrollPosition) { setLastHolderSelected(false ); mLastSelectedViewHolder = null ; } } }
设计要点:
setIsRecyclable(false):高亮的 ViewHolder 在动画期间不允许被回收,否则高亮会随回收消失。滚动结束后 onFastScrollCompleted 恢复可回收。
CLOCK_TICK 震动:每次 snap 到目标位置给触觉反馈,模拟”刻度感”。
目标变化检测:用户快速拖动快速滚动条时,会连续触发多次 smoothScrollToSection,MyScroller.onStop 检查 mTargetPosition != mTargetFastScrollPosition 判断是否已被新目标取代,避免旧滚动的高亮覆盖新滚动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 @Override public void onUpdateScrollbar (int dy) { if (mApps == null ) return ; List<AllAppsGridAdapter.AdapterItem> items = mApps.getAdapterItems(); if (items.isEmpty() || mNumAppsPerRow == 0 || getChildCount() == 0 ) { mScrollbar.setThumbOffsetY(-1 ); return ; } int scrollY = computeVerticalScrollOffset(); if (scrollY < 0 ) { mScrollbar.setThumbOffsetY(-1 ); return ; } if (Flags.letterFastScroller() && !mScrollbar.isDraggingThumb()) { setLettersToScrollLayout(mApps.getFastScrollerSections()); } int availableScrollBarHeight = getAvailableScrollBarHeight(); int availableScrollHeight = getAvailableScrollHeight(); if (availableScrollHeight <= 0 ) { mScrollbar.setThumbOffsetY(-1 ); return ; } if (mScrollbar.isThumbDetached()) { if (!mScrollbar.isDraggingThumb()) { int scrollBarY = (int ) (((float ) scrollY / availableScrollHeight) * availableScrollBarHeight); int thumbScrollY = mScrollbar.getThumbOffsetY(); int diffScrollY = scrollBarY - thumbScrollY; if (diffScrollY * dy > 0f ) { if (dy < 0 ) { int offset = (int ) ((dy * thumbScrollY) / (float ) scrollBarY); thumbScrollY += Math.max(offset, diffScrollY); } else { int offset = (int ) ((dy * (availableScrollBarHeight - thumbScrollY)) / (float ) (availableScrollBarHeight - scrollBarY)); thumbScrollY += Math.min(offset, diffScrollY); } thumbScrollY = Math.max(0 , Math.min(availableScrollBarHeight, thumbScrollY)); mScrollbar.setThumbOffsetY(thumbScrollY); if (scrollBarY == thumbScrollY) { mScrollbar.reattachThumbToScroll(); } } else { mScrollbar.setThumbOffsetY(thumbScrollY); } } } else { synchronizeScrollBarThumbOffsetToViewScroll(scrollY, availableScrollHeight); } }
“拇指脱离”模式:快速滚动结束后,拇指(thumb)位置与实际滚动位置可能有偏差(因为快速滚动是按 section 跳转,而正常滚动是连续的)。用户随后正常滑动时,拇指需要”追赶”到实际位置。算法按 dy 方向和距离比例移动拇指,直到追上后 reattachThumbToScroll 恢复吸附。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public void setLettersToScrollLayout ( List<AlphabeticalAppsList.FastScrollSectionInfo> fastScrollSections) { if (fastScrollSections.isEmpty()) return ; if (mLetterList != null ) mLetterList.removeAllViews(); Context context = getContext(); ActivityAllAppsContainerView<?> allAppsContainerView = ActivityContext.lookupContext(context).getAppsView(); mLetterList = allAppsContainerView.getFastScrollerLetterList(); mLetterList.setPadding(0 , getScrollBarTop(), 0 , getScrollBarMarginBottom()); List<LetterListTextView> textViews = new ArrayList <>(); for (int i = 0 ; i < fastScrollSections.size(); i++) { AlphabeticalAppsList.FastScrollSectionInfo sectionInfo = fastScrollSections.get(i); LetterListTextView textView = (LetterListTextView) LayoutInflater.from(context).inflate( R.layout.fast_scroller_letter_list_text_view, mLetterList, false ); int viewId = View.generateViewId(); textView.apply(sectionInfo, viewId); sectionInfo.setId(viewId); if (i == fastScrollSections.size() - 1 ) { textView.setVisibility(INVISIBLE); } textViews.add(textView); mLetterList.addView(textView); } LetterListTextView lastLetterListTextView = new LetterListTextView (context); lastLetterListTextView.setVisibility(INVISIBLE); textViews.add(lastLetterListTextView); mLetterList.addView(lastLetterListTextView); constraintTextViewsVertically(mLetterList, textViews); mLetterList.setVisibility(VISIBLE); mLetterList.setAlpha(0 ); }
constraintTextViewsVertically 用 ConstraintSet 建立垂直链:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 private void constraintTextViewsVertically (ConstraintLayout constraintLayout, List<LetterListTextView> textViews) { ConstraintSet chain = new ConstraintSet (); chain.clone(constraintLayout); for (int i = 0 ; i < textViews.size(); i++) { LetterListTextView currentView = textViews.get(i); if (i == 0 ) { chain.connect(currentView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP); } else { chain.connect(currentView.getId(), ConstraintSet.TOP, textViews.get(i - 1 ).getId(), ConstraintSet.BOTTOM); } chain.connect(currentView.getId(), ConstraintSet.START, constraintLayout.getId(), ConstraintSet.START); chain.connect(currentView.getId(), ConstraintSet.END, constraintLayout.getId(), ConstraintSet.END); } int [] viewIds = textViews.stream().mapToInt(TextView::getId).toArray(); float [] weights = new float [textViews.size()]; Arrays.fill(weights, 1 ); chain.createVerticalChain(constraintLayout.getId(), ConstraintSet.TOP, constraintLayout.getId(), ConstraintSet.BOTTOM, viewIds, weights, ConstraintSet.CHAIN_SPREAD); chain.applyTo(constraintLayout); }
设计意图:用 ConstraintLayout 垂直链让 26 个字母均分屏幕高度,手指在任何字母位置都能精确对应。CHAIN_SPREAD + 等权重保证间距均匀。字母列表初始 alpha=0,只在快速滚动时显示,避免静态干扰。
面试深问
快速滚动为什么按 section 线性映射而不是按 adapter position? section 数量固定(约 26),每个字母占快速滚动条等量空间,手指移动距离与字母跳转线性相关,符合直觉。按 position 映射时,app 密集的字母(如 S)占空间过大,稀疏字母几乎无法触及。
mLastSelectedViewHolder.setIsRecyclable(false) 为什么必要? 高亮的 ViewHolder 若被回收,高亮状态丢失且可能被复用绑定到其他位置导致错误高亮。设为不可回收,保证动画期间该 ViewHolder 稳定。onFastScrollCompleted 后恢复可回收,避免内存泄漏。
拇指脱离模式(thumb detached)解决什么问题? 快速滚动按 section 跳转,跳跃式;正常滚动连续。快速滚动后拇指位置与实际 scrollY 不严格对应,若立即吸附会产生跳变。脱离模式下拇指按 dy 比例平滑追赶,追上后再吸附,视觉更自然。
7. 搜索体系 7.1 搜索完整链路 搜索从用户输入到结果刷新的完整链路:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 用户输入 │ TextWatcher.afterTextChanged (AllAppsSearchBarController) ▼ mSearchAlgorithm.doSearch(query, callback) │ DefaultAppSearchAlgorithm.doSearch ▼ enqueueModelUpdateTask (切到 Model 后台线程) │ getTitleMatchResult (StringMatcherUtility.matches) ▼ mResultHandler.post (切回主线程,MAIN_EXECUTOR.getLooper()) │ callback.onSearchResult (AppsSearchContainerLayout) ▼ mAppsView.setSearchResults(items) │ AlphabeticalAppsList.setSearchResults → updateAdapterItems ▼ SearchRecyclerView 渲染 + animateToSearchState (过渡动画)
7.2 AppsSearchContainerLayout:搜索框与回调 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 public class AppsSearchContainerLayout extends ExtendedEditText implements SearchUiManager , SearchCallback<AdapterItem>, AllAppsStore.OnUpdateListener, Insettable { private final ActivityContext mLauncher; private final AllAppsSearchBarController mSearchBarController; private final SpannableStringBuilder mSearchQueryBuilder; private ActivityAllAppsContainerView<?> mAppsView; private final int mContentOverlap; public AppsSearchContainerLayout (Context context, AttributeSet attrs, int defStyleAttr) { super (context, attrs, defStyleAttr); mLauncher = ActivityContext.lookupContext(context); mSearchBarController = new AllAppsSearchBarController (); mSearchQueryBuilder = new SpannableStringBuilder (); Selection.setSelection(mSearchQueryBuilder, 0 ); setHint(prefixTextWithIcon(getContext(), R.drawable.ic_allapps_search, getHint())); mContentOverlap = getResources().getDimensionPixelSize(R.dimen.all_apps_search_bar_content_overlap); } @Override public void initializeSearch (ActivityAllAppsContainerView<?> appsView) { mAppsView = appsView; mSearchBarController.initialize( new DefaultAppSearchAlgorithm (getContext(), true ), this , mLauncher, this ); } @Override public void onSearchResult (String query, ArrayList<AdapterItem> items) { if (items != null ) { mAppsView.setSearchResults(items); } } @Override public void clearSearchResult () { mSearchQueryBuilder.clear(); mSearchQueryBuilder.clearSpans(); Selection.setSelection(mSearchQueryBuilder, 0 ); mAppsView.onClearSearchResult(); } @Override public void onAppsUpdated () { mSearchBarController.refreshSearchResult(); }
为什么 AppsSearchContainerLayout 同时是 SearchUiManager 和 SearchCallback:
SearchUiManager:对外暴露搜索控制接口(resetSearch、getEditText),让容器统一调度。
SearchCallback:作为搜索算法的回调接收者,收到结果后转给 mAppsView。
两者合一减少中间层,搜索框既是 UI 又是控制器。
preDispatchKeyEvent 处理物理键盘输入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Override public void preDispatchKeyEvent (KeyEvent event) { if (!mSearchBarController.isSearchFieldFocused() && event.getAction() == KeyEvent.ACTION_DOWN) { final int unicodeChar = event.getUnicodeChar(); final boolean isKeyNotWhitespace = unicodeChar > 0 && !Character.isWhitespace(unicodeChar) && !Character.isSpaceChar(unicodeChar); if (isKeyNotWhitespace) { boolean gotKey = TextKeyListener.getInstance().onKeyDown(this , mSearchQueryBuilder, event.getKeyCode(), event); if (gotKey && mSearchQueryBuilder.length() > 0 ) { mSearchBarController.focusSearchField(); } } } }
7.3 AllAppsSearchBarController:TextWatcher 驱动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 public class AllAppsSearchBarController implements TextWatcher , OnEditorActionListener, ExtendedEditText.OnBackKeyListener { protected SearchCallback<AdapterItem> mCallback; protected ExtendedEditText mInput; protected String mQuery; private String[] mTextConversions; protected SearchAlgorithm<AdapterItem> mSearchAlgorithm; public final void initialize (SearchAlgorithm<AdapterItem> searchAlgorithm, ExtendedEditText input, ActivityContext launcher, SearchCallback<AdapterItem> callback) { mCallback = callback; mLauncher = launcher; mInput = input; mInput.addTextChangedListener(this ); mInput.setOnEditorActionListener(this ); mInput.setOnBackKeyListener(this ); mSearchAlgorithm = searchAlgorithm; } @Override public void onTextChanged (CharSequence s, int start, int before, int count) { mTextConversions = extractTextConversions(s); } public static String[] extractTextConversions(CharSequence text) { if (text instanceof SpannableStringBuilder) { SpannableStringBuilder spanned = (SpannableStringBuilder) text; SuggestionSpan[] suggestionSpans = spanned.getSpans(0 , text.length(), SuggestionSpan.class); if (suggestionSpans != null && suggestionSpans.length > 0 ) { spanned.removeSpan(suggestionSpans[0 ]); return suggestionSpans[0 ].getSuggestions(); } } return null ; } @Override public void afterTextChanged (final Editable s) { mQuery = s.toString(); if (mQuery.isEmpty()) { mSearchAlgorithm.cancel(true ); mCallback.clearSearchResult(); } else { mSearchAlgorithm.cancel(false ); mSearchAlgorithm.doSearch(mQuery, mTextConversions, mCallback); } }
extractTextConversions 的设计意图:拼音输入法(如 Gboard 拼音)在用户输入拼音字母时,SuggestionSpan 携带候选汉字。提取后传给搜索算法,可以同时匹配拼音和汉字,提升中文搜索体验。
afterTextChanged 的 cancel 策略:
清空 query 时 cancel(true):中断进行中的搜索,避免延迟回来的旧结果污染空状态。
有新 query 时 cancel(false):不中断旧搜索,因为中断+重启开销可能比让旧搜索自然完成更大,且旧结果若先到会被新搜索覆盖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Override public boolean onEditorAction (TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_GO || ( actionId == EditorInfo.IME_NULL && event != null && event.getAction() == KeyEvent.ACTION_DOWN)) { return mLauncher.getAppsView().getMainAdapterProvider().launchHighlightedItem(); } return false ; } @Override public boolean onBackKey () { String query = Utilities.trim(mInput.getEditableText().toString()); if (query.isEmpty()) { reset(); return true ; } return false ; }
7.4 DefaultAppSearchAlgorithm:后台搜索 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 public class DefaultAppSearchAlgorithm implements SearchAlgorithm <AdapterItem> { private static final int MAX_RESULTS_COUNT = 5 ; private final LauncherAppState mAppState; private final Handler mResultHandler; private final boolean mAddNoResultsMessage; public DefaultAppSearchAlgorithm (Context context, boolean addNoResultsMessage) { mAppState = LauncherAppState.getInstance(context); mResultHandler = new Handler (MAIN_EXECUTOR.getLooper()); mAddNoResultsMessage = addNoResultsMessage; } @Override public void cancel (boolean interruptActiveRequests) { if (interruptActiveRequests) { mResultHandler.removeCallbacksAndMessages(null ); } } @Override public void doSearch (String query, SearchCallback<AdapterItem> callback) { mAppState.getModel().enqueueModelUpdateTask((taskController, dataModel, apps) -> { ArrayList<AdapterItem> result = getTitleMatchResult(apps.data, query); if (mAddNoResultsMessage && result.isEmpty()) { result.add(getEmptyMessageAdapterItem(query)); } mResultHandler.post(() -> callback.onSearchResult(query, result)); }); } @AnyThread public static ArrayList<AdapterItem> getTitleMatchResult (List<AppInfo> apps, String query) { final String queryTextLower = query.toLowerCase(); final ArrayList<AdapterItem> result = new ArrayList <>(); StringMatcherUtility.StringMatcher matcher = StringMatcherUtility.StringMatcher.getInstance(); int resultCount = 0 ; int total = apps.size(); for (int i = 0 ; i < total && resultCount < MAX_RESULTS_COUNT; i++) { AppInfo info = apps.get(i); if (StringMatcherUtility.matches(queryTextLower, info.title.toString(), matcher)) { result.add(AdapterItem.asApp(info)); resultCount++; } } return result; } }
为什么搜索在 Model 后台线程:
apps.data 是 Model 持有的全量 app 列表,访问需在 Model 线程保证一致性。
搜索遍历全部 app(可能几百个)做字符串匹配,在主线程会卡 UI。
enqueueModelUpdateTask 把搜索任务排入 Model 线程队列,与数据加载等任务串行,避免并发访问 apps.data。
mResultHandler.post 切回主线程:onSearchResult 会触发 UI 刷新(setSearchResults),必须在主线程。
7.5 StringMatcherUtility:断点切词与中文优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public static boolean matches (String query, String target, StringMatcher matcher) { int queryLength = query.length(); int targetLength = target.length(); if (targetLength < queryLength || queryLength <= 0 ) return false ; if (requestSimpleFuzzySearch(query)) { return target.toLowerCase().contains(query); } int lastType, thisType = Character.UNASSIGNED; int nextType = Character.getType(target.codePointAt(0 )); int end = targetLength - queryLength; for (int i = 0 ; i <= end; i++) { lastType = thisType; thisType = nextType; nextType = i < (targetLength - 1 ) ? Character.getType(target.codePointAt(i + 1 )) : Character.UNASSIGNED; if (matcher.isBreak(thisType, lastType, nextType) && matcher.matches(query, target.substring(i, i + queryLength))) { return true ; } } return false ; }
断点切词的原理:把 target 字符串按字符类型变化切成”词”,只在词的开头尝试匹配。例如 “Play Store” 的断点在空格后的 ‘S’,输入 “sto” 能匹配 “Store” 但 “lay” 不能匹配(因为 “Play” 整词已过)。
isBreak 判断哪些位置是词的开头:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 protected boolean isBreak (int thisType, int prevType, int nextType) { switch (prevType) { case Character.UNASSIGNED: case Character.SPACE_SEPARATOR: case Character.LINE_SEPARATOR: case Character.PARAGRAPH_SEPARATOR: return true ; } switch (thisType) { case Character.UPPERCASE_LETTER: if (nextType != Character.UPPERCASE_LETTER && nextType != Character.OTHER_SYMBOL && nextType != Character.DECIMAL_DIGIT_NUMBER && nextType != Character.UNASSIGNED) { return true ; } case Character.TITLECASE_LETTER: return prevType != Character.UPPERCASE_LETTER; case Character.LOWERCASE_LETTER: return prevType > Character.OTHER_LETTER || prevType <= Character.UNASSIGNED; case Character.DECIMAL_DIGIT_NUMBER: case Character.LETTER_NUMBER: case Character.OTHER_NUMBER: return !(prevType == Character.DECIMAL_DIGIT_NUMBER || prevType == Character.LETTER_NUMBER || prevType == Character.OTHER_NUMBER); case Character.MATH_SYMBOL: case Character.CURRENCY_SYMBOL: case Character.OTHER_PUNCTUATION: case Character.DASH_PUNCTUATION: return true ; default : return false ; } }
经典例子:"YouTube" 输入 "you" 匹配(Y 是断点),输入 "tube" 匹配(T 是驼峰断点),输入 "out" 不匹配(o 不是断点,无法从中间词开始)。
中文优化:
1 2 3 4 5 6 7 8 9 10 11 12 private static boolean requestSimpleFuzzySearch (String s) { for (int i = 0 ; i < s.length(); ) { int codepoint = s.codePointAt(i); i += Character.charCount(codepoint); switch (Character.UnicodeScript.of(codepoint)) { case HAN: return true ; } } return false ; }
为什么中文走 contains 而非断点:
中文字符都是 LOWERCASE_LETTER 或 OTHER_LETTER,断点规则对中文几乎不产生断点(除非前面有空格/符号),断点匹配退化为只能匹配首字。
中文搜索语义是”包含任意子串”(搜”信”匹配”微信”),contains 更符合用户预期。
检测 HAN script(汉字统一表意文字)后切到 contains,兼顾性能和体验。
StringMatcher.matches 本身用 Collator 做本地化前缀判断:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public static class StringMatcher { private static final char MAX_UNICODE = '\uFFFF' ; private final Collator mCollator; StringMatcher() { mCollator = Collator.getInstance(); mCollator.setStrength(Collator.PRIMARY); mCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); } public boolean matches (String query, String target) { if (query == null || target == null ) return false ; int compare = mCollator.compare(query, target); if (compare == 0 ) return true ; else if (compare < 0 ) { return mCollator.compare(query + MAX_UNICODE, target) >= 0 ; } else { return false ; } } }
PRIMARY 强度:忽略大小写(A=a)和重音(é=e)。CANONICAL_DECOMPOSITION:把组合字符分解(如 à 分解为 a + )。前缀判断的技巧:query + MAX_UNICODE后比较,若query是target前缀,追加最大字符后query+MAX应 >=target`。
7.6 搜索结果的复用与过渡 搜索结果推送到容器:
1 2 3 4 5 6 7 8 9 10 public void setSearchResults (ArrayList<AdapterItem> results) { getMainAdapterProvider().clearHighlightedItem(); if (getSearchResultList().setSearchResults(results)) { getSearchRecyclerView().onSearchResultsChanged(); } if (results != null ) { animateToSearchState(true ); } }
AlphabeticalAppsList.setSearchResults:
1 2 3 4 5 6 7 8 public boolean setSearchResults (ArrayList<AdapterItem> results) { if (Objects.equals(results, mSearchResults)) return false ; mSearchResults.clear(); if (results != null ) mSearchResults.addAll(results); updateAdapterItems(); return true ; }
搜索态下 updateAdapterItems 走 hasSearchResults() 分支,直接 mAdapterItems.addAll(mSearchResults),跳过 section 计算和私密空间逻辑——搜索结果是平铺列表,不分字母 section。
animateToSearchState 触发过渡动画:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 void animateToSearchState (boolean goingToSearch, long durationMs) { if (!mSearchTransitionController.isRunning() && goingToSearch == isSearching()) { return ; } mFastScroller.setVisibility(goingToSearch ? INVISIBLE : VISIBLE); if (goingToSearch) { mWorkManager.onActivePageChanged(SEARCH); } else if (mAllAppsTransitionController != null ) { mAllAppsTransitionController.animateAllAppsToNoScale(); } mSearchTransitionController.animateToState(goingToSearch, durationMs, () -> { mIsSearching = goingToSearch; updateSearchResultsVisibility(); int previousPage = getCurrentPage(); if (mRebindAdaptersAfterSearchAnimation) { rebindAdapters(false ); mRebindAdaptersAfterSearchAnimation = false ; } if (goingToSearch) { mSearchUiDelegate.onAnimateToSearchStateCompleted(); } else { setSearchResults(null ); if (mViewPager != null ) mViewPager.setCurrentPage(previousPage); onActivePageChanged(previousPage); } }); }
updateSearchResultsVisibility 切换可见性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 protected void updateSearchResultsVisibility () { if (isSearching()) { getSearchRecyclerView().setVisibility(VISIBLE); getAppsRecyclerViewContainer().setVisibility(GONE); mHeader.setVisibility(GONE); } else { getSearchRecyclerView().setVisibility(GONE); getAppsRecyclerViewContainer().setVisibility(VISIBLE); mHeader.setVisibility(VISIBLE); } if (mHeader.isSetUp()) { mHeader.setActiveRV(getCurrentPage()); } }
7.7 SearchTransitionController:A-Z ↔ 搜索过渡 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 public class SearchTransitionController extends RecyclerViewAnimationController { private static final Interpolator INTERPOLATOR_WITHIN_ALL_APPS = DECELERATE_1_7; private static final Interpolator INTERPOLATOR_TRANSITIONING_TO_ALL_APPS = INSTANT; private boolean mSkipNextAnimationWithinAllApps; @Override protected void animateToState (boolean goingToSearch, long duration, Runnable onEndRunnable) { super .animateToState(goingToSearch, duration, onEndRunnable); if (!goingToSearch) { mAnimator.addListener(forSuccessCallback(() -> { mAllAppsContainerView.getFloatingHeaderView().setFloatingRowsCollapsed(false ); mAllAppsContainerView.getFloatingHeaderView().reset(false ); mAllAppsContainerView.getAppsRecyclerViewContainer().setTranslationY(0 ); })); } mAllAppsContainerView.getFloatingHeaderView().setFloatingRowsCollapsed(true ); mAllAppsContainerView.getFloatingHeaderView().setVisibility(VISIBLE); mAllAppsContainerView.getFloatingHeaderView().maybeSetTabVisibility(VISIBLE); mAllAppsContainerView.getAppsRecyclerViewContainer().setVisibility(VISIBLE); getRecyclerView().setVisibility(VISIBLE); } @Override protected int onProgressUpdated (float searchToAzProgress) { int searchHeight = super .onProgressUpdated(searchToAzProgress); FloatingHeaderView headerView = mAllAppsContainerView.getFloatingHeaderView(); int appsTranslationY = searchHeight + headerView.getFloatingRowsHeight(); if (headerView.usingTabs()) { headerView.setTranslationY(searchHeight); headerView.setAlpha(clampToProgress(searchToAzProgress, 0.8f , 1f )); appsTranslationY += headerView.getTabsAdditionalPaddingBottom() + mAllAppsContainerView.getResources().getDimensionPixelOffset(R.dimen.all_apps_tabs_margin_top) - headerView.getPaddingTop(); } View appsContainer = mAllAppsContainerView.getAppsRecyclerViewContainer(); appsContainer.setTranslationY(appsTranslationY); appsContainer.setAlpha(clampToProgress(searchToAzProgress, 0.8f , 1f )); return searchHeight; }
searchToAzProgress 从 1(完全 A-Z)到 0(完全搜索)。过渡时 A-Z 容器下移并淡出,搜索 RV 从下方滑入。clampToProgress(progress, 0.8, 1) 表示只在 progress 的后 20% 范围内做 alpha 变化,让淡出发生在动画末段,前期先位移。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Override protected boolean shouldAnimate (View view, boolean hasDecorationInfo, boolean appRowComplete) { return !isAppIcon(view) || appRowComplete; } @Override protected TimeInterpolator getInterpolator () { TimeInterpolator timeInterpolator = mAllAppsContainerView.isInAllApps() ? INTERPOLATOR_WITHIN_ALL_APPS : INTERPOLATOR_TRANSITIONING_TO_ALL_APPS; if (mSkipNextAnimationWithinAllApps) { timeInterpolator = INSTANT; mSkipNextAnimationWithinAllApps = false ; } return timeInterpolator; }
面试深问
搜索为什么限制 MAX_RESULTS_COUNT = 5? 默认本地搜索是标题前缀匹配,结果相关性高,前 5 条已覆盖绝大多数意图。更多结果增加渲染开销且用户很少滚动查看。插件化搜索(如 Google App Search)可覆盖更多结果。
requestSimpleFuzzySearch 检测到中文后走 contains,为什么不也用断点匹配? 中文字符类型几乎都是 OTHER_LETTER,断点规则只在”前一个字符是空格/符号”时产生断点,对纯中文标题几乎无断点,断点匹配退化为只能匹配首字。中文搜索语义是子串包含,contains 更符合预期且实现简单。
animateToSearchState 为什么在动画结束的回调里才 setSearchResults(null)? 退出搜索时若立即清结果,搜索 RV 会瞬间空掉,过渡动画无内容可渲染。在回调里清,保证动画期间搜索 RV 仍有旧结果,动画结束后再清,视觉连贯。
8. AllAppsTransitionController:Workspace ↔ All Apps 切换 8.1 状态机:mProgress 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class AllAppsTransitionController implements StateHandler <LauncherState>, OnDeviceProfileChangeListener { public static final float INTERP_COEFF = 1.7f ; private static final float NAV_BAR_COLOR_FORCE_UPDATE_THRESHOLD = 0.1f ; private static final float SWIPE_DRAG_COMMIT_THRESHOLD = 1 - AllAppsSwipeController.ALL_APPS_STATE_TRANSITION_MANUAL; public static final FloatProperty<AllAppsTransitionController> ALL_APPS_PROGRESS = new FloatProperty <AllAppsTransitionController>("allAppsProgress" ) { @Override public Float get (AllAppsTransitionController controller) { return controller.mProgress; } @Override public void setValue (AllAppsTransitionController controller, float progress) { controller.setProgress(progress); } }; private ActivityAllAppsContainerView<Launcher> mAppsView; private final Launcher mLauncher; private final AnimatedFloat mAllAppScale; private boolean mIsVerticalLayout; private boolean mShouldShowAllAppsOnSheet; private float mShiftRange; private float mProgress; public AllAppsTransitionController (Launcher l) { mLauncher = l; DeviceProfile dp = mLauncher.getDeviceProfile(); mProgress = 1f ; mIsVerticalLayout = dp.isVerticalBarLayout(); mShouldShowAllAppsOnSheet = dp.shouldShowAllAppsOnSheet(); setShiftRange(dp.allAppsShiftRange); mLauncher.addOnDeviceProfileChangeListener(this ); }
8.2 setProgress:位移与联动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public void setProgress (float progress) { mProgress = progress; boolean fromBackground = mLauncher.getStateManager().getCurrentStableState() == BACKGROUND_APP; float shiftRange = fromBackground ? mLauncher.getDeviceProfile().getDeviceProperties().getHeightPx() : mShiftRange; getAppsViewProgressTranslationY().setValue(mProgress * shiftRange); mLauncher.onAllAppsTransition(1 - progress); boolean hasScrim = progress < NAV_BAR_COLOR_FORCE_UPDATE_THRESHOLD && mLauncher.getAppsView().getNavBarScrimHeight() > 0 ; mLauncher.getSystemUiController().updateUiState( UI_STATE_ALL_APPS, hasScrim ? mNavScrimFlag : 0 ); }
位移逻辑:mProgress * shiftRange = All Apps 容器相对顶部的 Y 偏移。progress=0 时偏移 0(All Apps 贴顶展开),progress=1 时偏移 shiftRange(All Apps 完全下移出屏幕,露出 Workspace)。
mShiftRange 来自 DeviceProfile.allAppsShiftRange:
手机竖屏:shiftRange ≈ 屏幕高度(All Apps 从底部上滑覆盖全屏)。
大屏 sheet:shiftRange ≈ sheet 高度(All Apps 作为底部 sheet,不全屏覆盖)。
8.3 手机上滑 vs 大屏 sheet 的差异 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public static final FloatProperty<AllAppsTransitionController> ALL_APPS_PULL_BACK_TRANSLATION = new FloatProperty <AllAppsTransitionController>("allAppsPullBackTranslation" ) { @Override public Float get (AllAppsTransitionController controller) { if (controller.mShouldShowAllAppsOnSheet) { return controller.mAppsView.getActiveRecyclerView().getTranslationY(); } else { return controller.getAppsViewPullbackTranslationY().getValue(); } } @Override public void setValue (AllAppsTransitionController controller, float translation) { if (controller.mShouldShowAllAppsOnSheet) { controller.mAppsView.getActiveRecyclerView().setTranslationY(translation); controller.getAppsViewPullbackTranslationY().setValue(ALL_APPS_PULL_BACK_TRANSLATION_DEFAULT); } else { controller.getAppsViewPullbackTranslationY().setValue(translation); controller.mAppsView.getActiveRecyclerView().setTranslationY(ALL_APPS_PULL_BACK_TRANSLATION_DEFAULT); } } };
差异本质:
手机(非 sheet):位移作用于整个 AppsView 容器,mProgress 直接控制容器 Y。ALL_APPS_PULL_BACK_TRANSLATION_DEFAULT = 0,pull back 时 RV 位移归零。
大屏 sheet:mProgress 控制 sheet(即 AppsView)整体,但 pull back(用户拖拽 sheet 时的弹性)作用于 RV 内部,因为 sheet 本身是定位的,弹性反馈体现在内容 RV 上。
mShouldShowAllAppsOnSheet 在 onDeviceProfileChanged 时更新:
1 2 3 4 5 6 7 8 9 10 11 @Override public void onDeviceProfileChanged (DeviceProfile dp) { mIsVerticalLayout = dp.isVerticalBarLayout(); setShiftRange(dp.allAppsShiftRange); if (mIsVerticalLayout) { mLauncher.getHotseat().setTranslationY(0 ); mLauncher.getWorkspace().getPageIndicator().setTranslationY(0 ); } mShouldShowAllAppsOnSheet = dp.shouldShowAllAppsOnSheet(); }
8.4 MultiProperty:多源位移叠加 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 private static final int INDEX_APPS_VIEW_PROGRESS = 0 ; private static final int INDEX_APPS_VIEW_PULLBACK = 1 ; private static final int APPS_VIEW_INDEX_COUNT = 2 ;private MultiValueAlpha mAppsViewAlpha; private MultiPropertyFactory<View> mAppsViewTranslationY; public void setupViews (ScrimView scrimView, ActivityAllAppsContainerView<Launcher> appsView) { mScrimView = scrimView; mAppsView = appsView; mAppsView.setScrimView(scrimView); mAppsViewAlpha = new MultiValueAlpha (mAppsView, APPS_VIEW_INDEX_COUNT, View.GONE); mAppsViewAlpha.setUpdateVisibility(true ); mAppsViewTranslationY = new MultiPropertyFactory <>( mAppsView, VIEW_TRANSLATE_Y, APPS_VIEW_INDEX_COUNT, Float::sum); }
为什么用多源叠加:
状态动画(INDEX_APPS_VIEW_PROGRESS)和手势弹性(INDEX_APPS_VIEW_PULLBACK)可能同时作用于位移。若用单一变量,手势松手时需先把弹性值合并回 progress,逻辑复杂且易错。
MultiPropertyFactory 让每个源独立设值,最终 translationY = progressValue + pullbackValue,互不干扰。松手后 pullback 归零,progress 接管完成剩余动画。
setAlphas 控制可见性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public void setAlphas (LauncherState state, StateAnimationConfig config, PropertySetter setter) { int visibleElements = state.getVisibleElements(mLauncher.getLauncherUiState()); boolean hasAllAppsContent = (visibleElements & ALL_APPS_CONTENT) != 0 ; Interpolator allAppsFade = config.getInterpolator(ANIM_ALL_APPS_FADE, LINEAR); setter.setFloat(getAppsViewProgressAlpha(), MultiPropertyFactory.MULTI_PROPERTY_VALUE, hasAllAppsContent ? 1 : 0 , allAppsFade); setter.setFloat(getAppsViewPullbackAlpha(), MultiPropertyFactory.MULTI_PROPERTY_VALUE, hasAllAppsContent ? 1 : 0 , allAppsFade); boolean shouldProtectHeader = !config.hasAnimationFlag(StateAnimationConfig.SKIP_SCRIM) && (ALL_APPS == state || mLauncher.getStateManager().getState() == ALL_APPS); mScrimView.setDrawingController(shouldProtectHeader ? mAppsView : null ); }
8.5 setStateWithAnimation:状态切换动画 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 @Override public void setStateWithAnimation (LauncherState toState, StateAnimationConfig config, PendingAnimation builder) { if (mLauncher.isInState(ALL_APPS) && !ALL_APPS.equals(toState)) { builder.addEndListener(success -> { ALL_APPS_PULL_BACK_TRANSLATION.set(this , ALL_APPS_PULL_BACK_TRANSLATION_DEFAULT); ALL_APPS_PULL_BACK_ALPHA.set(this , ALL_APPS_PULL_BACK_ALPHA_DEFAULT); mAllAppScale.updateValue(1f ); }); } float targetProgress = toState.getVerticalProgress(mLauncher); if (Float.compare(mProgress, targetProgress) == 0 ) { setAlphas(toState, config, builder); return ; } Interpolator verticalProgressInterpolator = config.getInterpolator(ANIM_VERTICAL_PROGRESS, config.isUserControlled() ? LINEAR : DECELERATE_1_7); Animator anim = createSpringAnimation(mProgress, targetProgress); anim.addListener(new AnimatorListenerAdapter () { @Override public void onAnimationCancel (Animator animation) { setProgress(targetProgress); } }); anim.setInterpolator(verticalProgressInterpolator); builder.add(anim); setAlphas(toState, config, builder); if (ALL_APPS.equals(toState) && mLauncher.isInState(NORMAL)) { if (Flags.msdlFeedback()) { if (config.isUserControlled()) { mMSDLPlayerWrapper.playToken(MSDLToken.SWIPE_THRESHOLD_INDICATOR); } else { mMSDLPlayerWrapper.playToken(MSDLToken.TAP_HIGH_EMPHASIS); } } else { mLauncher.getAppsView().performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); } } } public Animator createSpringAnimation (float ... progressValues) { return ObjectAnimator.ofFloat(this , ALL_APPS_PROGRESS, progressValues); }
8.6 Predictive Back 缩放 Android 14+ 的预测性返回手势会让 All Apps 缩放:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 @Override public void onBackStarted (LauncherState toState) { setShouldScaleHeader(!mLauncher.getAppsView().shouldBackExitSearch()); } @Override public void onBackProgressed (LauncherState toState, float backProgress) { if (!mLauncher.isInState(ALL_APPS) || !NORMAL.equals(toState)) return ; float scaleProgress = ScrollableLayoutManager.PREDICTIVE_BACK_MIN_SCALE + (1 - ScrollableLayoutManager.PREDICTIVE_BACK_MIN_SCALE) * (1 - backProgress); mAllAppScale.updateValue(scaleProgress); } private void onScaleProgressChanged () { final float scaleProgress = mAllAppScale.value; SCALE_PROPERTY.set(mLauncher.getAppsView(), scaleProgress); if (mShouldScaleHeader || !mShouldShowAllAppsOnSheet) { mLauncher.getScrimView().setScrimHeaderScale(scaleProgress); } AllAppsRecyclerView rv = mLauncher.getAppsView().getActiveRecyclerView(); boolean hasScaleEffect = scaleProgress < 1f ; if (hasScaleEffect != mHasScaleEffect) { mHasScaleEffect = hasScaleEffect; if (mHasScaleEffect) { modifyAttributesOnViewTree(rv, mLauncher.getAppsView(), CLIP_CHILDREN_FALSE_MODIFIER); } else { restoreAttributesOnViewTree(rv, mLauncher.getAppsView(), CLIP_CHILDREN_FALSE_MODIFIER); } } } public void animateAllAppsToNoScale () { if (mAllAppScale.isAnimating()) mAllAppScale.cancelAnimation(); Animator animator = mAllAppScale.animateToValue(1f ) .setDuration(REVERT_SWIPE_ALL_APPS_TO_HOME_ANIMATION_DURATION_MS); if (mAllAppsSearchBackAnimationListener != null ) { animator.addListener(mAllAppsSearchBackAnimationListener); } animator.start(); }
禁用裁剪的原因:缩放时 View 边缘会放大,若启用 clipChildren,放大露出的额外 icon 行会被裁掉。modifyAttributesOnViewTree 从 RV 到 AppsView 整棵树禁用裁剪,让预计算的额外 icon(AppsGridLayoutManager.calculateExtraLayoutSpace)可见,缩放动画不出现黑边。
8.7 Spring 物理回弹 ActivityAllAppsContainerView 继承 SpringRelativeLayout,提供手势松手后的弹性回弹:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public static final float PULL_MULTIPLIER = .02f ;public static final float FLING_VELOCITY_MULTIPLIER = 1200f ;public void addSpringFromFlingUpdateListener (ValueAnimator animator, float velocity, float progress) { animator.addListener(new AnimatorListenerAdapter () { @Override public void onAnimationStart (Animator animator) { float distance = (1 - progress) * getHeight(); float settleVelocity = Math.min(0 , distance / (AllAppsTransitionController.INTERP_COEFF * animator.getDuration()) + velocity); absorbSwipeUpVelocity(Math.max(1000 , Math.abs( Math.round(settleVelocity * FLING_VELOCITY_MULTIPLIER)))); } }); } public void onPull (float deltaDistance, float displacement) { absorbPullDeltaDistance(PULL_MULTIPLIER * deltaDistance, PULL_MULTIPLIER * displacement); }
INTERP_COEFF = 1.7f 是 DECELERATE_1_7 插值器的二阶导特征值,用于估算松手时的 settle velocity,让弹性回弹与状态动画速度衔接自然。
面试深问
mProgress 为什么 0 是展开、1 是隐藏,与直觉相反?mProgress * mShiftRange = translationY,展开时 translationY=0(贴顶),故 progress=0。隐藏时 translationY=shiftRange(下移出屏幕),故 progress=1。这与”进度”语义一致:progress 越大,离开 All Apps 的进度越深。LauncherState.getVerticalProgress() 返回值与此对应(ALL_APPS 返回 0,NORMAL 返回 1)。
手机和大屏 sheet 的位移差异为什么需要两套 ALL_APPS_PULL_BACK_TRANSLATION 逻辑? 手机上 All Apps 全屏,pull back 是整个容器的弹性位移。大屏 sheet 上 sheet 位置固定(贴底),弹性反馈只能体现在内容 RV 上。同一个属性根据 mShouldShowAllAppsOnSheet 分流到不同 View,避免 sheet 模式下整个 sheet 弹跳。
MultiPropertyFactory 为什么用 Float::sum 聚合? progress 和 pullback 是两个独立的位移来源,可能同时非零(手势拖拽期间 progress 在变,pullback 也在积累)。取和让两者叠加:手势期间总位移 = 状态位移 + 弹性位移,松手后 pullback 归零,progress 接管。若取 max 或覆盖,弹性与状态会互相吞噬,视觉不连贯。