android - onScroll gets called when I set listView.onScrollListener(this), but without any touch -
when set onscrolllistener
listview
, calls onscroll
. causes crash because things haven't been initialized.
is normal? note: happening without me touching phone.
public class mainactivity1 extends activity implements onclicklistener, onscrolllistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout1); listview lv = (listview)findviewbyid(r.id.listview1); lv.setonscrolllistener(this); ... } ... public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount){ if( firstvisibleitem+visibleitemcount == totalitemcount ){ pullcontactlist(); } }
it's normal because source code setonscrolllistener
in abslistview
(the superclass of listview
) this:
public void setonscrolllistener(onscrolllistener l) { monscrolllistener = l; invokeonitemscrolllistener(); }
and invokeonitemscrolllistener
this:
/** * notify our scroll listener (if there one) of change in scroll state */ void invokeonitemscrolllistener() { if (mfastscroller != null) { mfastscroller.onscroll(this, mfirstposition, getchildcount(), mitemcount); } if (monscrolllistener != null) { monscrolllistener.onscroll(this, mfirstposition, getchildcount(), mitemcount); } onscrollchanged(0, 0, 0, 0); // dummy values, view's implementation not use these. }
depending on you're trying do, there number of ways avoid problem.
edit:
since want if user scrolled, suppose like:
lv.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view view, motionevent motionevent) { if(view == lv && motionevent.getaction() == motionevent.action_scroll) { userscrolled = true; } return false; } });
then..
lv.setonscrolllistener(new abslistview.onscrolllistener() { @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount){ if(userscrolled && firstvisibleitem+visibleitemcount == totalitemcount ){ pullcontactlist(); } } });
Comments
Post a Comment