I'm trying to show a web page in a fragment that is inside a viewpager, but when I swipe to that fragment, it is empty and shows a blank page. I have searched this a lot and all solutions I found suggest the code I have. The app has internet permission (and it works, because it loads data from DB)
This is the fragment layout XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ubiqme.ubiqme.fragments.ReservationFragment">
<WebView
android:id="@+id/webview_reservation"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
And this is the fragment source:
public class ReservationFragment extends Fragment {
public ReservationFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_resevation, container, false);
WebView webView = (WebView) rootView.findViewById(R.id.webview_reservation);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
webView.loadUrl(getString(R.string.ubiqme_web));
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
return rootView;
}
}
This fragment (reservationFragment) is added to the viewPager here:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new TicketsFragment(), "");
adapter.addFragment(new KmFragment(), "");
adapter.addFragment(new ReservationFragment(), "");
viewPager.setAdapter(adapter);
}
Hope someone can help me and thanks in advance.
检查这段代码我不知道这段代码会解决你的问题,如果你的标签大小是3然后把2作为参数,否则把标签-1的数量作为参数
viewPager.setOffscreenPageLimit(2);
I feel so dumb about this, but I just found out that the url I was trying to open in the webView (and the others I tried) had no "http://" before the url. After adding it the webview works like a charm.
Sorry
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.