2012-06-22 57 views
0

我試圖在兩個包含webview的不同活動之間切換。我在切換時遇到了很多內存問題,現在我正在嘗試以下方法:在同一個佈局中設置VISIBLE或GONE兩個WebView。 當我按下webviewA的一個按鈕時,在webviewB上加載一個url,當webviewB onPageFinished(),A被設置爲GONE並且B被設置爲VISIBLE時。從B到A的問題同樣是URL只加載第一次......設置在相同佈局上的webviews之間的android切換

2個網頁視圖是在相同的佈局等設定,

<RelativeLayout 
       android:id="@+id/aLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_below="@+id/barraSocial"> 

       <WebView 
        android:id="@+id/webviewA" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:autoLink="web" 
        android:textColor="@android:color/black" 
        android:scrollbars="none"/> 
      </RelativeLayout> 

      <RelativeLayout 
       android:id="@+id/bLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_below="@+id/barraSocial"> 
       <WebView 
        android:id="@+id/webviewB" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:autoLink="web" 
        android:textColor="@android:color/black" 
        android:scrollbars="none"/> 
      </RelativeLayout> 

的onCreate活動,

上webviewA
final RelativeLayout LayoutA = (RelativeLayout)findViewById(R.id.aLayout); 
final RelativeLayout LayoutB = (RelativeLayout)findViewById(R.id.bLayout); 

webviewA = (WebView) findViewById(R.id.webviewA); 
webviewB = (WebView) findViewById(R.id.webviewB); 

按一下按鈕,

webviewB.loadUrl(UrlVista); 

然後,onPageFinished()從webviewB被激發,

webviewB.setWebViewClient(new WebViewClient() 
     { 
        @Override 
        public void onPageFinished(WebView view, String url) 
        { 
         LayoutA.setVisibility(View.GONE); 
         LayoutB.setVisibility(View.VISIBLE); 
+0

你有webview b按鈕點擊和webviewA.setWebViewClient實施? – trumpetlicks

+0

是的,已經實施!與發佈相同,但從B到A – Jaume

+0

當您說URL只是第一次加載時,是指初始化URL,還是第一次單擊按鈕後的URL?你也有2個相對的佈局與填充父母。第一個定義是贏。當你按下第一個按鈕,並且網址與新網站一起出現時,它甚至不在屏幕上! – trumpetlicks

回答

2

您有兩個RelativeLayouts。其中一個實質上是推動另一個屏幕。而不是你有什麼,只是有2個網頁視圖一個相對佈局:

在XML文件中:

<RelativeLayout android:id="@+id/aLayout"     
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent"     
    android:layout_below="@+id/barraSocial"> 

    <WebView android:id="@+id/webviewA"      
     android:layout_width="fill_parent"         
     android:layout_height="fill_parent"      
     android:autoLink="web"      
     android:textColor="@android:color/black"      
     android:scrollbars="none" 
     android:alignParentLeft="true" 
     android:alignParentTop="true"//> 

    <WebView android:id="@+id/webviewB"      
     android:layout_width="fill_parent"         
     android:layout_height="fill_parent"      
     android:autoLink="web"      
     android:textColor="@android:color/black"      
     android:scrollbars="none" 
     android:alignParentLeft="true" 
     android:alignParentTop="true"/> 

</RelativeLayout> 
在onPageFinished

webviewB.setWebViewClient(new WebViewClient(){ 
    @Override 
    public void onPageFinished(WebView view, String url{        
     webviewA.setVisibility(View.GONE); 
     webviewB.setVisibility(View.VISIBLE); 
    } 
}); 

這基本上已經在設置知名度

然後的WebViews本身而不是佈局。在這種情況下,佈局可以彼此重疊,並且使用它們的可見性將基本上關閉和打開。