getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem==0){
Log.e("log", "滑到顶部");
}
if(visibleItemCount+firstVisibleItem==totalItemCount){
Log.e("log", "滑到底部");
}
}
});
listview重写setOnScrollListener方法。
listview.setOnScrollListener(new OnScrollListener()
{
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
switch (scrollState)
{
// 当不滚动时
case OnScrollListener.SCROLL_STATE_IDLE:// 是当屏幕停止滚动时
scrollFlag = false;
// 判断滚动到底部
if (listview.getLastVisiblePosition() == (listview.getCount() - 1))
{
toTopBtn.setVisibility(View.VISIBLE);
}
// 判断滚动到顶部
if (listview.getFirstVisiblePosition() == 0)
{
toTopBtn.setVisibility(View.GONE);
}
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:// 滚动时
scrollFlag = true;
break;
case OnScrollListener.SCROLL_STATE_FLING:// 是当用户由于之前划动屏幕并抬起手指,屏幕产生惯性滑动时
scrollFlag = false;
break;
default:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
});