-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyScrollview
More file actions
92 lines (86 loc) · 2.81 KB
/
myScrollview
File metadata and controls
92 lines (86 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Scroll to a Specific Item in ScrollView List View
// https://aboutreact.com/scroll_to_a_specific_item_in_scrollview_list_view/
// import React in our code
import React, { useState, useEffect } from 'react';
// import all the components we are going to use
import { SafeAreaView, View, ScrollView, StyleSheet, Text, TouchableOpacity, TextInput } from 'react-native';
const App = () => {
const [dataSource, setDataSource] = useState([
{
title: 'Restaurant',
subTitle: '10.20 min - $0.99 Delivery Fee',
},
{
title: 'Clothing',
subTitle: '10.20 min',
},
{
title: 'GroceryGourmet',
subTitle: '10.20 min - $0.99 Delivery Fee',
},
{
title: 'Liquor',
subTitle: '10.20 min',
},
{
title: 'Books',
subTitle: '10.20 min - $0.99 Delivery Fee',
},
{
title: 'CellPhones',
subTitle: '10.20 min',
},
{
title: 'Computers',
subTitle: '10.20 min - $0.99 Delivery Fee',
},
{
title: 'VideoGames',
subTitle: '10.20 min',
},
]);
const [scrollToIndex, setScrollToIndex] = useState(0);
const [dataSourceCords, setDataSourceCords] = useState([]);
const [ref, setRef] = useState(null);
const getItem = (item, key) => {
if (dataSourceCords.length > key) {
ref.scrollTo({
x: dataSourceCords[key - 1],
y: 0,
animated: true,
});
} else {
alert('Out of Max Index');
}
};
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ backgroundColor: '#fff' }}>
<ScrollView
horizontal
ref={(ref) => {
setRef(ref);
}}
>
{dataSource.map((item, key) => {
return (
<View
key={key}
onLayout={(event) => {
const layout = event.nativeEvent.layout;
dataSourceCords[key] = layout.x;
setDataSourceCords(dataSourceCords);
}}
>
<Text style={{ padding: 10 }} onPress={() => getItem(item, key)}>
{key}. {item.title}
</Text>
</View>
);
})}
</ScrollView>
</View>
</SafeAreaView>
);
};
export default App;