Android WebView在不同的标签中

时间:2012-11-18 07:08:27

标签: android tabs webview swipe

我正在使用" android.support.v4.view.ViewPager"处理项目。有两个选项卡,在每个选项卡中,我想显示不同的WebView。以下是其中一个标签及其布局的代码:

TabOne.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加载的。在另一个标签上,这是完全相同的声明,我无法关注元素,有时我会得到一些强制关闭。如果我在第二个选项卡上有错误,并且我将方向切换为横向,则第二个选项卡完全加载,但是当我切换到第一个选项卡时,我收到错误。这是LogCat错误:

11-18 09:00:37.460: E/webcoreglue(29444): Should not happen: no rect-based-test nodes found

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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" />

答案 2 :(得分:0)

我在WebView中有ViewPager并通过扩展onTouchEvent()类覆盖WebView来解决此问题。

这是我的代码:

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

干杯!