listview精确监听向上向下的滚动

listview的上下滚动监听,很多人的做法是利用OnScrollListener的onScroll方法
有一个 firstVisibleItem 参数(第二个参数),向下滑动会越来越大,向上滑动就会越来越小
可以在滑动的时候存储一下这个值,然后再与当前值进行判断
这种做法在某些情况下适用,可是如果你的item高度是超过了屏幕高度的,这个时候,这个判断就失去作用了

直接贴代码:

class LvScrollEvent implements AbsListView.OnScrollListener {

        @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            mCurrentfirstVisibleItem = firstVisibleItem;
            View firstView = view.getChildAt(0);
            if (null != firstView) {
                ItemRecod itemRecord = recordSp.get(firstVisibleItem);
                if (null == itemRecord) {
                    itemRecord = new ItemRecod();
                }
                itemRecord.height = firstView.getHeight();
                itemRecord.top = firstView.getTop();
                recordSp.append(firstVisibleItem, itemRecord);
            }
            Logs.d(getScrollY() + "----");

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

    }

    private int getScrollY() {
        int height = 0;
        for (int i = 0; i < mCurrentfirstVisibleItem; i++) {
            ItemRecod itemRecod = recordSp.get(i);
            height += itemRecod.height;
        }
        ItemRecod itemRecod = recordSp.get(mCurrentfirstVisibleItem);
        if (null == itemRecod) {
            itemRecod = new ItemRecod();
        }
        return height - itemRecod.top;
    }


    class ItemRecod {
        int height = 0;
        int top = 0;
    }


你可能感兴趣的:(android,ListView,onScroll,scrolllistener)