【问题标题】:android HorizontalScrollView fling on samsung device三星设备上的android Horizo​​ntalScrollView
【发布时间】:2012-12-19 19:14:36
【问题描述】:

我有一个带有 Horizo​​ntalScrollView 和 Horizo​​ntal-LinearLayout 的简单布局,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:scrollbars="horizontal"
        android:fadingEdge="none">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sed velit sed nisl pharetra consequat"/>
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sed velit sed nisl pharetra consequat"/>
            <TextView ... (same text view repeated several times) />

        </LinearLayout>

    </HorizontalScrollView>

</RelativeLayout>

当我在模拟器上测试时,水平投掷效果很好。但是在三星 Galaxy S2 上进行测试,fling 的行为很奇怪:

当手指向一侧向上移动时,滚动视图开始正常滚动,但在停止之前,它会弹回并向后移动,尽管它有 NOT到达终点。就像滚动视图在任何滚动级别弹跳一样。

如果我只是滚动(将手指移动到侧边停止并向上),滚动就可以了。

有人经历过吗?三星实施中是否有任何错误?

关于如何解决这个问题的任何想法?

我的应用针对的是 android 2.2。 Galaxy S2 有三星官方安卓 4.0.3。

提前致谢!

【问题讨论】:

    标签: android horizontalscrollview


    【解决方案1】:

    经过大量测试,我得出结论,这是三星固件 (Galaxy S2 4.0.3) 上的 Horizo​​ntalScrollView 上的错误。

    似乎正在朝着相反的方向做出挥动手势: 例如,如果我按下并向左移动,然后松开屏幕,则弹射效果(继续移动并减速)完成到... RIGHT!

    所以我的解决方案是继承Horizo​​ntalScrollView,并添加一个补丁以确保如果向右滚动,则向右滚动,反之亦然。

    public class PatchedHorizontalScrollView extends HorizontalScrollView {
        public PatchedHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
        public PatchedHorizontalScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public PatchedHorizontalScrollView(Context context) {
            super(context);
        }
    
        // store most recent scroll X positions
        int olderScrollX = 0;
        int lastScrollX = 0;
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            // keep track of most recent scroll positions
            olderScrollX = lastScrollX;
            lastScrollX = getScrollX();
    
            return super.onTouchEvent(ev);
        }
    
        @Override
        public void fling(int velocityX) {
            // ensure velocityX has the right sign
            if (lastScrollX > olderScrollX) {
                // to the right: velocity must be positive
                if (velocityX < 0) {
                    velocityX = -velocityX;
                }
            } else if (lastScrollX < olderScrollX) {
                // to the left: velocity must be negative
                if (velocityX > 0) {
                    velocityX = -velocityX;
                }
            }
    
            super.fling(velocityX);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 2014-02-13
      • 2017-04-09
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多