android - Why do I load more than once when implementing load more data on the bottom of recyclerview? -
i implement following code. works, retrieve more tha once, makes many duplication in data. how make run retrieve more data once?
final linearlayoutmanager layoutmanager = (linearlayoutmanager) getlayoutmanager(); recyclerview.setonscrolllistener(new recyclerview.onscrolllistener() { boolean loading = true; @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { int pastvisibleitems, visibleitemcount, totalitemcount; visibleitemcount = layoutmanager.getchildcount(); totalitemcount = layoutmanager.getitemcount(); pastvisibleitems = layoutmanager.findfirstvisibleitemposition(); if (loading) { if ((visibleitemcount + pastvisibleitems) >= totalitemcount) { loading = false; //retrieve more data internet loading = true; } } } });
it turns out below algorithm better
private int previoustotal = 0; private boolean loading = true; private int visiblethreshold = 5; int firstvisibleitem, visibleitemcount, totalitemcount; mrecyclerview.setonscrolllistener(new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); visibleitemcount = mrecyclerview.getchildcount(); totalitemcount = mlayoutmanager.getitemcount(); firstvisibleitem = mlayoutmanager.findfirstvisibleitemposition(); if (loading) { if (totalitemcount > previoustotal) { loading = false; previoustotal = totalitemcount; } } if (!loading && (totalitemcount - visibleitemcount) <= (firstvisibleitem + visiblethreshold)) { // end has been reached log.i("...", "end called"); // loading = true; } } });
Comments
Post a Comment