Launcher3 負一屏的定制開發

左一屏

可能有的小伙伴不清楚什么是左一屏,或者是負一屏。以我的了解,在智能手機還沒有普及的時候,最早的左一屏的概念是來自Apple 蘋果電腦的dashboard操作面板,如下圖

dashboard

后來iphone, Android也都使用了這個概念,相對PC而言,手機上的dashboard相對的精簡了許多。如圖


iphone 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;
    }

  1. 可以在繼承Launcher的子類里設置一個LauncherCallbacks, 并讓hasCustomContentToLeft() 方法返回true即可
  2. 或者直接修改這個方法,直接返回true也可以

但其實上面的方法都不太好,因為在眾多的體驗中,有人喜歡這個功能,也有人不喜歡這個功能。故我們比較好的做法是設計一個開關的功能,
讓用戶自行選擇即可。

至于dashboard放什么內容就很值得考究了,設計得好的話就會讓用戶愛不釋手,這個就交給產品經理吧。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在Android開發中,我們經常會遇到界面的跳轉和回退,在開發中與之聯系比較緊密的概念是Task(任務)和Back...
    Geeks_Liu閱讀 2,944評論 3 14
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,287評論 25 708
  • 群里有人說:掉點兒了。 有人接下話茬兒:只有北方人才會這樣說。 我恍然想到,真的是如此呢。南方的朋友可能不明白啥叫...
    子龍老師閱讀 711評論 24 22
  • 瀟瀟砂雨點破夢幻, 昔刻雅雅天堂,驚醒于真切間 魂已守候多時―― 驀然回首,氣色蒼涼些 發梢綴了斑白! 不知是恍了...
    漂漂流閱讀 236評論 0 0
  • 多少年都好,鄉愁依舊在! 時間回不到過去,曾經不容改寫, 盡管世事百轉千回,少時結伴成長的兄弟們! 牽掛/祝福 ...
    別山舉水閱讀 2,167評論 77 91