자식 뷰를 소유하는 구성 요소가 강조 표시되어 있는지 또는 희미하게 표시되는지를 지정해야합니다. 그런 식으로 어떤 아이를 강조해야 할지를 결정하는 모든 논리는 코드에서 한 곳에서 이루어 지므로 사용자가 자녀 중 하나를 두드리면 소유자 구성 요소 만 다시 렌더링해야합니다.
각 어린이의 터치 이벤트를 감지하려면 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
어떻게 코드를 추가 할 수 있습니까? –
음, 선택된 상태의 아이디를 상태로 저장 한 다음, 각 아이에게 자신의 아이디와 selectedId를 비교하게 할 수 있습니다. 동일하면 강조 표시해야합니다. 그렇지 않으면 희미 해져야합니다. 내 머리에 접근하는 것만으로도 도움이되기를 바랍니다. –