0

i have webView on my android studio app

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

    mywebview = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = mywebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mywebview.loadUrl("https://m.site.com/");
    mywebview.setWebViewClient(new WebViewClient());

how i can make Swipe Refresh it?

Derick
  • 11
  • 2
  • Does this answer your question? [SwipeRefreshLayout + WebView when scroll position is at top](https://stackoverflow.com/questions/24658428/swiperefreshlayout-webview-when-scroll-position-is-at-top) – Jeremy Caney Jan 02 '22 at 17:36

3 Answers3

0

Take a look at this: https://stackoverflow.com/a/27916575/5627123. As you can see there you can do this very easily. Just place the webview inside of a SwipeToRefreshLayout and use the code from the link.

Community
  • 1
  • 1
Z3r0byte
  • 193
  • 1
  • 2
  • 8
0

Below you find a simple approach: [1] copy the simple OnTouchListener and [2] connect the OnTouchListener to your Webview. [3] The 'this' means that you implement the methods of the TouchListener() interface. So, you can implement YOUR 'refresh' via the methods onSwipeRight() and onSwipeLeft().

webView.setOnTouchListener( new OnSwipeWebviewTouchListener( getActivity(), this));

The TouchListener could be as simple as this one:

public interface TouchListener  {
    default void onSwipeLeft() {
        Logger.d( "Swipe left");
    }
    default void onSwipeRight() {
        Logger.d( "Swipe right");
    }
}

The simple OnTouchListener:

public class OnSwipeWebviewTouchListener implements View.OnTouchListener {
    private final GestureDetector gestureDetector;
    public OnSwipeWebviewTouchListener(Context ctx, TouchListener touchListener) {
        gestureDetector = new GestureDetector(ctx, new GestureListener(touchListener));
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
        private TouchListener touchListener;
        GestureListener(TouchListener touchListener) {
            super();
            this.touchListener = touchListener;
        }
        @Override
        public boolean onDown(MotionEvent e) {
            return false;  // THIS does the trick
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    // You can customize these settings, so 30 is an example
                    if (Math.abs(diffX) > 30 && Math.abs(velocityX) > 30) {
                        if (diffX > 0) {
                            touchListener.onSwipeRight();
                        } else {
                            touchListener.onSwipeLeft();
                        }
                        result = true;
                    }
                } else {
                    result = false;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
}
tm1701
  • 7,307
  • 17
  • 79
  • 168
0

Derick! https://stackoverflow.com/a/27916575/5627123 as mentioned from Z3r0byte is one (quite good!) approach where you need to to the manual integration path. If you use WebViewGold, you can also switch ENABLE__PULL_REFRESH to true in the Config.java config file.

  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/30722010) – TeachMeJava Jan 07 '22 at 14:06