看起来你在这里遇到了一个系统限制 - 系统会在你的XML中声明它们。
因此,当你扔第一个宣布ScrollView
,第二个得到更新,但第一个不会再更新。然而,当你扔第二个,第一个被更新,然后第二个被更新,这个更改被反映到第一个并且它被再次更新。
我不确定上面的描述是否100%准确,但它是沿着这些线。
我创建了一个测试案例来检查我的假设代替main.xml
如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_layout">
<View android:id="@+id/separator_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"/>
<com.example.www.syncscrollviewtesting_stackoverflow.ObservableScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@id/separator_view"
android:background="@android:color/holo_green_light"
android:id="@+id/left_scrollview">
<com.example.www.syncscrollviewtesting_stackoverflow.Container
android:layout_width="match_parent"
android:layout_height="100000dp"
android:minHeight="100000dp"
android:id="@+id/left_container"/>
</com.example.www.syncscrollviewtesting_stackoverflow.ObservableScrollView>
<com.example.www.syncscrollviewtesting_stackoverflow.ObservableScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_toStartOf="@id/separator_view"
android:background="@android:color/holo_red_dark"
android:id="@+id/right_scrollview">
<com.example.www.syncscrollviewtesting_stackoverflow.Container
android:layout_width="match_parent"
android:layout_height="100000dp"
android:minHeight="100000dp"
android:id="@+id/right_container"/>
</com.example.www.syncscrollviewtesting_stackoverflow.ObservableScrollView>
</RelativeLayout>
上述XML允许您同时ScrollView
S于该separator_view
的两侧定位。我发现无论你如何定位它们,一个具有红色背景的ScrollView
(声明为第二个)总是会导致“滞后”,而具有绿色背景的ScrollView
的投掷效果很好。
我也试图加入到了他们的代码,以防止ScrollView
S的不必要的更新:
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (!mIsDisabled && scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
public void setDisabled(boolean isDisabled) {
mIsDisabled = isDisabled;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mIsDisabled)
return false; // Ignore touch event when disabled
else
return super.onTouchEvent(ev);
}
...这对Activity
的:
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
if (scrollView == mRightScrollView) {
mLeftScrollView.setDisabled(true);
mLeftScrollView.setScrollY(y);
mLeftScrollView.setDisabled(false);
} else {
mRightScrollView.setDisabled(true);
mRightScrollView.setScrollY(y);
mRightScrollView.setDisabled(false);
}
}
,但它没“ t帮助...
所以,我想,你最好找到另一种方法,不涉及重绘一大堆Views
,或只是接受“落后”。
解决方案: 该解决方案是由OP自己,根据我的情况分析提供:触摸事件可以从右边ScrollView
(在XML声明秒)到左侧ScrollView
转发。这样,考虑到左边ScrollView
的fl do不会导致滞后,所有的触摸事件被视为由第一个声明的ScrollView
开始,并且避免了滞后。
为什么你使用两个ScrollViews而不是一个? –
我同意@Médéric为什么不使用1? – Budius
因为在某些时候,我需要一个重叠另一个。 – MartinMoizard