子ビューを所有するコンポーネントは、子ビューが強調表示されているか淡色表示されているかを指定する必要があります。そうすることで、どの子を強調表示するかを決定するロジックはすべてコード内の1か所にあり、ユーザーが子供の1人をタップしたときにのみ所有者コンポーネントを再レンダリングする必要があります。
各子供のタッチイベントを検出するには、TouchableWithoutFeedback
コンポーネントで囲みます。
class OwnerComponent extends React.Component {
state = {
selectedChildIndex: null,
};
render() {
return (
<ScrollView>
<TouchableWithoutFeedback
onPressIn={() => {
this.setState({ selectedChildIndex: 0);
}}
onPressOut={() => {
this.setState({ selectedChildIndex: null);
}}
style={styles.touchableContainer}>
<View style={this.getChildStyle(0)} />
</TouchableWithoutFeedback>
<TouchableWithoutFeedback
onPressIn={() => {
this.setState({ selectedChildIndex: 1);
}}
onPressOut={() => {
this.setState({ selectedChildIndex: null);
}}
style={styles.touchableContainer}>
<View style={this.getChildStyle(1)} />
</TouchableWithoutFeedback>
</ScrollView>
);
}
getChildStyle(childIndex) {
// No child is selected
if (this.state.selectedChildIndex === null) {
return null;
}
return (this.state.selectedChildIndex === childIndex) ?
styles.highlightedChild :
styles.dimmedChild;
}
}
let styles = StyleSheet.create({
touchableContainer: {
flexGrow: 1,
},
highlightedChild: {
backgroundColor: 0xf0f0f0;
},
dimmedChild: {
opacity: 0.7,
},
});
出典
2016-11-24 22:39:42
ide
あなたはどのようにコードを追加できますか? –
さて、選択した子の状態を状態に保存させておき、各子が自分のIDを選択したIDと比較させることができます。それが同じであれば、強調表示する必要があります。そうでない場合は、暗くする必要があります。 私の頭のアプローチのちょうど上、それが助けて欲しい! –