onScrollChanged() never fired on Android 4.0

2019-04-30 03:06发布

I have a GridView that shows images from your gallery. When user scrolls the list, details about the image animate in from the left. I implemented this in the class that defines custom layout for the GridView item. It extends LinearLayout.

OnScrollChangedListener mScrollListener = new OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
        if (!getGlobalVisibleRect(r)) {
            resetAnimation();
        } else {
            if (checkBounds()) {
                    showInfo();
            }
            } else {
                    hideInfo();
            }

Method resetAnimation() resets the animation if it's view is not visible on screen. Method checkBounds() compares the Rect got with getGlobalVisibleRect(r) and the Rect representing the screen to check if the details View should be shown.

I add the listener in onFinishInflate() like this:

        getViewTreeObserver().addOnScrollChangedListener(mScrollListener);

Now the actual issue:
Everything works fine on API 19, API 18, API 17 and API 13, tested both on real devices and emulators. On API 14 (Android 4.0.1 and 4.0.2) the onScrollChanged() never gets fired, both on physical device and emulator.
Is it a bug or am I missing something?

1条回答
男人必须洒脱
2楼-- · 2019-04-30 04:07

Instead of OnScrollChangedListener, could you try with OnScrollListener...

    gridView.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub

        }
    });

I don't know why it is not firing in 4.0, but you could try with this...

查看更多
登录 后发表回答