how to detect Android ListView Scrolling stopped?

后端 未结 2 1255
鱼传尺愫
鱼传尺愫 2020-12-01 03:17

I\'m trying to do something after scrolling stopped.So, I tried using OnScrollListener#onScrollStateChanged(SCROLL_STATE_IDLE) to detect when the scrolling stop

相关标签:
2条回答
  • 2020-12-01 03:21

    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...");
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-01 03:47

    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 讨论(0)
提交回复
热议问题