安卓:onScrollStateChanged SCROLL_STATE_IDLE有时不火onScrollStateChanged、SCROLL_STATE_IDLE

2023-09-05 00:46:21 作者:本宫累了

我遇到了一点小问题。我在做什么:我有已经得到的一些图像是一个ListView。为了使滚动顺畅,我禁用了图像滚动时展现出来。现在,似乎是在Android的一个错误这有时会导致滚动状态不回从SCROLL_STATE_FLING回SCROLL_STATE_IDLE,这将导致我的图像不会出现再次发生变化。

I'm running into a bit of a problem. What I'm doing: I've got a ListView which has got some images in it. To make the scrolling smoother I've disabled the images to show up when scrolling. Now there seems to be a bug in Android which sometimes causes the scroll state to not change back from SCROLL_STATE_FLING back to SCROLL_STATE_IDLE, which causes my images to not show up again.

我的第一个想法是设置一个onTouchListener和检查,当我得到ACTION_UP,但这并没有帮助,因为SCROLL_STATE_FLING状态明显在那之后被设置。所以,现在,我想我可以启动一个定时器,当SCROLL_STATE_FLING状态被设定并经过一段时间检查,如果国家仍处于一扔模式,然后验证我的观点。但我不认为这是一个很好的解决方案。

My first thought was to set an onTouchListener and check when I get ACTION_UP, but that doesn't help because the SCROLL_STATE_FLING state is obviously being set after that. So now I've thought I could start a timer when the SCROLL_STATE_FLING state is being set and check after some time if the state is still in fling mode and then invalidate my view. But I don't think that's a very good solution.

有没有人对我怎么可以做一个更好的主意吗?我已经看到了this答复,但我需要API级别℃的解决方案; 9(再加​​上它有时也当它不overscrolling发生)

Does anyone have a better idea on how I could do that? I've seen this reply but I need a solution for API level < 9 (plus it also sometimes happen when it's not overscrolling)

下面是我的code为:

Here's my code for that:

    mList.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            mListAdapter.setIsScrolling(scrollState != SCROLL_STATE_IDLE);
            Log.i(this, "scrollStateChanged" + scrollState);
            if (scrollState == SCROLL_STATE_IDLE) {
                mList.invalidateViews();
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }
    });

谢谢, 玛丽亚

Thanks, Maria

推荐答案

我有同样的问题,所以我的解决办法是只检测,如果滚动视图位置已经到达最后一页,并在这种情况下,不管总是加载图像滚动状态(因为问题似乎当用户甩到ListView的结束总是发生)。因此,修改你的code,你将有:

I had the same problem, so my solution was to just detect if the scrollview position has reached the last page and in that case always load the images regardless of the scroll state (since the problem seems to always occur when the user flings to the end of the listview). So modifying your code you would have:

mList.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        mListAdapter.setIsScrolling(scrollState != SCROLL_STATE_IDLE);
        Log.i(this, "scrollStateChanged" + scrollState);

        int first = view.getFirstVisiblePosition();
        int count = view.getChildCount();

        if (scrollState == SCROLL_STATE_IDLE || (first + count > mListAdapter.getCount()) ) {
            mList.invalidateViews();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    }
});
 
精彩推荐