Skip to content

Instantly share code, notes, and snippets.

@svgerasimov
Created April 25, 2019 14:00
Show Gist options
  • Save svgerasimov/305577b95b5588cc27c809755d4d1adf to your computer and use it in GitHub Desktop.
Save svgerasimov/305577b95b5588cc27c809755d4d1adf to your computer and use it in GitHub Desktop.
draggable_flatlist
import React from 'react';
import {
SafeAreaView,
View,
FlatList,
Text,
StyleSheet,
Animated,
PanResponder
} from 'react-native';
import { v4 } from 'uuid';
export default class App extends React.PureComponent{
state = {
data: [
{ id: v4(), title: 'Lightcoral', hex: '#eb7474' },
{ id: v4(), title: 'Orchid', hex: '#eb74dc' },
{ id: v4(), title: 'Mediumpurple', hex: '#9a74eb' },
{ id: v4(), title: 'Mediumslateblue', hex: '#8274eb' },
{ id: v4(), title: 'Skyblue', hex: '#74b6eb' },
{ id: v4(), title: 'Paleturquoise', hex: '#93ece2' },
{ id: v4(), title: 'Palegreen', hex: '#93ecb6' },
{ id: v4(), title: 'Khaki', hex: '#d3ec93' }
]
}
_position = new Animated.ValueXY()
_panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderMove: (event, gesture) => {
this._position.setValue({ x: gesture.dx, y: gesture.dy})
},
onPanResponderRelease: () => {}
})
_keyExtractor = (item) => item.id;
_renderItem = ({item}) => {
return (
<Animated.View
style={this._position.getLayout()}
{...this._panResponder.panHandlers}
>
<View style={{
backgroundColor: `${item.hex}`,
width: 80,
height: 80,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 1,
borderColor: '#fff'
}}>
<Text>{item.title}</Text>
</View>
</Animated.View>
)
}
render() {
const { data } = this.state
return (
<SafeAreaView style={{flex: 1}}>
<View style={{
flex: 1,
justifyContent: 'space-between',
alignItems: 'center',
backgroundColor: '#fff'
}}>
<View style={{ borderColor: 'black', borderWidth: 1, overflow: 'visible', height: 80 }}>
<FlatList
data={data}
keyExtractor={this._keyExtractor}
renderItem={this._renderItem}
horizontal={true}
removeClippedSubviews={false}
/>
</View>
</View>
</SafeAreaView>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment