Android7.0开发实现Launcher3去掉应用抽屉的方法详解
本文实例讲述了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<invariantdeviceprofile> getpredefineddeviceprofiles() { arraylist<invariantdeviceprofile> 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 ? 4 : 5), 48, (hasda ? 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 ? 4 : 5), 48, (hasda ? 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 ? 4 : 5), 48, (hasda ? 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 ? 4 : 5), 56, (hasda ? 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 ? 4 : 5), 56, (hasda ? 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<iteminfo> tmpinfos; arraylist<iteminfo> added = new arraylist<iteminfo>(); 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<iteminfo> addedinfos = new arraylist<iteminfo>(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程序设计有所帮助。