<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          自學(xué)HarmonyOS應(yīng)用開發(fā)(68)- 獲取并表示文件系統(tǒng)信息

          接下來我們想通過一個文件瀏覽器應(yīng)用,練習(xí)文件系統(tǒng)操作,文件表示等功能,本文首先說明獲取并表示文件系統(tǒng)內(nèi)容的方法。還是先看演示視頻:

          構(gòu)建FileStore表示畫面

          首先為準備畫面布局:

          <?xml version="1.0" encoding="utf-8"?><xwg.filebrowser.DynamicLayout    xmlns:ohos="http://schemas.huawei.com/res/ohos"    ohos:height="match_parent"    ohos:width="match_parent"    ohos:background_element="$graphic:main_ability_title_background"    ohos:orientation="vertical">    <include        ohos:id="$id:app_bar"        ohos:height="match_content"        ohos:width="match_parent"        ohos:layout="$layout:app_bar_layout"/>    <xwg.filebrowser.fileitems.FileListContainer        ohos:id="$+id:list_container"        ohos:height="0"        ohos:weight="300"        ohos:width="match_parent"        ohos:layout_alignment="left"/>    <xwg.filebrowser.LayoutSeparator        ohos:id="$+id:seperator"        ohos:height="20vp"        ohos:width="match_parent"        ohos:background_element="#7F7F7F"/>    <DirectionalLayout        ohos:id="$+id:detail_view_container"        ohos:width="match_parent"        ohos:height="0"        ohos:weight = "300"/></xwg.filebrowser.DynamicLayout>

          畫面的上半部分是一個定制的FileListContainer類,它的功能有:

          1. 獲取各個層次的文件系統(tǒng)信息并表示

          2. 當用戶選中某個節(jié)點時,觸發(fā)其詳細信息的表示

          FileListContainer集中管理文件系統(tǒng)節(jié)點列表的表示功能。

          在構(gòu)造函數(shù)中做了以下幾件事:

          1. 設(shè)定垂直滾動條格式和有效性

          2. 指定ItemProvider

          3. 指定用于處理選中操作的ListContainer.ItemSelectedListener.

          public class FileListContainer extends ListContainer {    public FileListContainer(Context context, AttrSet attrSet) {        super(context, attrSet);        enableScrollBar(Component.AXIS_Y, true);        setScrollbarThickness(50);        setScrollbarRoundRect(true);        setScrollbarRadius(20);        setScrollbarBackgroundColor(Color.LTGRAY);        setScrollbarColor(Color.DKGRAY);        disableFadeEffect(FadeEffectEnum.FADEEFFECT_SCROLLBAR);        BrowserItemProvider sampleItemProvider = new BrowserItemProvider(context);        setItemProvider(sampleItemProvider);        setItemSelectedListener(itemSelectedListener);    }        public interface SelectedListener{        public void onItemSelected(FileListContainer listContainer, BrowserItem item);    }    SelectedListener selectedListener = null;
              public void setSelectedListener(SelectedListener listener){ selectedListener = listener; }
          ListContainer.ItemSelectedListener itemSelectedListener = new ListContainer.ItemSelectedListener(){ Component prevSelected = null; @Override public void onItemSelected(ListContainer listContainer, Component component, int i, long l) { if(prevSelected != null){ FileListContainer.this.setComponentActive(prevSelected, false); } if(FileListContainer.this.selectedListener != null){ FileListContainer.this.selectedListener.onItemSelected(FileListContainer.this, (BrowserItem)(FileListContainer.this.getItemProvider().getItem(i))); } FileListContainer.this.setComponentActive(component, true); prevSelected = component; }    };
          void setComponentActive(Component component, boolean active){ ShapeElement bg = new ShapeElement(); if(active) { bg.setRgbColor(RgbPalette.LIGHT_GRAY); bg.setShape(ShapeElement.RECTANGLE); component.setBackground(bg); } else{ component.setBackground(getBackgroundElement()); } }}

          這個ItemSelectedListener處理選擇項目的表示狀態(tài)之,將通知轉(zhuǎn)發(fā)給自定義的SelectedListener,讓開發(fā)者可以更簡單地處理BrowserItem實例:

          public class MainAbilitySlice extends AbilitySlice {    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbilitySlice");    private ViewCreateHelper viewCreateHelper;
          @Override public void onStart(Intent intent) { setUIContent(ResourceTable.Layout_browser_ability); initListContainer(); }
          @Override public void onActive() { super.onActive(); }
          @Override public void onForeground(Intent intent) { super.onForeground(intent); }
          FileListContainer.SelectedListener listener = new FileListContainer.SelectedListener() { @Override public void onItemSelected(FileListContainer listContainer, BrowserItem item) { HiLog.info(LABEL, "MainAbilitySlice.onItemSelected, item=%{public}s!", item.getName()); ComponentContainer container = (ComponentContainer)(MainAbilitySlice.this.findComponentById(ResourceTable.Id_detail_view_container)); item.buildView(container); container.invalidate(); } };
          private void initListContainer() { FileListContainer listContainer = (FileListContainer) findComponentById(ResourceTable.Id_list_container); listContainer.setSelectedListener(listener); }}

          ?

          獲取FileStore信息

          示例代碼中定義了一個BaseItemProvider的派生類為FileListContainer 提供數(shù)據(jù),其代碼如下:

          public class BrowserItemProvider extends BaseItemProvider {    private List<BrowserItem> list = new ArrayList<>();    private Context context;    public BrowserItemProvider(Context c) {        this.context = c;        FileSystem fs = FileSystems.getDefault();        Iterable<FileStore> stores = fs.getFileStores();        for(FileStore store : stores) {            list.add(new StoreItem(c, store));        }    }    @Override    public int getCount() {        return list == null ? 0 : list.size();    }    @Override    public Object getItem(int i) {        if (list != null && i >= 0 && i < list.size()){            return list.get(i);        }        return null;    }    @Override    public long getItemId(int i) {        return i;    }    @Override    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {        final Component cpt;        if (component == null) {            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dir_item, null, false);        } else {            cpt = component;        }        BrowserItem item = list.get(i);        Text text = (Text) cpt.findComponentById(ResourceTable.Id_item_index);        text.setText(item.getName());        return cpt;    }}  

          這些都是使用ListContainer的常規(guī)操作。BaseItemProvider和


          生成表示信息

          目前只是通過FileSystems獲取FileStore列表并為每個FileStore實例生成了表示其詳細信息的StoreItem。為了將來可以處理更多的數(shù)據(jù)類型,例如目錄,文件等,這個StoreItem類繼承自下面的BrowserItem類

          public abstract class BrowserItem {    String name;    Context context;    public BrowserItem(Context context, String name) {        this.context = context;        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    abstract public void buildView(ComponentContainer container);}

          BrowserItem類除了有一個name成員之外聲明了一個純虛的用于構(gòu)建詳細信息表示視窗的buildView方法。buildView方法接受一個用于管理UI組件的容器組件,設(shè)計者可以以為這個組件添加下級組件的方式構(gòu)建不同類型item的表示畫面。

          例如StoeItem

          public class StoreItem extends BrowserItem{    FileStore store;    public StoreItem(Context context, FileStore s) {        super(context, s.name());        store = s;    }
          @Override public void buildView(ComponentContainer container) { container.removeAllComponents(); ComponentContainer child_container = (ComponentContainer)LayoutScatter.getInstance(context).parse( ResourceTable.Layout_info_list, null, false); ListContainer list_container = (ListContainer)child_container.findComponentById( ResourceTable.Id_info_list_container); InfoItemProvider provider = new InfoItemProvider(context); try { provider.addItem(new SimpleInfoItem("Name", store.name())); provider.addItem(new SimpleInfoItem("ReadOnly", String.valueOf(store.isReadOnly()))); provider.addItem(new SimpleInfoItem("TotalSpace", String.valueOf(store.getTotalSpace()/1024) + " KB")); provider.addItem(new SimpleInfoItem("UsableSpace", String.valueOf(store.getUsableSpace()/1024) + " KB")); provider.addItem(new SimpleInfoItem("Type", store.type())); } catch (IOException e) { e.printStackTrace(); } list_container.setItemProvider(provider); container.addComponent(child_container); }}

          buildView方法首先構(gòu)建了根據(jù)指定的布局文件生成相應(yīng)的組件,并為其中的ListContainer生成InfoItemProvider示例。接下來從FileStore獲取各種信息并作為列表項添加到InfoItemProvider中。最后將生成的整個布局組件添加到通過參數(shù)指定的容器中即可。


          參考資料

          ListContainer

          https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-listcontainer-0000001060007847

          FileSystems

          https://developer.harmonyos.com/cn/docs/documentation/doc-references/filesystems-0000001054238505

          FileSystem

          https://developer.harmonyos.com/cn/docs/documentation/doc-references/filesystem-0000001054558507

          FileStore

          https://developer.harmonyos.com/cn/docs/documentation/doc-references/filestore-0000001054358485


          參考代碼

          完整代碼可以從以下鏈接下載:

          https://github.com/xueweiguo/Harmony/tree/master/FileBrowser


          作者著作介紹

          《實戰(zhàn)Python設(shè)計模式》是作者去年3月份出版的技術(shù)書籍,該書利用Python 的標準GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設(shè)計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運用設(shè)計模式。

          對設(shè)計模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計和開發(fā)的參考;使用Python 語言進行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。




          覺得本文有幫助?請分享給更多人。

          關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>



          瀏覽 42
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  射一射在线视频 | www国产无码内射 | 一级操B视频 | 99.热| 久热99r视频在线 |