-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollViewHorizotal
More file actions
141 lines (130 loc) · 4.36 KB
/
ScrollViewHorizotal
File metadata and controls
141 lines (130 loc) · 4.36 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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([]);
const [scrollToIndex, setScrollToIndex] = useState(0);
const [dataSourceCords, setDataSourceCords] = useState([]);
const [ref, setRef] = useState(null);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/posts')
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
setDataSource(responseJson);
})
.catch((error) => {
console.error(error);
});
}, []);
const scrollHandler = () => {
console.log(dataSourceCords.length, scrollToIndex);
if (dataSourceCords.length > scrollToIndex) {
ref.scrollTo({
x: dataSourceCords[scrollToIndex - 1],
y: 0,
animated: true,
});
} else {
alert('Out of Max Index');
}
};
const ItemView = (item, key) => {
return (
// Flat List Item
<View
key={key}
style={styles.item}
onLayout={(event) => {
const layout = event.nativeEvent.layout;
dataSourceCords[key] = layout.x;
setDataSourceCords(dataSourceCords);
console.log(dataSourceCords);
console.log('height:', layout.height);
console.log('width:', layout.width);
console.log('x:', layout.x);
console.log('y:', layout.y);
}}
>
<Text style={styles.itemStyle} onPress={() => getItem(item)}>
{item.id}. {item.title}
</Text>
<ItemSeparatorView />
</View>
);
};
const ItemSeparatorView = () => {
return (
// Flat List Item Separator
<View style={styles.itemSeparatorStyle} />
);
};
const getItem = (item) => {
// Function for click on an item
alert('Id : ' + item.id + ' Title : ' + item.title);
};
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={styles.container}>
<View style={styles.searchContainer}>
<TextInput
value={String(scrollToIndex ? scrollToIndex : 0)}
numericvalue
keyboardType={'numeric'}
onChangeText={(scrollToIndex) => {
setScrollToIndex(parseInt(scrollToIndex != '' ? scrollToIndex : 0));
}}
placeholder={'Enter the index to scroll'}
style={styles.searchInput}
/>
<TouchableOpacity activeOpacity={0.5} onPress={scrollHandler} style={styles.searchButton}>
<Text style={styles.searchButtonText}>Go to Index</Text>
</TouchableOpacity>
</View>
{/* List Item as a function */}
<ScrollView
horizontal
ref={(ref) => {
setRef(ref);
}}
>
{dataSource.map(ItemView)}
</ScrollView>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: 'white',
},
itemStyle: {
padding: 10,
},
itemSeparatorStyle: {
height: 0.5,
width: '100%',
backgroundColor: '#C8C8C8',
},
searchContainer: {
flexDirection: 'row',
backgroundColor: '#1e73be',
padding: 5,
},
searchInput: {
flex: 1,
backgroundColor: 'white',
padding: 10,
},
searchButton: {
padding: 15,
backgroundColor: '#f4801e',
},
searchButtonText: {
color: '#fff',
},
});
export default App;