在Android的web视图无限滚动 [英] infinite scroll in android webview

查看:194
本文介绍了在Android的web视图无限滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些本地HTML文件,我想与无限滚动的方法来显示它们。

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

注意:我不能更改HTML内容,所以请不要建议JavaScript添加到他们。我必须做它在运行时。

NOTE: i cant change the html content, so please don't advice to add javascript to them. i must do it in run time.

所以,我想通了,我可以通过使用loadURL执行的JavaScript 在运行时(JavaScript的:......)

so, i figured out that i can execute javascript in runtime via loadUrl("javascript: ....").

我overrided onOverScrolled() web视图的方法找出当用户达到web视图的结束。 (仔细演戏,所以问题不在这里)

i overrided onOverScrolled() method of webView to find out when user reach the end of webView. (it acting carefully, so the problem is not here)

的问题是有时候新的内容成功连接其他时间并没有歌厅连接。

在日志中我可以看到,页面的方法月底被触发,检索新的HTML身体被调用,执行JavaScript的code被调用,但它并没有影响。

in the log i can see that the end of page method get triggered, retrieving new html body get called, executing javascript code get called, but it did not affect.

这是我的code,可能是出事了,我不能看到它:

here is my code, may be something went wrong and i can not see it:

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)
{
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    if(clampedY & reloadFlag) //for first time realodFlag is false, when the WebViewClient.onPageFinished() get called it turn to ture
    {
        if (!(isVerticalScrollPossible(SCROLL_DOWN)))
        {
            reloadFlag = false; 
            currUri = nextResource(currUri); //findout next page
            appendNextPage();
        }

    }
}

private final int SCROLL_DOWN = 1;
private final int SCROLL_UP = -1;

private boolean isVerticalScrollPossible(int direction)
{
    final int offset = computeVerticalScrollOffset();
    final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
    if (range == 0) return false;
    if (direction < 0) {
        return offset > 0;
    } else {
        return offset < range - 1;
    }
}

public String getNextPageJS(Uri currPage)
{
    String body = getNextPageBody(currPage);
    //Log.d("myTAG", body);
    String jsResult = "javascript:(function() { document.body.innerHTML += '<div id=\"separator\" style=\"height:10px; margin-top:10px; margin-bottom:10px; background-color:#000000;\"></div>" + body + "';})()";
    return jsResult;
}

private void appendNextPage()
{
    reloadFlag = false;
    Thread appendThread = new Thread(null, doAppend, "backgroundAppend");
    appendThread.start();
    Log.i("appendNextPage", "get called");
}

public String rs = "";
private Runnable doAppend = new Runnable()
{
    @Override
    public void run() 
    {
        Log.i("doAppend", "get called + currUri: " + currUri);
        rs = getNextPageJS(currUri);
        //loadUrl(rs);
        appendHandler.sendEmptyMessage(0);
    }       
};

private Handler appendHandler = new Handler()
{
    public void handleMessage(Message msg)
    {
        loadUrl(rs);
        reloadFlag = true;
        Log.i("appendHandler", "get called");       
    }
};

注意:有时我得到这个在模拟器日志(而不是在真实设备):

NOTE: sometimes i get this in the emulator log (not in real device):

I/chromium(1339): [INFO:CONSOLE(1)] "Uncaught SyntaxError: An invalid or illegal string was specified.", source: http://localhost:1025/OEBPS/Text/Section0042.xhtml (1)

页面的数量是不时不同的,可能是它的坏javasccript code,我不知道。

the number of page is different from time to time, may be it's for bad javasccript code, i don't know.

提示:

1)我不是JavaScript的codeR,所以可能的JavaScript code是不好的。

1) i'm not javascript coder, so may be the javascript code is not good

2)或者调用JavaScript的code几次导致此问题

2) or maybe calling javascript code several times cause this problem

3)我知道的JavaScript code必须页面加载后执行完全,所以也许是code称为太早,对于这个问题是onPageFinished()获取调用只是第一页,它不调用时通过javascript code附加新的内容,我尝试使用线程来解决这个问题,我认为它的工作。

3) i know that javascript code must execute after page loading completely, so maybe the code called too soon, the problem for this is that onPageFinished() getting called just for first page and it does not called when new content attached via javascript code, i tried to solve this problem using thread, and i think it worked.

更新:我想通了,当HTML体小,这个code工作正常,但是当我尝试将庞大的身躯没有奏效。为使用loadURL()方法有字符限制吗?或任何其他的想法?

UPDATE: i figured out that this code works fine when the html body is small, but when i try to attach large body it didn't work. is loadUrl() method has char limit? or any other idea?

推荐答案

好吧,我发现了问题,如果有人想知道的。

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

问题是, loadUri()(至少在我的情况)无法加载一次(在JavaScript $ C $词写的)太多的HTML标记

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.

这里是code,我用:

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;
}

编辑:

还有:
首先是 S IN字符串应与替换\\'

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.

这篇关于在Android的web视图无限滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆