2
React Native是否有一種方法來同步兩個或更多滾動視圖,以便它們彼此跟隨?我已經看到了objective-c的一些實現,但不知道如何實現這個對於native native。React本機同步兩個滾動視圖
React Native是否有一種方法來同步兩個或更多滾動視圖,以便它們彼此跟隨?我已經看到了objective-c的一些實現,但不知道如何實現這個對於native native。React本機同步兩個滾動視圖
到目前爲止,最乾淨的,我發現這是this太大的問題:
import React, { Component } from 'react';
import { Text, View, ScrollView } from 'react-native';
function Squares(props){
var squares = [];
for (var i=0; i<props.numRows; i++){
squares.push(<View style={{ height: 50, backgroundColor: props.color1 }}><Text>{i}</Text></View>);
squares.push(<View style={{ height: 50, backgroundColor: props.color2 }}><Text>{i}</Text></View>);
}
return squares;
}
export default class App extends Component {
constructor(props) {
super(props);
this.leftIsScrolling = false;
this.rigthIsScrolling = false;
}
render() {
return (
<View
style={{flex: 1, alignItems: 'flex-start',backgroundColor: 'yellow'}}>
<View style={{ backgroundColor: '#bbb', flexDirection: 'row' }}>
<ScrollView
scrollEventThrottle={16}
ref={scrollView => { this._leftView = scrollView; }}
onScroll={e => {
if (!this.leftIsScrolling) {
this.rigthIsScrolling = true;
var scrollY = e.nativeEvent.contentOffset.y;
this._rightView.scrollTo({ y: scrollY });
}
this.leftIsScrolling = false;
}}>
<Squares numRows={20} color1={"green"} color2={"darkgreen"} />
</ScrollView>
<ScrollView
ref={scrollView => { this._rightView = scrollView; }}
scrollEventThrottle={16}
onScroll={e => {
if (!this.rigthIsScrolling) {
this.leftIsScrolling = true;
var scrollY = e.nativeEvent.contentOffset.y;
this._leftView.scrollTo({ y: scrollY });
}
this.rigthIsScrolling = false;
}}>
<Squares numRows={20} color1={"red"} color2={"darkred"} />
</ScrollView>
</View>
</View>
);
}
}
我自己也嘗試沿着這gist線的東西,但它的行爲很奇怪,由於TH在滾動位置的聆聽者總是影響兩個ScrollView
s。
對於這個,我受這個answer的啓發,關於如何在javascript中使用它。
如果有人仍在尋找答案,這裏有一個可能的實現:https://gist.github.com/jevakallio/e95b8ee3c649eb64b2dc768be9375e11 –