惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
G
Google Developers Blog
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
博客园 - 聂微东
V
Visual Studio Blog
博客园_首页
D
DataBreaches.Net
腾讯CDC
I
InfoQ
F
Fortinet All Blogs
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
月光博客
月光博客
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog

博客园 - hangox

Claude Code 自定义状态栏配置指南 如何给 Claude 中的网页做截图 AGP 8.0+ Publish idea 插件兼容 K2 模式 IDEA 不提示 gitlab-ci 字段问题 不要随便使用 npm 源加速站点 Xcode Format Swift 代码 idea Convert File To Kotlin File 多 Commit 问题 ApkList 源问题修复 BCM94360CS2 黑苹果 macOS Sonoma Wifi 不能开启修复 NginxProxyManager 数据库迁移记录 android ConcurrentModificationException 错误 Android Studio 插件分享——Gradle Utilities Android Studio 插件分享——Jadx Class Decompiler templete Android Studio 3.0 新特性 Android SpringAnimator初探 关于Android菜单上的记录 升级到 classpath 'com.android.tools.build:gradle:1.0.0-rc1
OnScrollListenerPro
hangox · 2014-11-03 · via 博客园 - hangox

@[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;
    }
}

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