onScrollChanged() never fired on Android 4.0

眉间皱痕 提交于 2019-12-22 06:49:31

问题


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:


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...



来源:https://stackoverflow.com/questions/20833612/onscrollchanged-never-fired-on-android-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!