WebView slow on loading content

[亡魂溺海] 提交于 2019-12-11 07:37:10

问题


I am doing a custom dialog plugin for PhoneGap in Android. The Notification plugin for PhoneGap cannot set text in HTML format in dialog boxes. So I decided to make my own using WebView. But I noticed that when the Alert.Dialog appears, the WebView is initially empty and then the content is loaded a split second later causing the dialog height to "jump". Even when I hardcode the content passed to loadData, the loading of content is still slow.

Is there anything I'm missing?

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    try {
        JSONObject jo = args.getJSONObject(0);
        showCustomDialog(jo.getString("title"), jo.getString("message"), jo.getString("buttonLabel")); 
        return new PluginResult(PluginResult.Status.OK);
    } catch (JSONException e) {
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }
}

private void showCustomDialog(final String dialogTitle, final String dialogMessage, final String dialogButtonLabel) {           

    Runnable runnable = new Runnable() {

        public void run() {                         
            WebView webView = new WebView(ctx);
            webView.loadData(dialogMessage, "text/html", "UTF-8");
        webView.setBackgroundColor(Color.TRANSPARENT);

        AlertDialog.Builder alert = new AlertDialog.Builder(ctx);           
        alert.setView(view)
            .setCancelable(false)
            .setIcon(R.drawable.icon)
            .setTitle(dialogTitle)
            .setPositiveButton(dialogButtonLabel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    }
            )
            .show();
        }
    };
    this.ctx.runOnUiThread(runnable);
}

来源:https://stackoverflow.com/questions/9573850/webview-slow-on-loading-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!