当MapView可见时,不会调用WebView onPageFinished

时间:2016-02-29 09:48:12

标签: android webview

我在片段中有MapView,可以创建WebView和加载网址。

如果WebView被隐藏(MapView),则android:visibility="gone"加载正常。但是当它变得可见时,会调用WebView的onPageStarted(),但永远不会调用onPageFinished()

MapView的:

<com.google.android.gms.maps.MapView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:visibility="gone"
    android:id="@+id/mapView" />

web视图:

webView = new WebView(getActivity());
webView.addJavascriptInterface(this, "scraper");
WebSettings webSettings = webView.getSettings();
webSettings.setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return true;
    }


    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        System.out.println("page start called");
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        System.out.println("page finished called");
    }
});

webView.loadUrl("http://denver.craigslist.org/apa/5436947521.html");

几点观察:

  1. 如果我隐藏了MapView,WebView加载正常。

  2. onPageStarted()之后的日志消息 - D / cr_Ime:[ImeAdapter.java:587]分离

  3. onReceivedError()中没有任何内容或任何其他错误回调。

2 个答案:

答案 0 :(得分:2)

我这样做了,它对我来说很好。

我使用MapFragment代替MapView

<强> fragment_location.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

<强> LocationFragment.java

public class LocationFragment extends Fragment implements OnMapReadyCallback {

private static View view;
private GoogleMap googleMap;
private double latitude = 23.0300, longitude = 72.5800;
private  WebView webView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try
    {
        view = inflater.inflate(R.layout.fragment_location,null);

        MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }catch (InflateException e) {
    /* map is already there, just return view as it is */
    }

    return view;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    LatLng mLatLong = new LatLng(latitude, longitude);

    googleMap.setMyLocationEnabled(true);
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLong, 13));

    googleMap.addMarker(new MarkerOptions()
            .title("MyTitle")
            .snippet("MySnippet")
            .position(mLatLong));

    if(webView!=null){
        webView.requestFocus();
    }
}


@Override
public void onResume() {
    super.onResume();

    webView = new WebView(getActivity());
    webView.addJavascriptInterface(this, "scraper");
    WebSettings webSettings = webView.getSettings();
    webSettings.setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.i("WebView", "page start called");
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
           Log.i("WebView", "page finished called");
        }
    });

    webView.loadUrl("http://stackoverflow.com/");
}

}

就我而言onPageStarted&amp; onPageFinished两者都被触发了。

<强>截图

enter image description here

编辑1:实际上MapView始终在初始化时始终关注,因此一旦MapView初始化过程完成请求重点关注WebView

希望这会对你有所帮助。

答案 1 :(得分:-1)

Android Webview不支持地图,这就是它不显示的原因。