看起來你在這裏遇到了一個系統限制 - 系統會在你的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