如何检测 Android ListView 滚动停止?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1768391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to detect Android ListView Scrolling stopped?
提问by koji lin
I'm trying to do something after scrolling stopped.So, I tried using OnScrollListener#onScrollStateChanged(SCROLL_STATE_IDLE)
to detect when the scrolling stopped(either TOUCH_SCROLL or FLING
)(at 1.5 it's runs as i expect).
But when it runs on 2.0, onScrollStateChanged can't received the event after releasing the finger.Is there any callback or anyway to detect that event?
我正在尝试OnScrollListener#onScrollStateChanged(SCROLL_STATE_IDLE)
在滚动停止后做一些事情。所以,我尝试使用来检测滚动停止的时间(或者TOUCH_SCROLL or FLING
)(在 1.5 时它按我的预期运行)。但是当它在2.0上运行时,松开手指后onScrollStateChanged无法接收事件。是否有任何回调或无论如何检测该事件?
回答by Vidar Vestnes
Try using the setOnScrollListener and implement the onScrollStateChanged with scrollState == 0 ... do what you need to do...
尝试使用 setOnScrollListener 并使用 scrollState == 0 实现 onScrollStateChanged ......做你需要做的......
setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
Log.i("a", "scrolling stopped...");
}
}
});
回答by Phil Kulak
The trick is to keep track of when the user is flinging and when they are not. It seems like this is about the only distinction you can make because, like you said, the transitions from scrolling with a finger to idle are notrecorded. Here's what I'm talking about:
诀窍是跟踪用户何时投掷以及何时不投掷。似乎这是您可以做出的唯一区别,因为就像您说的那样,不会记录从手指滚动到空闲的转换。这就是我要说的:
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState != OnScrollListener.SCROLL_STATE_FLING) {
flinging = false;
int count = view.getChildCount();
for (int i = 0; i < count; i++) {
View convertView = view.getChildAt(i);
Recipe recipe = (Recipe) convertView.getTag();
ImageView image = (ImageView) convertView.findViewById(R.id.icon);
if (recipe != null && recipe.getImageURL() != null) {
ImageLoader.loadImage(this, image, recipe.getImageURL());
}
}
} else {
flinging = true;
}
}
And then in the listView adapter:
然后在 listView 适配器中:
public View getView(int position, View convertView, ViewGroup parent) {
// Bunch of code....
if (!flinging) {
ImageLoader.loadImage(BrowseRecipes.this, image, recipe.getImageURL());
}
}
So, instead of reacting to the change in the listener, just load the images (or whatever you need to do that's intensive) in the first place as long as there's no flinging going on.
因此,不要对侦听器的变化做出反应,只要首先加载图像(或任何你需要做的密集的事情),只要没有甩动就行。
This is all taken out of my project here: https://github.com/pkulak/mealfire_android
这都是从我的项目中取出的:https: //github.com/pkulak/mealfire_android