自學(xué)HarmonyOS應(yīng)用開(kāi)發(fā)(71)- 優(yōu)化目錄間遷移體驗(yàn)

通過(guò)前面幾篇文章,雖然我們實(shí)現(xiàn)了在各級(jí)目錄之間進(jìn)行切換的功能,但還是有一些不夠方便的地方。例如:
返回上級(jí)目錄之前必須首先當(dāng)前回到目錄的最頂端
退回上級(jí)目錄之后,總是回到該目錄的最頂端。這對(duì)于希望繼續(xù)瀏覽目錄內(nèi)容的用戶很不友好
針對(duì)這兩個(gè)問(wèn)題,我們采取如下對(duì)策:
通過(guò)畫(huà)面頂端的操作區(qū)實(shí)現(xiàn)返回上級(jí)目錄功能
每次進(jìn)入下級(jí)目錄時(shí)記錄當(dāng)前表示位置,從下級(jí)目錄返回時(shí)恢復(fù)這個(gè)表示位置。
修改后的動(dòng)作視頻如下:
返回上級(jí)目錄按鈕
畫(huà)面最上面操作區(qū)的布局文件中和返回上級(jí)目錄按鈕相關(guān)的部分如下:
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:id="$+id:app_bar"ohos:height="match_content"ohos:width="match_parent"ohos:background_element="$color:title_layout_background_color"ohos:orientation="horizontal"><DirectionalLayoutohos:id="$+id:title_layout"ohos:height="56vp"ohos:width="match_content"ohos:orientation="horizontal"ohos:weight="1"><Imageohos:id="$+id:left_arrow"ohos:height="24vp"ohos:width="24vp"ohos:layout_alignment="center"ohos:start_margin="24vp"/>
當(dāng)MainAbilitySlice啟動(dòng)時(shí),為該按鈕指定圖像文件和按下處理:
public void onStart(Intent intent) {setUIContent(ResourceTable.Layout_browser_ability);Image leftArrow = (Image) findComponentById(ResourceTable.Id_left_arrow);leftArrow.setPixelMap(ResourceTable.Media_return_gray50);leftArrow.setClickedListener(new Component.ClickedListener() {public void onClick(Component component) {FileListContainer listContainer = (FileListContainer) findComponentById(ResourceTable.Id_list_container);listContainer.backToParent();}});initListContainer();}
最初表示的目錄是根目錄,因此對(duì)應(yīng)的圖像文件是灰色按鈕。接下來(lái)我們可以在FileListContainer的當(dāng)前目錄發(fā)生變化時(shí)決定返回上級(jí)目錄按鈕的狀態(tài):
private void initListContainer() {FileListContainer listContainer = (FileListContainer) findComponentById(ResourceTable.Id_list_container);listContainer.setSelectedListener(listener);listContainer.setDirChangeListener(new BrowserItem.DirChangeListener() {public void dirChanged(File dir) {String parent = dir.getParent();Image leftArrow = (Image) findComponentById(ResourceTable.Id_left_arrow);if(parent != null){leftArrow.setPixelMap(ResourceTable.Media_return_black50);}else{leftArrow.setPixelMap(ResourceTable.Media_return_gray50);}}});}
記錄和恢復(fù)進(jìn)入下級(jí)目錄時(shí)的表示位置
首先準(zhǔn)備一個(gè)用于管理每次進(jìn)入下級(jí)目錄時(shí)表示位置的entryPointList,當(dāng)FileListContainer的當(dāng)前目錄發(fā)生變化時(shí),如果新目錄比原目錄長(zhǎng)則判斷為進(jìn)入下級(jí)目錄,這時(shí)保存進(jìn)入位置;如果新目錄比原目錄短,則判斷為返回上級(jí)目錄,這時(shí)回復(fù)進(jìn)入位置。具體代碼如下:
void changeDir(File dir){int firstVisible = getItemPosByVisibleIndex(0);setSelectedItemIndex(-1);sampleItemProvider.setCurrentDir(dir);setItemProvider(sampleItemProvider);if(currentDir == null|| dir.getAbsolutePath().length() > currentDir.getAbsolutePath().length()){entryPointList.add(firstVisible);}else{int listSize = entryPointList.size();if(listSize > 0){int last = entryPointList.remove(listSize - 1);scrollTo(last);}}currentDir = dir;if(dirChangeListener != null){dirChangeListener.dirChanged(dir);}}
參考資料
ListContainer
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-listcontainer-0000001060007847
參考代碼
完整代碼可以從以下鏈接下載:
https://github.com/xueweiguo/Harmony/tree/master/FileBrowser
作者著作介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者去年3月份出版的技術(shù)書(shū)籍,該書(shū)利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過(guò)可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說(shuō)明。這樣一方面可以使讀者了解真實(shí)的軟件開(kāi)發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場(chǎng)景和想要解決的問(wèn)題;另一方面通過(guò)對(duì)這些問(wèn)題的解決過(guò)程進(jìn)行說(shuō)明,讓讀者明白在編寫(xiě)代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。

對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過(guò)本書(shū)可以快速跨越從理解到運(yùn)用的門(mén)檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書(shū)中的示例作為設(shè)計(jì)和開(kāi)發(fā)的參考;使用Python 語(yǔ)言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書(shū)中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎迹?/span>
