2016-07-12 10 views
1

Ich muss mein Webview aktualisieren, weil nichts angezeigt wird, bis ich den Bildschirm rotiere.Wie aktualisiere ich Webview?

Mein Webview

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.myfragment3, container, false); 
    webview = (WebView)v.findViewById(R.id.WebView); 
    webview.getSettings().setJavaScriptEnabled(true); 
    webview.getSettings().setDomStorageEnabled(true); 
    webview.setLayerType(View.LAYER_TYPE_HARDWARE, null); 
    webview.setWebChromeClient(new WebChromeClient()); 
    String url = "http://www.acr-sl.com/catalogo/1/1/1"; 
    webview.loadUrl(url); 
    return v; 
} 

UPDATE: Schließlich dieses Problem gelöst, die Webansicht von 0 ina neuen Fragment zu schaffen, das einzige, was verändert es waren die Layouts so dass das Problem von dort abgeleitet wurde nehme ich an.

Antwort

0

meine Layout-Datei fragment_meru_detail ist als

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 
<ProgressBar 

    android:id="@+id/progressBar" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/pageload" 
     android:layout_gravity="center" 
     android:text="Loding........" 
     /> 

<WebView 

    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:id="@+id/meruweb" 
    ></WebView> 
</LinearLayout> 

Meine Java-Klasse ist als:

package com.rayz.digitalpostbox.fragments; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.rayz.digitalpostbox.R; 
import com.rayz.digitalpostbox.activities.HomeActivity; 


public class MeruDetail extends Fragment { 

    TextView pageload ; 
    WebView webview ; 
    private ProgressBar progres; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView = inflater.inflate(R.layout.fragment_meru_detail, container, false); 
     ((HomeActivity)getActivity()).setTitle("MERU Cabs"); 
     webview = (WebView) rootView.findViewById(R.id.meruweb); 
     pageload = (TextView) rootView.findViewById(R.id.pageload); 
     progres = (ProgressBar)rootView.findViewById(R.id.progressBar); 
     progres.setMax(100); 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); 
     webview.getSettings().setSupportZoom(true); 
     webview.loadUrl("http://merucabs.com/mobile/"); 




     webview.setWebChromeClient(new WebChromeClient() { 

      public void onProgressChanged(WebView view, int progress) { 
       MeruDetail.this.setValue(progress); 
       super.onProgressChanged(view, progress); 
       if(progress==100){ 
        progres.setVisibility(View.GONE); 
       }else { 
        progres.setVisibility(View.VISIBLE); 
       } 

       if(progress>40){ 
        pageload.setVisibility(View.GONE); 
       } 
      } 

     }); 

     webview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 


     MeruDetail.this.progres.setProgress(0); 

     return rootView ; 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
    } 

    public void setValue(int progress) { 
     this.progres.setProgress(progress); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     ((HomeActivity)getActivity()).setTitle("Dashboard"); 
    } 
} 

Und ich denke, Sie auch in Ihrem Mainfest-Datei für den Internet-Berechtigung überprüfen sollten

<uses-permission android:name="android.permission.INTERNET" /> 
+0

Danke, immer noch daran arbeiten, es muss etwas dumm sein, weil das Webview selbst funktioniert, wenn ich die Ausrichtung von vertikal zu horizontal ändern oder umgekehrt das WebVIEW ist in Ordnung . – Xaloju

0

diese Weise können Sie für Ihre WebView webview.reload()

+0

Bereits versucht, funktioniert nicht für mich: s thx – Xaloju

0

Sie können webView.reload() aber bewusst sein, diese eine Seite reposts, wenn die Anfrage war POST, so funktioniert nur korrekt mit GET. Wenn Ihre URL statisch oder gleich ist, sollten Sie loadURL erneut verwenden, es ist besser.

YourActivity.this.webView.loadUrl("http://www.website.com"); 
0

unten arbeitet Code für Ihre Anforderung haben Blick und setzen es als>

Layout-Datei wie:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.lenovo.webview.MainActivity"> 

    <WebView 
     android:id="@+id/WebView" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:text="Hello World!" 
     app:layout_constraintBottom_toBottomOf="@+id/activity_main" 
     app:layout_constraintLeft_toLeftOf="@+id/activity_main" 
     app:layout_constraintRight_toRightOf="@+id/activity_main" 
     app:layout_constraintTop_toTopOf="@+id/activity_main" /> 

</android.support.constraint.ConstraintLayout> 

Mainfest-Datei ist als:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.lenovo.webview"> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

MainActivity .java ist wie folgt:

package com.example.lenovo.webview; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 

public class MainActivity extends AppCompatActivity { 
    WebView webview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     webview = (WebView) findViewById(R.id.WebView); 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); 
     webview.getSettings().setSupportZoom(true); 
     webview.loadUrl("http://www.acr-sl.com/catalogo/1/1/1"); 
    } 
} 
+0

Versucht Ihren Code, aber löste das Problem nicht, ich benutze fragmen Vielleicht ist das das Problem? – Xaloju

+0

Es funktioniert auch in Fragment-Look unten Ich poste es für Fragment – Teekam