infinite scroll in android webview

后端 未结 1 1725
半阙折子戏
半阙折子戏 2021-01-15 11:25

i have some local html file and i want to show them with infinite scroll method.

NOTE: i cant change the html content, so please don\'t advice to ad

相关标签:
1条回答
  • 2021-01-15 12:07

    OK, i found the problem, if anyone wants to know.

    the problem is that the loadUri() (at least in my case) can not load too many html tag at once (in javascript code i written)

    so, the solution is easy, load tags one by one.

    here is the code i used:

    public ArrayList<String> getNextPageBody(Uri currAddress)
    {
        String html = getHtml(currAddress); // this is the all html tags in the next file
    
        //get body elements as arrayList, using jsoup
        Document doc = Jsoup.parse(html);
        Elements elements = doc.select("body").first().children();
        ArrayList<String> chuncks = new ArrayList<String>();
        for (org.jsoup.nodes.Element el : elements)
        {
            chuncks.add(el.toString());
        }
    
        return chuncks;
    }
    
    public void loadBodyChunk(ArrayList<String> bodyChunks)
    {    
        //show a separator for each page
        bodyChunks.add(0, "javascript:(function() { document.body.innerHTML += '<div id=\"separator\" style=\"height:10px; margin-top:10px; margin-bottom:10px; background-color:#000000;\"></div>';}())");
        loadUrl(bodyChunks.get(0));
    
        for(int i = 1; i < bodyChunks.size(); i++)
        {   
            String jsResult = "javascript:(function() { document.body.innerHTML += '" +  bodyChunks.get(i) + "';}())";
            loadUrl(jsResult);
        }
    
        reloadFlag = true;
    }
    

    EDIT:

    also: first the 's in String should be replaced with \' :

    body = body.replace("'", "\\'");
    

    then all newline char should be eliminated:

    body = body.replaceAll(System.getProperty("line.separator"), " ");
    

    all problem solved.

    0 讨论(0)
提交回复
热议问题