androidandroid-webviewandroid-viewpager

WebView not loading with ViewPager


I'm trying to build a ViewPager which contains a bunch of WebViews. I'm able to get the ViewPager working, however, only the initial WebView is loading it's content. When I swipe to the other views, they are coming up blank. No error message or anything. When I breakpoint the code, the loadUrl is being hit with the correct Url, so not sure what is going on:

Activity

public class BrowserPager extends SherlockFragmentActivity {
    private  List <String> urls;
    private static int NUMBER_OF_PAGES;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        setContentView(R.layout.browser_pager);

        urls = GetUrls();
        NUMBER_OF_PAGES = urls.getCount();

        mViewPager = (ViewPager)findViewById(R.id.pager);
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mMyFragmentPagerAdapter);
    }

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter  {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override
        public Fragment getItem(int index) {            
            return Browser.newInstance(urls.get(index));
        }

        @Override
        public int getCount() {  
             return NUMBER_OF_PAGES;  
        }
   }
}

Fragment

public class Browser extends SherlockFragment {

    private String mUrl;
    private WebView mWebView;

    public static Browser newInstance(String url) {

        Browser b = new Browser();
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        b.setArguments(bundle);

        return b;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (getArguments() != null)
            mUrl = getArguments().getString("url");     

        return inflater.inflate(R.layout.browser, container, false);
    }

    @Override
    public void onResume()
    { 
        super.onResume();

        DisplaySite();
    }   

    private void DisplaySite() {

        mWebView = (WebView)getActivity().findViewById(R.id.webview);

        mWebView.getSettings().setSupportZoom(true);  
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.loadUrl(mUrl);

        final ProgressBar progess = (ProgressBar)getActivity().findViewById(R.id.ProgressBar);

        mWebView.setWebViewClient(new WebViewClient() {
              public void onPageStarted(WebView view, String url, Bitmap favicon) {
                progess.setVisibility(View.VISIBLE);
                progess.setProgress(0);
              }

              public void onPageFinished(WebView view, String url) {
                progess.setVisibility(View.GONE);
              }

        });

        mWebView.setWebChromeClient(new WebChromeClient() {

              public void onProgressChanged(WebView view, int progress) {
                  progess.setProgress(progress);
              }
        });
    }   
}

Solution

  • The problem is probably the following line:

    mWebView = (WebView)getActivity().findViewById(R.id.webview);
    

    Here you're asking the Activity (!) to find the first view with id R.id.webview. Now think what happens when there are multiple instances of the same Browser fragment attached to the Activity... Exactly, always the first occurrence with that id is returned. In other words: the url will always be loaded into the WebView of the first attached Browser fragment.

    Rather than inflating the WebView from the Activity, you'll want to inflate the WebView relevant to each Browser fragment by finding the view in the fragment's root view:

    mWebView = (WebView) getView().findViewById(R.id.webview);
    

    Basically you need to use the correct scope for the view lookup.