【问题标题】:How to slide the web view in android如何在android中滑动网页视图
【发布时间】:2015-07-15 18:44:32
【问题描述】:

我正在开发一个应用程序,我在其中以不同的 Web 视图显示不同的网页。 目前,我正在使用下一个和上一个按钮分别转到下一页和上一页。

但我想添加滑动手势以转到下一页和上一页。我的意思是当用户从右向左滑动时,下一页应该打开,当用户从左向右滑动时,上一页应该打开。 (就像电子书阅读器)

我在谷歌上尝试过搜索,但我发现幻灯片效果只在 imageview 上,而不是在 webview 上。

请指导我,如何在网页视图中使用手势从当前页面移动到下一页或上一页。

【问题讨论】:

标签: android slider android-webview swipe-gesture android-gesture


【解决方案1】:

您必须覆盖标准 WebView 才能覆盖触摸事件方法。在this 答案中,您可以找到一个示例。

【讨论】:

    【解决方案2】:

    我希望这对你有用。

    private class CustomeGestureDetector extends SimpleOnGestureListener 
        {      
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if(e1 == null || e2 == null) 
                    return false;
                if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) 
                    return false;
                else 
                {
                    try 
                    { 
                        // right to left swipe .. go to next page
                        if(e1.getX() - e2.getX() > 100 && Math.abs(velocityX) > 800) 
                        {
                            if((pg_no-1) == cpg_no)
                            {
                                cpg_no = 0 ;
                                if(contentDetails.size() > (page_no+2))
                                    page_no++ ;
                            }
                            else
                            {
                                cpg_no++;
                            }
                            onPageTurn();
                            Log.e(" right turn ", "go to next page");
                            //do your stuff
                            return true;
                        } //left to right swipe .. go to prev page
                        else if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 800) 
                        {
                            if(0 < (page_no))
                                page_no-- ;
                            onPageTurn();
                            Log.e(" left turn ", "go to prev page");
                            //do your stuff
                            return true;
                        } //bottom to top, go to next document
                        else if(e1.getY() - e2.getY() > 100 && Math.abs(velocityY) > 800 && webview.getScrollY() >= webview.getScale() * (webview.getContentHeight() - webview.getHeight())) 
                        {
                            //do your stuff
                            return false;
                        } //top to bottom, go to prev document
                        else if (e2.getY() - e1.getY() > 100 && Math.abs(velocityY) > 800 ) 
                        {
                            //do your stuff
                            return false;
                        } 
                    } 
                    catch (Exception e) 
                    { 
                        // nothing
                    }
                    return false;
                }
            }
        }
    

    【讨论】:

    • 如何实现里面的activity 能否帮忙提供完整的代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多