热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android7.0开发实现Launcher3去掉应用抽屉的方法详解

这篇文章主要介绍了Android7.0开发实现Launcher3去掉应用抽屉的方法,结合实例形式分析了Android7.0Launcher3调整界面布局的相关操作技巧与注意事项,需要的朋友可以参考下

本文实例讲述了Android7.0开发实现Launcher3去掉应用抽屉的方法。分享给大家供大家参考,具体如下:

年初做过一个项目,有一个需求就是需要将桌面变为单层不需要二级菜单。最近几次有小伙伴有这个问我这个解决办法。现在我将分享给大家。

先上效果图:

 

功能分解

1. 去除Allapp键,调整HotSeat布局
2. 将所有应用摆在launcher第一层
3. 去掉长按时删除选项

解决方案

一、设置总开关

按照6.0 Launcher3 的模式,添加一个开关,控制是否去掉抽屉。
LauncherAppState类:单例模式,主要在启动的时候用,他初始化了一些对象,并且注册了广播监听器和ContentObserver。为了能灵活切换模式,在此类中添加静态开关。

Launcher3\src\com\android\launcher3\LauncherAppState.java:

public static boolean isDisableAllApps() {
    // Returns false on non-dogfood builds.
    return android.os.SystemProperties.get("ro.wind.launcher3.ishome2","0").equals("1");
}

二、Allapp键的加载

在HotSeat里面去掉Allapp键的加载 ,屏蔽isAllAppsButtonRank()占用allapp位置。

1) 不再占用allapp位置

2) 在加载Workspace时,会留出HotSeat的第三个位置给allapp按钮,若不取消该位置的占用,在HotSeat加载时会留出空位。HotSeat的初始化在HotSeat.java中

Launcher3\src\com\android\launcher3\HotSeat.java –>isAllAppsButtonRank():

public boolean isAllAppsButtonRank(int rank) {
    //添加 @{
    if (LauncherAppState.isDisableAllApps()) {
      return false;
      }
    //添加 @}
    return rank == mAllAppsButtonRank;
}

3) Home2没有抽屉,所以不需要allapp按钮。在HotSeat里面去掉Allapp键的加载,在HotSeat.java 的void resetLayout()中初始化HotSeat布局。在Home2时停止加载Allapp按钮。

Launcher3\src\com\android\launcher3\HotSeat.java –>resetLayout():

void resetLayout() {
    mContent.removeAllViewsInLayout();
    //添加 @{
    if(LauncherAppState.isDisableAllApps()){
    //添加 }@
    // Add the Apps button
    Context cOntext= getContext();
    LayoutInflater inflater = LayoutInflater.from(context);
    TextView allAppsButton = (TextView)
        inflater.inflate(R.layout.all_apps_button, mContent, false);
    Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
    mLauncher.resizeIconDrawable(d);
    allAppsButton.setCompoundDrawables(null, d, null, null);
    allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
    allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
    if (mLauncher != null) {
      mLauncher.setAllAppsButton(allAppsButton);
      allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
      allAppsButton.setOnClickListener(mLauncher);
      allAppsButton.setOnLongClickListener(mLauncher);
      allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
    }
    // Note: We do this to ensure that the hotseat is always laid out in the orientation of
    // the hotseat in order regardless of which orientation they were added
    int x = getCellXFromOrder(mAllAppsButtonRank);
    int y = getCellYFromOrder(mAllAppsButtonRank);
    CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
    lp.canReorder = false;
    mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
    }
}//别漏了这里的 }

三、数据初始化类中更改HotSeat布局

InvariantDeviceProfile.java Launcher3进行布局初始化的一个类。

在有allapp按钮时HotSeat里Hotseat图标数量为五个,没有allapp按钮时Hotseat图标数量应为四个。

Launcher3\src\com\android\launcher3\InvariantDeviceProfile.java:

1)先加个宏控

//添加 @{
private boolean hasDA = LauncherAppState.isDisableAllApps();
//添加 }@

2)去掉抽屉时,HotSeat的格数为四格,所以不能抛出异常。 ( numHotseatIcons 为偶时不抛异常)

InvariantDeviceProfile( ):

InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, int maapc,
    // Ensure that we have an odd number of hotseat items (since we need to place all apps)
    if (hs % 2 == 0&& !hasDA) {// 在无抽屉情况下不抛异常
      throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
    }
    name = n;
     ...  ...
}

3)去掉抽屉的情况下加载不同的布局

getPredefinedDeviceProfiles() :

ArrayList getPredefinedDeviceProfiles() {
    ArrayList predefinedDeviceProfiles = new ArrayList<>();
    // width, height, #rows, #columns, #folder rows, #folder columns,
    // iconSize, iconTextSize, #hotseat, #hotseatIconSize, defaultLayoutId.
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
        255, 300,   2, 3, 2, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
        255, 400,   3, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
        275, 420,   3, 4, 3, 4, 4, 48, 13, (hasDA &#63; 4 : 5), 48, (hasDA &#63; R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
        255, 450,   3, 4, 3, 4, 4, 48, 13, (hasDA &#63; 4 : 5), 48, (hasDA &#63; R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus S",
        296, 491.33f, 4, 4, 4, 4, 4, 48, 13,(hasDA &#63; 4 : 5), 48, (hasDA &#63; R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4",
        335, 567,   4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasDA &#63; 4 : 5), 56, (hasDA &#63; R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5",
        359, 567,   4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13,(hasDA &#63; 4 : 5), 56, (hasDA &#63; R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Large Phone",
        406, 694,   5, 5, 4, 4, 4, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5));
    // The tablet profile is odd in that the landscape orientation
    // also includes the nav bar on the side
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 7",
        575, 904,   5, 6, 4, 5, 4, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6));
    // Larger tablet profiles always have system bars on the top & bottom
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10",
        727, 1207,  5, 6, 4, 5, 4, 76, 14.4f, 7, 64, R.xml.default_workspace_5x6));
    predefinedDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet",
        1527, 2527,  7, 7, 6, 6, 4, 100, 20, 7, 72, R.xml.default_workspace_4x4));
    return predefinedDeviceProfiles;
}

5)记得改下 dw_phone_hotseat.xml 的布局 ,因为Hotseat只有5格了。

四、将所有应用放在第一层

launcher3加载流程:进入 LauncherApplication -> LauncherAppState -> 进行初始化环境(通过传递sContext)。进行事件监听&&初始化一些环境。例如:横竖屏、当局语言、像素密度、小部件和快捷图标数据库操作对象、应用图标缓存对象、初始化LauncherMode等。在初始化过后,从Launcher的Oncreate方法入手。mModel.startLoader(mWorkspace.getRestorePage());里加载数据 。在加载完成所有快捷方式后将其余为加载完的应用布局在第一层。

1) 成所有快捷方式后将其余为加载完的应用布局在第一层。

Launcher3\src\com\android\launcher3\LauncherModel.java:

LauncherModel$LoaderTask –> run():

public void run() {
  ... ...
  // Optimize for end-user experience: if the Launcher is up and // running with the
  // All Apps interface in the foreground, load All Apps first. Otherwise, load the
  // workspace first (default).
  keep_running: {
    if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
    loadAndBindWorkspace();
    if (mStopped) {
      LauncherLog.i(TAG, "LoadTask break in the middle, this = " + this);
      break keep_running;
    }
    waitForIdle();
    // second step
    if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
    loadAndBindAllApps();
    //添加 @{
    if (LauncherAppState.isDisableAllApps()) {
      verifyApplications();
    }
    //添加 }@
  }
  // Clear out this reference, otherwise we end up holding it until all of the
  // callback runnables are done.
  ... ...
}

添加verifyApplications():

private void verifyApplications() {
    final Context cOntext= mApp.getContext();
    // Cross reference all the applications in our apps list with items in the workspace
    ArrayList tmpInfos;
    ArrayList added = new ArrayList();
    synchronized (sBgLock) {
      for (AppInfo app : mBgAllAppsList.data) {
        tmpInfos = getItemInfoForComponentName(app.componentName, app.user);
        if (tmpInfos.isEmpty()) {
          // We are missing an application icon, so add this to the workspace
          added.add(app);
          // This is a rare event, so lets log it
          Log.e(TAG, "Missing Application on load: " + app);
        }
      }
    }
    if (!added.isEmpty()) {
      addAndBindAddedWorkspaceItems(context, added);//7.0 虽然去掉了去抽屉的代码,但留了这个方法给我们。
    }
}

五、有新应用添加时更新Workspace

当安装新应用时,我们需要对左面更新,保证安装的应用添加在第一层上。

Launcher3\src\com\android\launcher3\LauncherModel.java:

LauncherModel$PackageUpdatedTask –> run():

public void run() {
  if (!mHasLoaderCompletedOnce) {
    // Loader has not yet run.
    return;
  }
  final Context cOntext= mApp.getContext();
  ... ...
  if (added != null) {
    // 添加 @{
    if(LauncherAppState.isDisableAllApps()){
        final ArrayList addedInfos = new ArrayList(added);
        addAndBindAddedWorkspaceItems(context, addedInfos);
    }else{
    // 添加 }@
    addAppsToAllApps(context, added);
    }
    for (AppInfo ai : added) {
      addedOrUpdatedApps.put(ai.componentName, ai);
    }
  }
  ... ...
}

六、去掉长按时的删除选项

长按时,不该有删除选项 。

DeleteDropTarget.java: 中更改长按时的监听,开始时直接屏蔽删除按钮,后来发现应用自身发出的快捷方式无法删除 所以做了如下处理。

Launcher3\src\com\android\launcher3\DeleteDropTarget.java –>supportsDrop():

public static boolean supportsDrop(Object info) {
     //添加 @{
    if (LauncherAppState.isDisableAllApps()) {
      if (info instanceof ShortcutInfo) {
        ShortcutInfo item = (ShortcutInfo) info;
        return item.itemType != LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
      }
      return info instanceof LauncherAppWidgetInfo;
    }
    //添加 }@
    return (info instanceof ShortcutInfo)
        || (info instanceof LauncherAppWidgetInfo)
        || (info instanceof FolderInfo);
}

写在最后

到此,Launcher3去掉应用抽屉的改动已经完成。还有很多我们需要去美化的,就比如HotSeat布局自适应等。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android窗口相关操作技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。


推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
author-avatar
DZ2017
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有