我正在开发一个带有“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 4.1上的a bug, either in ViewPager
or WebView
(也许更高)。
唉,我没有办法让WebView
在寻呼机的第二页或后续页面上。
在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" />
我在WebView
内部有ViewPager
并通过扩展onTouchEvent()
类来覆盖WebView
来解决这个问题。
这是我的代码:
@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) {
requestFocusFromTouch(); } return super.onTouchEvent(event); }
干杯!