如何检测如果一个ListView是快速滚动 [英] How to detect if a ListView is fast scrolling

查看:129
本文介绍了如何检测如果一个ListView是快速滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来检测,如果一个ListView是快速滚动?也就是说,我们可以在某种程度上知道当用户presses并释放FastScroller?

Is there a way to detect if a ListView is fast scrolling? That is to say, can we somehow know when the user presses and releases the FastScroller?

推荐答案

思考可以让你做到这一点。通过采取一看<一href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/AbsListView.java"相对=nofollow> AbsListView源$ C ​​$ C ,有一个 FastScroller 对象,表示围绕快速滚动功能的包装。 <一href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/FastScroller.java"相对=nofollow>它的源$ C ​​$ C 显示了一个有趣的<一个href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/FastScroller.java#178"相对=nofollow>字段:

Reflection allows you to do it. By taking a look at the AbsListView source code, there is a FastScroller object which indicates a wrapper around the fast scrolling functionality. Its source code shows an interesting field:

/**
 * Current decoration state, one of:
 * <ul>
 * <li>{@link #STATE_NONE}, nothing visible
 * <li>{@link #STATE_VISIBLE}, showing track and thumb
 * <li>{@link #STATE_DRAGGING}, visible and showing preview
 * </ul>
 */
private int mState;

此字段包含 FastScroller 对象的状态。该解决方案包括在通过反射阅读该字段的值,每次 onScroll() onScrollStateChanged()方法被触发

This field contains the status of the FastScroller object. The solution consists in reading this field's value via reflection, every time the onScroll() and onScrollStateChanged() methods are triggered.

这code实现上述解决办法:

This code implements the solution described above:

private class CustomScrollListener implements OnScrollListener {

    private ListView list;
    private int mState = -1;
    private Field stateField = null;
    private Object mFastScroller;
    private int STATE_DRAGGING;

    public CustomScrollListener() {
        super();

        String fastScrollFieldName = "mFastScroller";
        // this has changed on Lollipop
        if (Build.VERSION.SDK_INT >= 21) {
            fastScrollFieldName = "mFastScroll";
        }

        try {
            Field fastScrollerField = AbsListView.class.getDeclaredField(fastScrollFieldName);
            fastScrollerField.setAccessible(true);
            mFastScroller = fastScrollerField.get(list);

            Field stateDraggingField = mFastScroller.getClass().getDeclaredField("STATE_DRAGGING");
            stateDraggingField.setAccessible(true);
            STATE_DRAGGING = stateDraggingField.getInt(mFastScroller);

            stateField = mFastScroller.getClass().getDeclaredField("mState");
            stateField.setAccessible(true);
            mState = stateField.getInt(mFastScroller);

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

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

        // update fast scroll state
        try {
            if (stateField != null) {
                mState = stateField.getInt(mFastScroller);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


        if (mState == STATE_DRAGGING)) {
            // the user is fast scrolling through the list
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        // update fast scroll state
        try {
            if (stateField != null) {
                mState = stateField.getInt(mFastScroller);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
}

这篇关于如何检测如果一个ListView是快速滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆