Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onScroll gets called when I set listView.onScrollListener(this), but without any touch

When I set the onScrollListener for my ListView, it calls onScroll. This causes a crash because certain things haven't been initialized.

Is this normal? Note: this is happening without me even touching the 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();     } } 
like image 388
Siavash Avatar asked Apr 18 '13 02:04

Siavash


1 Answers

Just a reminder, according to the javadoc of

MotionEvent.ACTION_SCROLL :

This action is always delivered to the window or view under the pointer, which may not be the window or view currently touched.

This action is not a touch event so it is delivered to onGenericMotionEvent(MotionEvent) rather than onTouchEvent(MotionEvent).

Hence, motionEvent.getAction() will never gets the SCROLL event. Check for MOVE will do the job

You can also do the similar thing in the onScrollStateChanged method of the OnScrollListener

@Override public void onScrollStateChanged(AbsListView view, int scrollState) {         if(scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){             userScrolled = true;         }    } 
like image 66
3 revs, 2 users 97% Avatar answered Oct 01 '22 14:10

3 revs, 2 users 97%