问题描述
当我设置了 onScrollListener
我的的ListView
,它会调用 onScroll
。这会导致系统崩溃,因为某些事情没有被初始化。
这是正常的吗?注:这是发生无我,甚至触摸手机
公共类MainActivity1扩展活动实现OnClickListener,OnScrollListener {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.layout1);
ListView的LV =(ListView控件)findViewById(R.id.listview1);
lv.setOnScrollListener(本);
...
}
...
公共无效onScroll(AbsListView观点,诠释firstVisibleItem,
INT visibleItemCount,诠释totalItemCount){
如果(firstVisibleItem + visibleItemCount == totalItemCount){
pullContactList();
}
}
这是正常的,因为在源$ C $下 setOnScrollListener
AbsListView
(的ListView的超
)做到这一点:
公共无效setOnScrollListener(OnScrollListener L){
mOnScrollListener =升;
invokeOnItemScrollListener();
}
和 invokeOnItemScrollListener
做到这一点:
/ **
*通知我们的滚动监听器(如果有)的滚动状态的变化
* /
无效invokeOnItemScrollListener(){
如果(mFastScroller!= NULL){
mFastScroller.onScroll(这一点,mFirstPosition,getChildCount(),mItemCount);
}
如果(mOnScrollListener!= NULL){
mOnScrollListener.onScroll(这一点,mFirstPosition,getChildCount(),mItemCount);
}
onScrollChanged(0,0,0,0); //虚值,View的实现不使用这些。
}
这取决于你正在试图做的是什么,有多种方法来避免这个问题。
编辑:
既然你只是想这样做,如果用户实际滚动,我想你可以这样做:
lv.setOnTouchListener(新OnTouchListener(){
@覆盖
公共布尔onTouch(查看视图,MotionEvent motionEvent){
如果(查看==低压和放大器;&安培; motionEvent.getAction()== MotionEvent.ACTION_SCROLL){
userScrolled = TRUE;
}
}
});
然后..
公共无效onScroll(AbsListView观点,诠释firstVisibleItem,
INT visibleItemCount,诠释totalItemCount){
如果(userScrolled&安培;&功放; firstVisibleItem + visibleItemCount == totalItemCount){
pullContactList();
}
}
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();
}
}
It's normal because the source code for setOnScrollListener
in AbsListView
(the superclass of ListView
) does this:
public void setOnScrollListener(OnScrollListener l) {
mOnScrollListener = l;
invokeOnItemScrollListener();
}
and invokeOnItemScrollListener
does this:
/**
* Notify our scroll listener (if there is one) of a 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 does not use these.
}
depending on what it is you're trying to do, there are a number of ways to avoid this problem.
EDIT:
Since you only want to do this if the user actually scrolled, I suppose you could do something like:
lv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(view == lv && motionEvent.getAction() == MotionEvent.ACTION_SCROLL) {
userScrolled = true;
}
}
});
Then..
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount){
if(userScrolled && firstVisibleItem+visibleItemCount == totalItemCount ){
pullContactList();
}
}
这篇关于onScroll被调用时,我设置listView.onScrollListener(这一点),但没有任何触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!