android - OnScrollListener for RecyclerView total scroll is inaccurate? -
i implemented onscrolllistener recyclerview follows:
new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); scrolly += dy; } };
this should add scroll changes scrolly holds total vertical scroll of recyclerview.
but when scroll down end of list , scroll up, scroll value not equal 0, positive (in test example ends @ around 300).
what part of implementation wrong?
okay, figured out , timing issue. might have same problem me @ point, here happened:
in fragment added onscrolllistener add dy complete scrolly value recyclerview:
new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); mscrolly += dy; } };
in recyclerview rows, wanted use mscrolly value, whenever recyclerview scrolled, respond parallax effect, implemented onscrolllistener in viewholder, so:
new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); applyparallax(mscrolly); //mscrolly calculated fragment's onscrolllistener } };
i figured, since viewholders got destroyed , reused, resource efficient add child onscrolllisteners when needed.
the thing didn't know recyclerview onscrolllisteners handled in sequence of addition (it's kinda obvious after fact). since fragment's onscrolllistener added first , row's onscrolllisteners after, fragments onscrolllistener called last, resulting in mscrolly value being updated after had been handled row's onscrolllisteners, missed last update , stopped @ second last mscrolly value, positive.
i fixed implementing logic fragment's onscrolllistener (note, check first child, since needed parallax effect in first row! if have more effects in other places, must loop through current views in layoutmanager):
new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); mscrolly += dy; linearlayoutmanager manager = ((linearlayoutmanager) mrecyclerview.getlayoutmanager()); view firstchild = manager.getchildat(0); if(firstchild.getid() == r.id.fpd_title_super){ final postdataadapter.maintitleviewholder holder = (postdataadapter.maintitleviewholder)mrecyclerview.getchildviewholder(firstchild); holder.dispatchscrolly(mscrolly); } } };
and implementing method viewholders:
public void dispatchscrolly(int scrolly){ // handle parallax effect here }
now works.
Comments
Post a Comment