6

Okay so I have a Tab view going inside of which I have a webview, a listview and a few other pages. I want to be able to do a SwipeRefreshLayout to refresh each item. I have this working on each page. But, when I scroll down in a webview I can't scroll back up. It triggers the refresh and rebuilds it. I'd like to be able to scroll up in my webview.

Layout stuff

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="20px"
    android:layout_weight="1"
    android:nestedScrollingEnabled="true">


<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=".Splash$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:overScrollMode="ifContentScrolls"
        android:nestedScrollingEnabled="false"/>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_centerVertical="false"
        android:layout_centerHorizontal="true"
        android:visibility="visible" />

</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

The SwipeRefreshLayout is being used inside the fragment for my tabbed layout.

        swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);

        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        swipeLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {

                @Override public void onRefresh() {
                    if(getArguments().getInt(ARG_SECTION_NUMBER) == 4 ) {
                        stringArrayList.clear();
                        new updatetheQueue().execute(ctx);
                    }
                    else {
                        swipeLayout.setRefreshing(false);
                    }
                }});
Dandrews
  • 598
  • 2
  • 9
  • 17

6 Answers6

7

You can use an OnScrollChangedListener to disable SwipeRefreshLayout when the WebView is not scrolled to the top.

// Only enable swipe to refresh if the `WebView` is scrolled to the top.
webView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
  @Override
  public void onScrollChanged() {
     if (webView.getScrollY() == 0) {
         swipeRefreshLayout.setEnabled(true);
      } else {
         swipeRefreshLayout.setEnabled(false);
      }
  }
});
Soren Stoutner
  • 1,413
  • 15
  • 21
0

Getting this working was very challenging. However there is a fairly simple modification that can be made to the layout XML to work around this. It means I can't update the webviews on swipe but thats okay.

<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=".Splash$PlaceholderFragment">

<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:overScrollMode="ifContentScrolls" />

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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=".Splash$PlaceholderFragment">

        <ListView
            android:id="@+id/listView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="false"
            android:visibility="visible" />
    </RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

Dandrews
  • 598
  • 2
  • 9
  • 17
0

you can make a class extend SwipeRefreshLayout and Override canChildScrollUp to sit scro

@Override
public boolean canChildScrollUp() {
    // TODO Auto-generated method stub


        return true; // if you want it to scroll up 

        // check if child Get To Top 

        //return false // if you want it to execute swipe down to refresh listener

}
Yousef Zakher
  • 1,634
  • 15
  • 18
0

If you are using the ViewPager and the tabs are Fragments, you can fix your WebViewFragment with this snippet:

private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;

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

    swipeLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
            new ViewTreeObserver.OnScrollChangedListener() {
                @Override 
                public void onScrollChanged() { 
                    if (mWebView.getScrollY() == 0) 
                        swipeLayout.setEnabled(true); 
                    else 
                        swipeLayout.setEnabled(false); 

                } 
            }); 
} 

@Override 
public void onStop() { 
    swipeLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
    super.onStop(); 
}

More info in Android - SwipeRefreshLayout with empty textview.

Community
  • 1
  • 1
vizZ
  • 1,530
  • 14
  • 14
0

this is how i solve the problem, hope it will help :

mWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                if(mWebView.getScrollY() == 0){
                    mSwipeRefreshLayout.setEnabled(true);
                }else{
                    mSwipeRefreshLayout.setEnabled(false);
                }
            }
        });
Cevin Ways
  • 984
  • 11
  • 13
0

In kotlin:

webView.viewTreeObserver.addOnScrollChangedListener {
    binding.swipeRefreshLayout.isEnabled = webView.scrollY == 0
}
Sunkas
  • 9,542
  • 6
  • 62
  • 102