来源网络,非原创

转载自:react native的滑动吸顶动画

代码片段:

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* eslint-disable react-native/no-color-literals */
import React, { Component } from 'react';
import {
View, Text, StyleSheet, Animated, PixelRatio, Dimensions,
} from 'react-native';

function getUIPt(px) {
const { width } = Dimensions.get('window');
return PixelRatio.roundToNearestPixel((px * width) / 750);
}

export default class TestPage9 extends Component {
scrollY = new Animated.Value(0)

constructor(props) {
super(props);
this.state = {
list: null,
topIndexList: [], // 被指定的index
pageNum: 0,
isLoading: false,
};
}

timer1=null

// 模拟请求接口的
getTempList= (from) => {
const { list, topIndexList, pageNum } = this.state;
console.log("getTempList", from, list, topIndexList, pageNum);
if (this.timer1) {
clearTimeout(this.timer1);
}
this.setState({ isLoading: true });
this.timer1 = setTimeout(() => {
if (!list) {
topIndexList.push(0);
this.setState({
list: [{ title: `0我是一个准备置顶的`, stickable: true }, ...this.randomDate()],
topIndexList,
pageNum: pageNum + 1,
isLoading: false,
});
} else if (list && pageNum < 5) {
topIndexList.push(list.length);
list.push({ title: `${list.length}我是一个准备置顶的`, stickable: true });
list.push(...this.randomDate());
this.setState({
list, topIndexList, pageNum: pageNum + 1, isLoading: false,
});
} else {
this.setState({ isLoading: false });
}
}, 300);
}

// 瞎几把随机数据的
randomDate() {
const tempDate = parseInt(Math.random() * 80, 10) + 1;
const arr = [];
for (let index = 0; index < tempDate; index++) {
arr.push({ title: `我是一个普通的列表数据¥${index}`, stickable: false });
}
return arr;
}

renderItem = ({ item, index }) => {
const { stickable, title } = item;
if (stickable) {
return (
<View>
<Text style={styles.stickableTitle}>{title}</Text>
</View>
);
}
return (
<View>
<Text style={styles.originTitle}>{title}</Text>
</View>
);
}

componentDidMount() {
this.getTempList(0);
}

componentWillUnmount() {
if (this.timer1) {
clearTimeout(this.timer1);
}
}

renderFooter = (isLoading, pageNum, dataList) => {
const listLength = dataList.length;
if (listLength <= 3) {
return (

<View style={{ height: (5 - listLength) * getUIPt(100) }} />
);
}
if (isLoading && pageNum > 1) {
return (<Text>加载中…</Text>);
}
return null;
};

render() {
const {
list, topIndexList, pageNum, isLoading,
} = this.state;
return (
<View style={styles.Container}>
<HeaderLayout />
<MyFilter scrollY={this.scrollY} />
{ (!!list && list.length > 0&& (
<Animated.FlatList
contentContainerStyle={styles.FlatListContainer}
data={list}
ListFooterComponent={() => this.renderFooter(isLoading, pageNum, list)}
renderItem={this.renderItem}
keyExtractor={(item, index) => `${index}`}
showsVerticalScrollIndicator={false}
stickyHeaderIndices={topIndexList}
scrollEventThrottle={1}
onEndReached={() => { this.getTempList(1); }}
onEndReachedThreshold={0.3}
onScroll={
Animated.event(
[
{
nativeEvent: {
contentOffset: { y: this.scrollY },
},
},
],
{
useNativeDriver: true,


}
)
}
/>
))}
</View>
);
}
}
class MyFilter extends Component {
render() {
const { scrollY } = this.props;
const translateY = scrollY.interpolate({
inputRange: [0, getUIPt(230), getUIPt(230) + 1],
outputRange: [getUIPt(230), 0, 0],
});
return (
<Animated.View style={[styles.MyFilterContaioner, { transform: [{ translateY }] }]}>
<Text style={styles.MyFilterContaionerLabel}>我是赛选条件,我需要固定</Text>
</Animated.View>
);
}
}
class HeaderLayout extends Component {
render() { return (<Text style={styles.headerLayout}>我是一个很高的头部固定的</Text>); }
}
const styles = StyleSheet.create({
headerLayout: {
position: 'absolute',
backgroundColor: '#FEEBC9',
width: getUIPt(750),
height: getUIPt(230),
lineHeight: getUIPt(230),
textAlign: 'center',

},
FlatListContainer: {
paddingTop: getUIPt(230),
marginTop: getUIPt(80),
paddingBottom: getUIPt(80),
},
Container: {

},
MyFilterContaioner: {

position: 'absolute',
zIndex: 3,
top: 0,
},
MyFilterContaionerLabel: {
lineHeight: getUIPt(80),
height: getUIPt(80),
textAlign: 'center',
backgroundColor: '#70BB66',
color: '#000',
width: getUIPt(750),
},
stickableTitle: {
color: '#fff',
backgroundColor: '#A0BB66',
lineHeight: getUIPt(80),
},
originTitle: {
height: getUIPt(84),
backgroundColor: '#A0BBE7',
color: '#999',
},
});