zoukankan      html  css  js  c++  java
  • OnScrollListenerPro

    OnScrollListenerPro

    ————OnScrollListener 加强版

    @[Android|ListView|OnScrollListener]


    在写listview的时候很时候我们都会遇到一些场景

    1. 判断当前是向上滚动,精确到px
    2. 判断是否滚动到最后一个

    但是Android提供给我们的OnScrollView不能给我们提供这些功能
    于是我写了一个OnScrollListenerPro来完成这样的事情

    package org.hangox.test;
    
    import android.view.View;
    import android.widget.AbsListView;
    
    /**
     * Created With Android Studio
     * User 47
     * Date 2014-11-03
     * Time 17:42
     * 一个加强板的OnScrollListener
     * 可以提供向上滑动向下活动检测
     * 是否是最后一个检测
     */
    public class OnScrollListenerPro implements AbsListView.OnScrollListener {
        private AbsListView mAbsListView;
        private int mLimit = 40;
        private int mLastScroll;
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            mAbsListView = view;
        }
    
        /**
         * 设定最少滚动多少才算是滚动
         * @param mLimit
         */
        public void setLimit(int mLimit){
            this.mLimit = mLimit;
        }
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            mAbsListView = view;
            int currentScrollY = getScrollY();
            int dValue = currentScrollY - mLastScroll;
            if(dValue < -mLimit){//向上滚
                onScrollUp(mLastScroll,currentScrollY);
                mLastScroll = currentScrollY;
            }else if(dValue > mLimit ){//向下滚
                onScrollDown(mLastScroll,currentScrollY);
                mLastScroll = currentScrollY;
            }
            if(((totalItemCount > 0) && (firstVisibleItem + visibleItemCount >= totalItemCount - 1)))
                onLastItemShow();
        }
    
        /**
         * 向上滚动
         * @param lastScroll
         * @param currentScroll
         */
        public void onScrollUp(int lastScroll,int currentScroll){
    
        }
    
    
        /**
         * 向下滚动
         * @param lastScroll 上一次滚动值
         * @param currentScroll 这一次
         */
        public void onScrollDown(int lastScroll,int currentScroll){
    
        }
    
        public void onLastItemShow(){
    
        }
    
    
    
        public int getScrollY() {//获取滚动距离
            View c = mAbsListView.getChildAt(0);
            if (c == null) {
                return 0;
            }
    
            int firstVisiblePosition = mAbsListView.getFirstVisiblePosition();
            int top = c.getTop();
    
            int headerHeight = 0;
            if (firstVisiblePosition >= 1) {
                headerHeight = mAbsListView.getHeight();
            }
            return -top + firstVisiblePosition * c.getHeight() + headerHeight;
        }
    }
    
    

    使用很简单,重写其中的函数就可以了

  • 相关阅读:
    VA插件突然不能使用,彈出“the security key for....”
    【WIN10】Segoe MDL2 Assets
    【WIN10】WIN2D——圖層
    【WIN10】WIN2D——圖像處理
    【WIN10】WIN2D——繪製文字
    【WIN10】WIN2D——基本圖形的繪製
    為你的文件夾添加“使用CMD命令打開”菜單
    【WIN10】移植opencc到WIN10-UWP,實現自己的繁簡轉換工具
    【WIN10】判斷程序運行在哪個平台
    【WIN10】使用VS生成appx安裝包,並安裝測試
  • 原文地址:https://www.cnblogs.com/Jabba93/p/4072482.html
Copyright © 2011-2022 走看看