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,914评论 3 14
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,861评论 25 708
  • 群里有人说:掉点儿了。 有人接下话茬儿:只有北方人才会这样说。 我恍然想到,真的是如此呢。南方的朋友可能不明白啥叫...
    子龙老师阅读 709评论 24 22
  • 潇潇砂雨点破梦幻, 昔刻雅雅天堂,惊醒于真切间 魂已守候多时―― 蓦然回首,气色苍凉些 发梢缀了斑白! 不知是恍了...
    漂漂流阅读 220评论 0 0
  • 多少年都好,乡愁依旧在! 时间回不到过去,曾经不容改写, 尽管世事百转千回,少时结伴成长的兄弟们! 牵挂/祝福 ...
    别山举水阅读 2,160评论 77 91