私は、リストの最初の要素が「追加」ボタンであるグリッドとして画像のリストを表示するアプリケーションを持っています。私はまた、すべての画像をTouchableにする必要があります。現在、私のコードは次のようになります場合、誰かに異なるコールバックを持つTouchableOpacityアイテムのグリッドを作成する最も良い方法は?
class RNtest extends Component {
constructor() {
super();
this.state = {
data: [],
counter: 0 //Used to give unique numbers to the items
};
}
itemPicker() {
var temp = this.state.data;
temp.push({text: this.state.counter});
this.setState({
data: temp,
counter: this.state.counter + 1
});
}
onPress() {
console.warn('YO!');
}
render() {
return (
<ScrollView style={styles.body}>
<View style={styles.section}>
<Text style={styles.sectionHeader}>ITEMS</Text>
<View style={styles.gallery}>
<TouchableOpacity
style={styles.addButton}
onPress={this.itemPicker.bind(this)}
>
<Text>ADD</Text>
</TouchableOpacity>
{this.state.data.map(function(item) {
return (
<TouchableOpacity
style={styles.item}
onPress={this.onPress.bind(this)}
>
<Text>{item.text}</Text>
</TouchableOpacity>
);
})}
</View>
</View>
</ScrollView>
);
}
}
スタイルシート、それを必要とします:
var styles = StyleSheet.create ({
addButton: {
alignItems: 'center',
justifyContent: 'center',
height: 72,
width: 72,
borderRadius: 2,
marginVertical: 6,
marginRight: 6,
backgroundColor: 'white'
},
gallery: {
flexDirection: 'row',
flexWrap: 'wrap'
},
item: {
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#DCFFC2',
height: 72,
width: 72,
borderRadius: 2,
margin: 6
},
section: {
flexDirection: 'column',
paddingTop: 10
},
sectionHeader: {
fontSize: 13,
fontWeight: '400',
paddingBottom: 1,
color: '#A7A7A7'
},
body: {
flex: 1,
backgroundColor: '#EEEEEE',
paddingVertical: 10,
paddingHorizontal: 20
},
});
明らかthis
は、forループ内の表示されていないので、当然たonPress機能が動作しません。
私はListViewを使用することができましたが、renderRowの内部では、レンダリングするアイテムが追加ボタンであるかどうかを確認しなければならず、いくつかの他の部分も同様に難しくなります。
このような状況では、私は参考文献を使用することができますか?いいですか?
私はAndroidエミュレータでReact Native 0.27.2を実行しています。
ありがとう、これは美しく動作します。私はReact Nativeで多くの経験を積んでいないので、他の間違いを指摘してくれてうれしいです。 –