开发者

how to detect Android ListView Scrolling stopped?

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?


Try using the setOnScrollListener and implement the onScrollStateChanged with scrollState == 0 ... do what you need to do...

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...");
        }
    }
});


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 not recorded. 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:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜