1

I've got a strange issue with a WebView inside a fragment. The horizontal scrolling doesn't work properly. It scrolls only a bit. It's no problem, to scroll the content vertically, pinch to zoom, and yes, moving the page with two fingers while pinching also works in all directions.

This is the onCreateView()-Method of the Fragment, that carries the WebView. I can't use WebViewFragment. This Fragment is inside a Viewpager! The provided data is a valid html5 document. But I've not successfully tried a "real webpage" as well.

this.mView = inflater.inflate(R.layout.search_results, container, false);
        mWebView = (WebView)this.mView.findViewById(R.id.search_results);
        mWebView.setWebViewClient(new SearchResultsWebViewClient());
        mWebView.setInitialScale(120);
        mWebView.setScrollContainer(true);
        mWebView.bringToFront();
        mWebView.setScrollbarFadingEnabled(true);
        mWebView.setVerticalScrollBarEnabled(true);
        mWebView.setHorizontalScrollBarEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setJavaScriptEnabled(false);

        mWebView.loadData(this.data, "text/html", "utf-8");

        return this.mView;

This is the search_results.xml

    <?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id ="@+id/search_results"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</WebView>

And the layout-file in which the fragment is loaded

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
        android:baselineAligned="false"
        android:weightSum="1.0">

    <LinearLayout android:layout_weight="0.5"
                    android:layout_width="0dip"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >        
<ListView android:id="@id/android:list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#000000"
           android:drawSelectorOnTop="false"/>
</LinearLayout>
     <LinearLayout android:layout_weight="0.5"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:orientation="vertical" >
                <FrameLayout android:id="@+id/resultsWebWiewFragmentFrame"
                      android:layout_width="match_parent" 
                      android:layout_height="match_parent"/>
    </LinearLayout>
</LinearLayout>

Because I'm using ABS, it isn't possible to load the fragment directly in the xml file. This'll throw a ClassCastException.

wog
  • 135
  • 3
  • 11

1 Answers1

2

I just searched this and ran into the same question on SO, check out this answer and see if that helps.

He added the following:

mWebView.getSettings().setUseWideViewPort(true);

Yeah It seems like the ViewPager consumes your horizontal scroll, check out these two solutions. Hopefully they help.

Community
  • 1
  • 1
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
  • Hey Asok. No, this makes it even worse. I think this fails with the ViewPager. Either the Pager or the WebView are gathering the horizontal scrolling. Perhaps I am wrong. I've posted the landscape view. In portrait, this WebView is added as a single Fragment to the ViewPager. Have you tried scrolling a WebView horizontally in a WiewPager? This won't work ;) But a viewpager without "paging" doesn't make sense. – wog Jul 26 '12 at 12:48
  • Sorry the first time I researched I didn't include the fact that you used `viewPager`. See edit to my answer above. – jnthnjns Jul 26 '12 at 13:18
  • Hey, no problem. Actually these answeres are solving the problem with the scrolling well. Maybe I've chosen the wrong title. Searching for "WebView inside a ViewPager" makes this question needless ;) As I said, from the perspective as an interface designer, scrolling a WebView in a ViewPager depending from the current scale or other states might not make much sense. I think, that I'll either add Tabs to the portrait view OR play around with this wonderful ActionBarSherlock... – wog Jul 26 '12 at 13:50
  • Glad that helped your horizontal scroll issue. I haven't worked with `ActionBarSherlock` yet, so best of luck! :) – jnthnjns Jul 26 '12 at 14:49