Question

I am developing an application where I am showing different web pages in different web views. At present, I am using next and previous button to go to next and previous page respectively.

But I want to add slide gesture to go to next and previous page. I mean when user slide from right to left then next page should open and when the user slide from left to right then previous page should open. (Like the e book reader)

I have tried and searched on google but I found the slide effect only on imageview, not on webview.

Please guide me , how can I move from current to the next or previous page using gestures in web view.

Était-ce utile?

La solution

You have to override the standard WebView to be able to override touch events methods. In this answer you can find an example.

Autres conseils

I hope this is useful for you.

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;
            }
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top