Android WebView在不同的标签中

问题描述 投票:4回答:3

我正在开发一个带有“android.support.v4.view.ViewPager”的项目。有两个选项卡,在每个选项卡中,我想显示不同的WebView。以下是其中一个选项卡的代码及其布局:

tab one.Java

public class TabOne extends Fragment
{
 WebView myWebView;
 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
        View view = inflater.inflate(R.layout.tabone, container, false);
            WebView myWebView = (WebView) view.findViewById(R.id.webview);
    myWebView.loadUrl("file:///android_asset/help/index.html");
    return view;
}
}

tabone.xml

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

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/webview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />

</LinearLayout>

问题是在第一个选项卡中,WebView是corectly加载的。在另一个Tab上,这是完全相同的声明,我无法集中元素,有时我会得到一些强制关闭。如果我在第二个选项卡上,有错误,我将方向切换为横向,第二个选项卡完全加载,但当我切换到第一个选项卡时,我得到错误。这是LogCat错误:

11-18 09:00:37.460: E/webcoreglue(29444): Should not happen: no rect-based-test nodes found
android tabs webview swipe
3个回答
1
投票

这似乎是在Android 4.1上的a bug, either in ViewPager or WebView(也许更高)。

唉,我没有办法让WebView在寻呼机的第二页或后续页面上。


0
投票

在Java中,将MyWebView创建为WebView的子类

public class MyWebView extends WebView {

    public MyWebView(Context context) {
        super(context);
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        onScrollChanged(getScrollX(), getScrollY(), getScrollX(), getScrollY());
        return super.onTouchEvent(event);
    }
}

在布局xml中,使用MyWebView而不是WebView

<your.package.MyWebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

0
投票

我在WebView内部有ViewPager并通过扩展onTouchEvent()类来覆盖WebView来解决这个问题。

这是我的代码:

@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) {
requestFocusFromTouch(); } return super.onTouchEvent(event); }

干杯!

© www.soinside.com 2019 - 2024. All rights reserved.