ScrollView 在高速滚动时忽略 setScrollX()

ScrollView is ignoring setScrollX()at high scrolling speeds

所以,我打算创建一个自定义视图,以便获得像 tinder 这样的相册,一个在照片位置自动调整的滚动视图。我认为通过几个调整的 horizo​​ntalscrollview 就可以完成这项工作。在 scrollView sw 中添加了一个 5*LayoutWidth 的水平 LinerLayout 并定义了它的 onTouchListener 如下:

sw.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            Log.d("MotionEvent", String.valueOf( event.getAction()));
            if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
                int scrollX=sw.getScrollX();
                Log.d("scrollX", String.valueOf( scrollX));
                //Log.d("layoutWidth", String.valueOf( layoutWidth));
                int aux = Math.round((float)scrollX/layoutWidth)*layoutWidth;
                Log.d("correction", String.valueOf( aux));
                sw.setScrollX(aux );



            }



            return false;
        }


    });

它按预期工作,在释放触摸输入时专注于 5 张照片中的一张,但是当我通过快速手势滚动和释放时,滚动视图只是忽略 sw.setScrollX(aux) 并继续滚动.我能做些什么来避免发布后自动滚动吗?我能以某种方式操纵输入事件的数据吗?

谢谢!

终于找到解决办法了。我这样扩展了 Horizo​​ntalcrollView:

public class stepScrollView extends HorizontalScrollView {
public stepScrollView(Context context) {
    super(context);
}

public stepScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public stepScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_UP:

            Display d = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
            Point size = new Point();
            d.getSize(size);
            int layoutWidth = size.x;
            int aux = Math.round((float)this.getScrollX()/layoutWidth)*layoutWidth;
            this.setScrollX(aux );


            return false; 
        default:
            return super.onTouchEvent(ev);
    }
}



}

它适用于您想要放入此自定义视图内的 Horizo​​ntal LinearLayout 中的所有图像。您还必须将图像调整为 stepScrollView 水平尺寸。在我的例子中 layoutWidth.