我在MainActivity
内有一个WebView,打算添加OnSwipeDown
事件监听器,以便当用户在屏幕上向下滑动时,WebView
尝试重新加载URL,如果加载失败。大多数浏览器都采用了这种实现方式...
我该怎么办...
到目前为止,我有这个带有参数的OnFling()
方法,但是我不知道当用户触摸屏幕并向下滑动时如何让它监听...
这是源代码
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
bool result = false;
int SWIPE_THRESHOLD = 80;
int SWIPE_VELOCITY_THRESHOLD = 80;
try
{
float diffY = e2.GetY() - e1.GetY();
float diffX = e2.GetX() - e1.GetX();
if (Math.Abs(diffX) > Math.Abs(diffY))
{
if (Math.Abs(diffX) > SWIPE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
{
if (diffX > 0)
{
//code for swipe right here, Experimented on this, It didn't work
string url="www.youtube.com";
webview.loadurl("https://"+url);
}
else
{
//code for swipe Left here, reload webview with google url
string loc="www.google.com";
webview.loadurl("https://"+loc);
//That above didnt work
}
}
}
}
catch (Exception exception)
{
//exception.printStackTrace();
}
return result;
}
我是否使用变量diffY
检查向下滑动以重新加载?