刷卡android webView

时间:2017-04-19 17:09:41

标签: android webview swiperefreshlayout

我的android studio app上有webView

{$MODE Delphi}{$ifdef net}
net,
{$else}
NewKernel,
{$ifndef st}
{$ifndef neter}
hyper,
{$endif}
{$endif}
{$endif}

我怎么能刷卡刷新呢?

2 个答案:

答案 0 :(得分:0)

看看这个:https://stackoverflow.com/a/27916575/5627123。 正如你在那里看到的,你可以很容易地做到这一点。只需将webview放在SwipeToRefreshLayout中,然后使用链接中的代码。

答案 1 :(得分:0)

在下面找到一种简单的方法:[1]复制简单的OnTouchListener,然后[2]将OnTouchListener连接到Webview。 [3]“ this”表示您实现TouchListener()接口的方法。因此,您可以通过onSwipeRight()和onSwipeLeft()方法实现“刷新”。

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

TouchListener可能像这样简单:

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

简单的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;
        }
    }
}