1

View在一个 android 应用程序中进行了自定义,HorizontalScrollView如图所示。

    <HorizontalScrollView
            android:id="@+id/feedBackScroller"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none">

            <com.my.views.OfflineFeedbackView
                android:id="@+id/feedBackView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginTop="@dimen/session_rec_page_title_bar_height"
                android:layout_marginBottom="@dimen/session_rec_page_ctrl_bar_height"/>

</HorizontalScrollView> 

显示OfflineFeedbackView我正在播放的音轨的音高轨道,我通过scrollTo(newXPos, newYPos)基于当前播放时间的调用来滚动此视图。我面临的问题是,如果我通过触摸滚动屏幕然后开始播放,滚动的参考似乎在我的视图上发生了变化,并且滚动发生在我通过触摸滚动到的位置。我浏览了 API 文档,发现scrollTo(newXPos, newYPos)内部调用onScrollChanged(int l, int t, int oldl, int oldt)whereoldloldt分别是旧的水平和垂直原点。因此,我对此的解释是,当我通过触摸屏幕滚动时,这些原始值会发生变化。所以,我也试过打电话onScrollChanged(newX, newY, 0, 0)但这只是冻结屏幕并且没有滚动。难道我做错了什么?还有什么其他更好的方法来处理这个问题?

4

1 回答 1

0

这里的问题就像你所说的,当你触摸时,Horizo​​ntalScrollView 位置会改变。

因此,一种解决方案可能是找到滚动视图的初始左上角位置

HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView);
int x, y;
x = hsv.getLeft();
y = hsv.getTop();

并始终相对于这些存储的 x,y 位置滚动您的子视图,即

childView.scrollTo(x+newXPos,y+newYPos);
于 2016-01-27T19:59:29.093 回答