Android7.0開發(fā)實(shí)現(xiàn)Launcher3去掉應(yīng)用抽屜的方法詳解
本文實(shí)例講述了Android7.0開發(fā)實(shí)現(xiàn)Launcher3去掉應(yīng)用抽屜的方法。分享給大家供大家參考,具體如下:
年初做過一個(gè)項(xiàng)目,有一個(gè)需求就是需要將桌面變?yōu)閱螌硬恍枰壊藛巍W罱鼛状斡行』锇橛羞@個(gè)問我這個(gè)解決辦法?,F(xiàn)在我將分享給大家。
先上效果圖:

功能分解
1. 去除Allapp鍵,調(diào)整HotSeat布局
2. 將所有應(yīng)用擺在launcher第一層
3. 去掉長按時(shí)刪除選項(xiàng)
解決方案
一、設(shè)置總開關(guān)
按照6.0 Launcher3 的模式,添加一個(gè)開關(guān),控制是否去掉抽屜。
LauncherAppState類:單例模式,主要在啟動(dòng)的時(shí)候用,他初始化了一些對象,并且注冊了廣播監(jiān)聽器和ContentObserver。為了能靈活切換模式,在此類中添加靜態(tài)開關(guān)。
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時(shí),會(huì)留出HotSeat的第三個(gè)位置給allapp按鈕,若不取消該位置的占用,在HotSeat加載時(shí)會(huì)留出空位。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時(shí)停止加載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);
}
}//別漏了這里的 }
三、數(shù)據(jù)初始化類中更改HotSeat布局
InvariantDeviceProfile.java Launcher3進(jìn)行布局初始化的一個(gè)類。
在有allapp按鈕時(shí)HotSeat里Hotseat圖標(biāo)數(shù)量為五個(gè),沒有allapp按鈕時(shí)Hotseat圖標(biāo)數(shù)量應(yīng)為四個(gè)。
Launcher3\src\com\android\launcher3\InvariantDeviceProfile.java:
1)先加個(gè)宏控
//添加 @{
private boolean hasDA = LauncherAppState.isDisableAllApps();
//添加 }@
2)去掉抽屜時(shí),HotSeat的格數(shù)為四格,所以不能拋出異常。 ( numHotseatIcons 為偶時(shí)不拋異常)
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 的布局 ,因?yàn)镠otseat只有5格了。
四、將所有應(yīng)用放在第一層
launcher3加載流程:進(jìn)入 LauncherApplication -> LauncherAppState -> 進(jìn)行初始化環(huán)境(通過傳遞sContext)。進(jìn)行事件監(jiān)聽&&初始化一些環(huán)境。例如:橫豎屏、當(dāng)局語言、像素密度、小部件和快捷圖標(biāo)數(shù)據(jù)庫操作對象、應(yīng)用圖標(biāo)緩存對象、初始化LauncherMode等。在初始化過后,從Launcher的Oncreate方法入手。mModel.startLoader(mWorkspace.getRestorePage());里加載數(shù)據(jù) 。在加載完成所有快捷方式后將其余為加載完的應(yīng)用布局在第一層。
1) 成所有快捷方式后將其余為加載完的應(yīng)用布局在第一層。
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 雖然去掉了去抽屜的代碼,但留了這個(gè)方法給我們。
}
}
五、有新應(yīng)用添加時(shí)更新Workspace
當(dāng)安裝新應(yīng)用時(shí),我們需要對左面更新,保證安裝的應(yīng)用添加在第一層上。
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);
}
}
... ...
}
六、去掉長按時(shí)的刪除選項(xiàng)
長按時(shí),不該有刪除選項(xiàng) 。
DeleteDropTarget.java: 中更改長按時(shí)的監(jiān)聽,開始時(shí)直接屏蔽刪除按鈕,后來發(fā)現(xiàn)應(yīng)用自身發(fā)出的快捷方式無法刪除 所以做了如下處理。
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去掉應(yīng)用抽屜的改動(dòng)已經(jīng)完成。還有很多我們需要去美化的,就比如HotSeat布局自適應(yīng)等。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android窗口相關(guān)操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
微前端架構(gòu)ModuleFederationPlugin源碼解析
這篇文章主要為大家介紹了微前端架構(gòu)ModuleFederationPlugin源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android自定義View之邊框文字、閃爍發(fā)光文字
這篇文章主要為大家詳細(xì)介紹了Android自定義View之邊框文字、閃爍發(fā)光文字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能
這篇文章主要介紹了Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能,文中還給大家?guī)硭姆N方法,大家可以根據(jù)自己需要參考下2018-07-07
發(fā)布?Android?library?到?Maven?解析
這篇文章主要介紹了發(fā)布?Android?library到Maven解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法
這篇文章主要介紹了Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法,涉及Android activity相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2017-07-07
Android監(jiān)聽輸入法彈窗和關(guān)閉的實(shí)現(xiàn)方法
用過ios的都知道ios上輸入法關(guān)閉的同時(shí)會(huì)自動(dòng)關(guān)閉輸入框,那么在android上如何實(shí)現(xiàn)監(jiān)聽輸入法彈出和關(guān)閉呢?接下來通過本文給大家分享一種可靠的實(shí)現(xiàn)方式2016-11-11

