主目錄見:Android高級進階知識(這是總目錄索引)
Launcher3源碼地址:Launcher3-master
[This tutorial was written by Ticoo]
左一屏
可能有的小伙伴不清楚什么是左一屏,或者是負一屏。以我的了解,在智能手機還沒有普及的時候,最早的左一屏的概念是來自Apple 蘋果電腦的dashboard操作面板,如下圖
后來iphone手機沿用了Mac的概念,相對PC而言,手機上的dashboard相對的精簡了許多。如圖
以上圖片來源于Apple官網
用過Google親兒子手機的小伙伴都會發現,原生的Launcher并沒有左一屏的功能,而像最近新出的手機都帶了這個功能。
但其實dashboard的功能Google已經提供給我們了.
我們找到WorkSpace里的 createCustomContentContainer ,這個方法就是創建dashboard的功能。
WorkSpace.java
public void createCustomContentContainer() {
CellLayout customScreen = (CellLayout)
mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, this, false);
customScreen.disableDragTarget();
customScreen.disableJailContent();
mWorkspaceScreens.put(CUSTOM_CONTENT_SCREEN_ID, customScreen);
mScreenOrder.add(0, CUSTOM_CONTENT_SCREEN_ID);
// We want no padding on the custom content
customScreen.setPadding(0, 0, 0, 0);
addFullScreenPage(customScreen);
// Ensure that the current page and default page are maintained.
mDefaultPage = mOriginalDefaultPage + 1;
// Update the custom content hint
if (mRestorePage != INVALID_RESTORE_PAGE) {
mRestorePage = mRestorePage + 1;
} else {
setCurrentPage(getCurrentPage() + 1);
}
updateCustomContentMarker();
}
我們都知道Launcher的工作臺是WorkSpace,而Workspace里的每一屏就是CellLayout啦。可以發現,從布局 R.layout.workspace_screen inflate出CellLayout,然后以全屏的方式添加到WorkSpace中,指定dashboard的特定screenId CUSTOM_CONTENT_SCREEN_ID -301,同時更新我們的默認主頁 mDefaultPage。
知道創建的方法,那怎么把它啟用呢?
找到它的調用方法,在Launcher的bindScreens方法里。創建dashboard的條件是 hasCustomContentToLeft()
@Override
public void bindScreens(ArrayList<Long> orderedScreenIds) {
bindAddScreens(orderedScreenIds);
// If there are no screens, we need to have an empty screen
if (orderedScreenIds.size() == 0) {
mWorkspace.addExtraEmptyScreen();
}
// Create the custom content page (this call updates mDefaultScreen which calls
// setCurrentPage() so ensure that all pages are added before calling this).
if (hasCustomContentToLeft()) {
mWorkspace.createCustomContentContainer();
populateCustomContentContainer();
}
}
hasCustomContentToLeft方法,有一個LauncherCallbacks的回調,這樣我們就有思路了
/**
* To be overridden by subclasses to hint to Launcher that we have custom content
*/
protected boolean hasCustomContentToLeft() {
if (mLauncherCallbacks != null) {
return mLauncherCallbacks.hasCustomContentToLeft();
}
return false;
}
- 可以在繼承Launcher的子類里設置一個LauncherCallbacks, 并讓hasCustomContentToLeft() 方法返回true即可
- 或者直接修改這個方法,直接返回true也可以
但其實上面的方法都不太好,因為在眾多的體驗中,有人喜歡這個功能,也有人不喜歡這個功能。故我們比較好的做法是設計一個開關的功能,
讓用戶自行選擇即可。
至于dashboard放什么內容就很值得考究了,設計得好的話就會讓用戶愛不釋手,這個就交給產品經理吧。