Reputation: 342
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.
Upvotes: 1
Views: 771
Reputation: 342
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
Upvotes: 1
Reputation: 56
check this code I don't know this code will solve your problem , if your tab size is 3 then put 2 as parameter,otherwise put number of tab -1 as parameter
viewPager.setOffscreenPageLimit(2);
Upvotes: 0