Android WebView in different tabs

I am working on a project with "android.support.v4.view.ViewPager". There are two tabs in each tab, and I want to show a different WebView. Here is the code for one of these tabs and layout:

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> 

The problem is that on the first tab, WebView loads differently. On another tab, which is declared the same way, I cannot focus the elements, and sometimes I get some locks. If I am on the second tab that has an error and I switch the orientation to Landscape, the second tab loads fine, but when I switch to the first tab, I get an error. Here is the LogCat error:

 11-18 09:00:37.460: E/webcoreglue(29444): Should not happen: no rect-based-test nodes found 
+4
source share
3 answers

It looks like an error, either in ViewPager or WebView , on Android 4.1 (and possibly higher).

Alas, I have no workaround for having a WebView on the second or subsequent pages of the pager.

+1
source

In Java, create MyWebView as a subclass of 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); } } 

In xml layout using MyWebView instead of 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
source

I have a WebView inside a ViewPager and fixed this problem by overriding onTouchEvent() , extending the WebView class.

Here is my code:

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

Hooray!

0
source

Source: https://habr.com/ru/post/1446736/


All Articles