HarmonyOS學習路之開發篇—Java UI框架(基礎組件說明【三】)

接上一篇文章…

上兩篇文章已經介紹了大部分的Java UI組件 ,因為時間關系把一個內容分為了三個部分,這是最后一篇組件的介紹。分別介紹ListContainer、ScrollView和WebView。

二、分類說明

?ListContainer

ListContainer是用來呈現連續、多行數據的組件,包含一系列相同類型的列表項。

ListContainer和ScrollView在介紹嵌套開發時使用到了其中的一些屬性,但是沒有具體的介紹說明。

ListContainer的共有XML屬性繼承自:Component,其基礎屬性就不再過多的贅述。ListContainer的自有XML屬性見下表:

屬性名稱 屬性描述 使用案例
rebound_effect 開啟/關閉回彈效果 ohos:rebound_effect="true"
shader_color 著色器顏色 ohos:shader_color="#A8FFFFFF"
orientation 列表項排列方向 ohos:orientation="horizontal" or ohos:orientation="vertical"

使用ListContainer時,需要編寫適配器來構造不同的數據結構,在編寫適配器時需要注意:要繼承自BaseItemProvider并重寫以下方法;

方法 描述
int getCount() 返回填充的表項個數。
Object getItem(int position) 根據position返回對應的數據。
long getItemId(int position) 返回某一項的id。
Component getComponent(int position, Component covertComponent,ComponentContainer componentContainer) 根據position返回對應的界面組件。

?ScrollView

ScrollView是一種帶滾動功能的組件,它采用滑動的方式在有限的區域內顯示更多的內容。

ScrollView的共有XML屬性繼承自:StackLayout,ScrollView的自有XML屬性見下表:

屬性名稱 屬性描述 使用案例
match_viewport 是否拉伸匹配(boolean類型) ohos:match_viewport="true"
rebound_effect 回彈效果(boolean類型) ohos:rebound_effect="true"

ScrollView的速度、滾動、回彈等常用接口如下:

方法 描述
doFling(int velocityX, int velocityY)
doFlingX(int velocityX)
doFlingY(int velocityY) 設置X軸和Y軸滾動的初始速度,單位(px)
fluentScrollBy(int dx, int dy)
fluentScrollByX(int dx)
fluentScrollByY(int dy) 沿坐標軸將內容平滑地移動指定數量的像素,單位(px)
fluentScrollTo(int x, int y)
fluentScrollXTo(int x)
fluentScrollYTo(int y) 根據指定坐標平滑滾動到指定位置,單位(px)
setReboundEffect(boolean enabled) 設置是否啟用回彈效果,默認false
setReboundEffectParams(int overscrollPercent, float overscrollRate, int remainVisiblePercent) 配置回彈效果
setReboundEffectParams(ReboundEffectParams reboundEffectParams) overscrollPercent:過度滾動百分比,默認值40
setOverscrollPercent(int overscrollPercent) overscrollRate:過度滾動率,默認值0.6
setOverscrollRate(float overscrollRate) remainVisiblePercent:應保持可見內容的最小百分比,默認值20
setRemainVisiblePercent(int remainVisiblePercent)

?WebView

WebView提供在應用中集成Web頁面的能力。

在使用WebView時需要配置應用的網絡權限。打開“entry > src > main > config.json”,并添加如下配置。

{
  ...
  "module": {
    ...
    "reqPermissions": [
      {
        "name": "ohos.permission.INTERNET"
      }
    ],
    ...
  }
}

與Android中的Webview有著一樣的功能,顯示相應的網頁數據,并且可以與JavaScript代碼相互調用。

通過WebConfig啟用JavaScript

webView.getWebConfig().setJavaScriptPermit(true);

使用回調方法或者應用內部調用JavaScript方法。

  • 注入回調對象到頁面內容
final String jsName = "JsCallbackToApp";
webView.addJsCallback(jsName, new JsCallback() {
    @Override
    public String onCallback(String msg) {
        // 增加自定義處理
        return "jsResult";
    }
});
//---------------------------------------------
function callToApp() {
    if (window.JsCallbackToApp && window.JsCallbackToApp.call) {
       var result = JsCallbackToApp.call("message from web");
    }
}
  • 在應用內調用頁面內的JavaScript方法
webView.executeJs("javascript:callFuncInWeb()", new AsyncCallback<String>() {
    @Override
    public void onReceive(String msg) {
        // 在此確認返回結果
    }
});

觀測Web狀態

通過setWebAgent方法設置自定義WebAgent對象,以觀測頁面狀態變更等事件:

webView.setWebAgent(new WebAgent() {
    @Override
    public void onLoadingPage(WebView webview, String url, PixelMap favicon) {
        super.onLoadingPage(webview, url, favicon);
        // 頁面開始加載時自定義處理
    }

    @Override
    public void onPageLoaded(WebView webview, String url) {
        super.onPageLoaded(webview, url);
        // 頁面加載結束后自定義處理
    }

    @Override
    public void onLoadingContent(WebView webview, String url) {
        super.onLoadingContent(webview, url);
        // 加載資源時自定義處理
    }

    @Override
    public void onError(WebView webview, ResourceRequest request, ResourceError error) {
        super.onError(webview, request, error);
        // 發生錯誤時自定義處理
    }
});
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容