在Android中按像素滚动ListView

Ran*_*ku' 7 android listview scroll

我想ListView按像素数滚动Android中的a .例如,我想将列表向下滚动10个像素(以便列表中的第一个项目隐藏其前10个像素行).

我认为ListView上明显可见scrollByscrollTo方法可以完成这项工作,但他们没有,相反,他们错误地滚动整个列表(实际上,getScrollY即使我用手指滚动列表,总是返回零.)

我正在做的是我正在捕捉轨迹球事件,我想根据轨迹球的运动平滑地滚动列表视图.

Sam*_*Sam 11

滚动ListView窗口小部件的支持方式是:

mListView.smoothScrollToPosition(position);

http://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollToPosition(int)

但是,由于您特别提到要垂直偏移视图,因此必须调用:

mListView.setSelectionFromTop(position, yOffset);

http://developer.android.com/reference/android/widget/ListView.html#setSelectionFromTop(int,%20int)

请注意,您也可以使用smoothScrollByOffset(yOffset).但是,它仅在API> = 11时受支持

http://developer.android.com/reference/android/widget/ListView.html#smoothScrollByOffset(int)


enl*_*now 10

如果查看api 19中添加的scrollListBy()方法的源代码,您将看到可以使用包作用域trackMotionScroll方法.

public class FutureListView {

    private final ListView mView;

    public FutureListView(ListView view) {
        mView = view;
    }

    /**
     * Scrolls the list items within the view by a specified number of pixels.
     *
     * @param y the amount of pixels to scroll by vertically
     */
    public void scrollListBy(int y) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mView.scrollListBy(y);
        } else {
            // scrollListBy just calls trackMotionScroll
            trackMotionScroll(-y, -y);
        }
    }

    private void trackMotionScroll(int deltaY, int incrementalDeltaY) {
        try {
            Method method = AbsListView.class.getDeclaredMethod("trackMotionScroll", int.class, int.class);
            method.setAccessible(true);
            method.invoke(mView, deltaY, incrementalDeltaY);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        };
    }
}
Run Code Online (Sandbox Code Playgroud)