2011-03-20 8 views
7

Estoy en Android 2.2 y estoy creando un diálogo con una WebView en su interior:WebView en un cuadro de diálogo (activos de carga) y no dispuestas

@Override 
protected Dialog onCreateDialog(int id) { 
    Dialog dialog = new Dialog(this); 
    //..... 
    dialog.setContentView(R.layout.dialoghelp); 
    WebView v = (WebView)dialog.findViewById(R.id.helpWebView); 
    v.loadUrl("file:///android_asset/help.html"); 
    //...... 
    return dialog; 
} 

Está funcionando, pero la primera vez que se abren el diálogo de la WebView no se presenta, incluso si el contenido se carga realmente.

Lo sé porque con HierarchyViewer puedo ver el contenido y obligando a una solicitud de diseño llego a verlos en el emulador también. Además, si cancelo el diálogo y lo vuelvo a abrir, todo funciona.

quién está equivocado, Android o yo? Intenté poner la carga en onPrepareDialog() pero es lo mismo.

EDITAR

me cambió params disposición WebView 's fill_parent-wrap_content y de esta manera funciona. Puedo ver que se abre con una altura de 0, luego, después de la carga, crece. El ancho funcionó incluso antes.

+0

me enfrento con el mismo problema, pero el cambio de diseño de parámetro 'wrap_content' no ayuda. ¿Tenías alguna otra solución? – R4j

+0

@ R4j no he estado programando Android para las edades, pero me gustaría tratar de [adjuntar un evento de "carga terminado"] (http://stackoverflow.com/a/3149260/503900) a la vista web y llamando allí el ' requestLayout() '. – bigstones

Respuesta

1

Bueno, yo creo que puede que tenga que sustituir la clase de diálogo, algo así como lo que Facebook SDK ha puesto en marcha: -

aquí hay código

clase pública FbDialog se extiende de diálogo {

static final int FB_BLUE = 0xFF6D84B4; 
static final float[] DIMENSIONS_DIFF_LANDSCAPE = {20, 60}; 
static final float[] DIMENSIONS_DIFF_PORTRAIT = {40, 60}; 
static final FrameLayout.LayoutParams FILL = 
    new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
        ViewGroup.LayoutParams.FILL_PARENT); 
static final int MARGIN = 4; 
static final int PADDING = 2; 
static final String DISPLAY_STRING = "touch"; 
static final String FB_ICON = "icon.png"; 

private String mUrl; 
private DialogListener mListener; 
private ProgressDialog mSpinner; 
private ImageView mCrossImage; 
private WebView mWebView; 
private FrameLayout mContent; 

public FbDialog(Context context, String url, DialogListener listener) { 
    super(context, android.R.style.Theme_Translucent_NoTitleBar); 
    mUrl = url; 
    mListener = listener; 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mSpinner = new ProgressDialog(getContext()); 
    mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    mSpinner.setMessage("Loading..."); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    mContent = new FrameLayout(getContext()); 

    /* Create the 'x' image, but don't add to the mContent layout yet 
    * at this point, we only need to know its drawable width and height 
    * to place the webview 
    */ 
    createCrossImage(); 

    /* Now we know 'x' drawable width and height, 
    * layout the webivew and add it the mContent layout 
    */ 
    int crossWidth = mCrossImage.getDrawable().getIntrinsicWidth(); 
    setUpWebView(crossWidth/2); 

    /* Finally add the 'x' image to the mContent layout and 
    * add mContent to the Dialog view 
    */ 
    mContent.addView(mCrossImage, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    addContentView(mContent, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
} 

private void createCrossImage() { 
    mCrossImage = new ImageView(getContext()); 
    // Dismiss the dialog when user click on the 'x' 
    mCrossImage.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mListener.onCancel(); 
      FbDialog.this.dismiss(); 
     } 
    }); 
    Drawable crossDrawable = getContext().getResources().getDrawable(R.drawable.close); 
    mCrossImage.setImageDrawable(crossDrawable); 
    /* 'x' should not be visible while webview is loading 
    * make it visible only after webview has fully loaded 
    */ 
    mCrossImage.setVisibility(View.INVISIBLE); 
} 

private void setUpWebView(int margin) { 
    LinearLayout webViewContainer = new LinearLayout(getContext()); 
    mWebView = new WebView(getContext()); 
    mWebView.setVerticalScrollBarEnabled(false); 
    mWebView.setHorizontalScrollBarEnabled(false); 
    mWebView.setWebViewClient(new FbDialog.FbWebViewClient()); 
    mWebView.getSettings().setJavaScriptEnabled(true); 
    mWebView.loadUrl(mUrl); 
    mWebView.setLayoutParams(FILL); 
    mWebView.setVisibility(View.INVISIBLE); 
    mWebView.getSettings().setSavePassword(false); 

    webViewContainer.setPadding(margin, margin, margin, margin); 
    webViewContainer.addView(mWebView); 
    mContent.addView(webViewContainer); 
} 

private class FbWebViewClient extends WebViewClient { 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     Util.logd("Facebook-WebView", "Redirect URL: " + url); 
     if (url.startsWith(Facebook.REDIRECT_URI)) { 
      Bundle values = Util.parseUrl(url); 

      String error = values.getString("error"); 
      if (error == null) { 
       error = values.getString("error_type"); 
      } 

      if (error == null) { 
       mListener.onComplete(values); 
      } else if (error.equals("access_denied") || 
         error.equals("OAuthAccessDeniedException")) { 
       mListener.onCancel(); 
      } else { 
       mListener.onFacebookError(new FacebookError(error)); 
      } 

      FbDialog.this.dismiss(); 
      return true; 
     } else if (url.startsWith(Facebook.CANCEL_URI)) { 
      mListener.onCancel(); 
      FbDialog.this.dismiss(); 
      return true; 
     } else if (url.contains(DISPLAY_STRING)) { 
      return false; 
     } 
     // launch non-dialog URLs in a full browser 
     getContext().startActivity(
       new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
     return true; 
    } 

    @Override 
    public void onReceivedError(WebView view, int errorCode, 
      String description, String failingUrl) { 
     super.onReceivedError(view, errorCode, description, failingUrl); 
     mListener.onError(
       new DialogError(description, errorCode, failingUrl)); 
     FbDialog.this.dismiss(); 
    } 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     Util.logd("Facebook-WebView", "Webview loading URL: " + url); 
     super.onPageStarted(view, url, favicon); 
     mSpinner.show(); 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     super.onPageFinished(view, url); 
     mSpinner.dismiss(); 
     /* 
     * Once webview is fully loaded, set the mContent background to be transparent 
     * and make visible the 'x' image. 
     */ 
     mContent.setBackgroundColor(Color.TRANSPARENT); 
     mWebView.setVisibility(View.VISIBLE); 
     mCrossImage.setVisibility(View.VISIBLE); 
    } 
} 

}

+0

¿Podría señalar las líneas que resolverían el problema? – bigstones

+0

@bigstones - por lo que el ejemplo que he publicado no tiene ninguna línea específica que resuelve su problema .., en primer lugar onCreateDialog está en desuso, también en la actividad que están tratando de establecer el contenido de diálogo con miras Web, intente anular el diálogo clase y en eso estableces el contenido como vista web ... –

Cuestiones relacionadas