我有一个与两个不同视图的同步滚动相关的棘手问题。 我制作了自己的自定义网格视图小部件,它具有左侧和顶部的“粘性”视图,只在一个方向上与网格一起移动。想像一个日历,你在上面有时间,在左边有日期,当你水平滚动时间时,日期视图应该保持不变,当你垂直滚动日期时,时间视图应该保持不变。
网格本身是使用嵌套的水平ScrollView实现的,位于垂直ScrollView中。网格工作得很好,所以没有问题。由于粘性视图不在实际网格中,因此我已经覆盖了网格ScrollView中的onScrollChanged,并在用户滚动网格时以编程方式调用了sticky视图的scrollTo。
这按预期工作,除了两个不同视图开始滚动和结束滚动的时间存在轻微的偏移。我认为考虑到滚动可能是在线程上线性执行的,这是有道理的。
所有视图都是scroll views,我启用了平滑滚动并使用smoothScrollTo等方法来尝试改进,但无论如何问题都是一样的。该问题特别明显在较大的屏幕上,例如三星Galaxy Tab上,而在中小型屏幕设备上几乎不可见。
任何帮助都将不胜感激!如果有简单的解决方法,那太好了......如果意味着需要新的设计(满足上面的粘性视图用例),那就这样吧。
触发程序滚动的代码,水平方向相同
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
mListener.onScrollY(y);
super.onScrollChanged(x, y, oldx, oldy);
}
// which leads to,
// Handle vertical scroll
public void onScrollY(final int y) {
mCurrentY = y;
mVerticalScroll.smoothScrollTo(0, y);
}
以下是XML布局,如果有帮助的话
实际的网格是一个水平滚动视图包裹在垂直滚动视图中,网格项在嵌套的线性布局中垂直添加
>
< com.....VerticalScrollView
android:id="@+id/gridscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/timescroll"
android:layout_toRightOf="@id/vertscroll"
android:layout_alignTop="@id/vertscroll"
android:layout_marginLeft="2dp" android:scrollbars="none"
android:fadingEdge="none">
< com....HorizScrollView
android:id="@+id/horizscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:fadingEdge="none">
< LinearLayout android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
< /LinearLayout>
< /com.....HorizScrollView>
< /com.....VerticalScrollView>
The horizontal sticky view
< com.....GridTimeScrollView
android:id="@+id/timescroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:fadingEdge="none">
< LinearLayout android:id="@+id/timelist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" />
< /com.....GridTimeScrollView>
垂直粘性视图
< com....GridVertListScrollView
android:id="@+id/vertscroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:fadingEdge="none">
< LinearLayout
android:id="@+id/vertitemlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" />
< /com.....GridVertListScrollView>