简体   繁体   中英

How to Display the Html content page wise using webview in android?

hi i create simple app to display html page in webview i use the webview and display the page load time like this.

显示第一次加载数据

After this Disable the scroll and use the next and previous button to back and forward contain. So my code is below.

First onCreate display add webview and load the html file.

        mainWebView = (WebView) findViewById(R.id.mainWebView);
    mainWebView.setVerticalScrollBarEnabled(false);
    mainWebView.setHorizontalScrollBarEnabled(false);
    mainWebView.getSettings().setJavaScriptEnabled(true);
    mainWebView.setWebViewClient(new MyWebClient());
    mainWebView.setPictureListener(new MyPictureClass());

    mainWebView.loadUrl("file:///android_asset/chapter-001.html");

after use the MyWebclient Class for get the Height and width for mainwebview.

class MyWebClient extends WebViewClient 
    {
        @Override
        public void onPageFinished(WebView view, String url) 
        {
            System.err.println("Page Finish Call");
            lanscapHeight = protraitHeight = findHeight = mainWebView.getHeight();
            System.err.println("Find     Height->"+findHeight);
            System.err.println("Portait  Height->"+protraitHeight);
            System.err.println("Landscap Height->"+lanscapHeight);


        }
    }

after this use the myPictureClass to get the webView contain length.

class MyPictureClass implements PictureListener
    {
        @Override
        public void onNewPicture(WebView view, Picture picture) 
        {   

                proTraitContain = mainWebView.getContentHeight();

                System.err.println("picture Class Call-->"+proTraitContain);
        }
    }

after this.create button next and previous to display the next and previous page.so use the SimpleOnGestureListener to Detect touch event.

 btnNext = (Button) findViewById(R.id.btnNext);
        btnNext.setOnTouchListener(this);
        btnPrev = (Button) findViewById(R.id.btnPrev);
        btnPrev.setOnTouchListener(this);

Override touch Method.

 @Override
        public boolean onTouch(View view, MotionEvent event) 
        {
            if (view == btnNext)
            {
                btnClickFlage = true;
                gestureDetector.onTouchEvent(event);
            } else 
            {
                btnClickFlage = false;
                gestureDetector.onTouchEvent(event);
            }
            return false;
        }

implement the SimpleGestureListener class as Below.

class MyGesture extends SimpleOnGestureListener 
    {
        @Override
        public boolean onSingleTapUp(MotionEvent e)
        {

            super.onSingleTapUp(e);

                    System.err.println("Display Total Contain For Protrait -->"+proTraitContain);
                    System.err.println("Before Height-->" + findHeight);

                    if (btnClickFlage) 
                    {



                        if (findHeight > (proTraitContain+protraitHeight)) 
                        {
                            if(restProtraitFlag)
                            {
                                System.err.println("If part In side Flag-->"+findHeight);
                                findHeight=findHeight+protraitHeight;
                                restProtraitFlag=false;
                                //findHeight=findHeight+protraitHeight;
                                mainWebView.scrollTo(0, findHeight);
                                System.err.println("If part In side Flag-->"+findHeight);
                            }else
                            {
                                mainWebView.loadUrl("file:///android_asset/chapter-002.html");
                                restProtraitFlag=true;
                            }
                        } 
                        else
                        {
                            if(protraitFlag)
                            {
                                if(findHeight==protraitHeight)
                                {
                                    findHeight = protraitHeight;
                                }else
                                {
                                    findHeight = findHeight + protraitHeight;   
                                }
                                protraitFlag=false;
                            }else
                            {
                                findHeight = findHeight + protraitHeight;
                            }
                            mainWebView.scrollTo(0, findHeight);
                        }
                    }
                    else
                    {
                        restProtraitFlag=true;
                        if (findHeight<=0)
                        {
                            mainWebView.loadUrl("file:///android_asset/chapter-001.html");
                            System.err.println("Load Previous page");

                        } 
                        else 
                        {
                            findHeight = findHeight - protraitHeight;
                            mainWebView.scrollTo(0, findHeight);
                        }
                    }
                    System.err.println("After Height-->" + findHeight);
                }
            return true;
        }
    }

but i can't Display the last page of current html page path.now what to do.any solution please give.it's urgent. i get the content width properly and use the scrollTo method to display but i can't do it.after last page rest of some contain can't display.

Please saw me the any way.

Thank in advance..

Hi Friends Finally i got my question answer.i use the ScrollTo method to scroll the contain and Display next contain of current page.but problem is there webView Display all contain according device.so all time contain display is higher then this current value.so i use the Webview.getScale(); method to Display how much scale use by webview.according to this i use this method and get current contain of webview in and use Display page wise.it s finally it s work for me..

Name For Calender Outhentication De

Hello You have to put your HTML in res and then in that you have to keep it in raw after then you can access it like this...

webviewTips.loadUrl("file:///android_res/raw/tips.html");

You are showing the web page stored in your assets folder or from sd card.

So, My advice is that leave this approach and this way to show the web page to user..

And Edit your HTML files and put Anchor Tags in that...using that the user can traverse through the web page easily. ( Example for that )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM