Skip to content

Commit 91cf07d

Browse files
committed
Optimize the jitter problem of AnySizeDragSortableView Android
1 parent 3c6b40d commit 91cf07d

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

Example/app/widget/AnySizeDragSortableView.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ export default class AnySizeDragSortableView extends React.PureComponent {
174174
const key = keyExtractor(item, index);
175175
const curLayout = this.layoutMap.get(key)
176176
const firstOffsetY = (this.curScrollData && this.curScrollData.offsetY) || 0
177-
const initTop = curLayout.y - firstOffsetY + headerViewHeight
177+
const initTop = parseInt(curLayout.y - firstOffsetY + headerViewHeight + 0.5)
178178
this.setState({
179179
scrollEnabled: false,
180180
selectedItem: item,
181181
selectedKey: key,
182182
selectedOriginLayout: {...curLayout},
183183
selectedPosition: {
184-
left: curLayout.x,
184+
left: parseInt(curLayout.x+0.5),
185185
top: initTop,
186186
initTop,
187187
width: curLayout.width,
@@ -251,7 +251,6 @@ export default class AnySizeDragSortableView extends React.PureComponent {
251251
}
252252
}
253253
}
254-
255254
if (!this.isUpdating) {
256255
const moveX1 = selectedOriginLayout.x + dx + childMarginLeft
257256
const moveX2 = moveX1 + selectedOriginLayout.width - childMarginRight
@@ -288,13 +287,20 @@ export default class AnySizeDragSortableView extends React.PureComponent {
288287
}
289288
}
290289

291-
this.setState({
292-
selectedPosition: {
293-
...selectedPosition,
294-
left: selectedOriginLayout.x + dx,
295-
top: selectedPosition.initTop + (moveY - y0)
296-
}
297-
})
290+
const preLeft = selectedPosition.left
291+
const preTop = selectedPosition.top
292+
const nextLeft = parseInt(selectedOriginLayout.x + dx + 0.5)
293+
const nextTop = parseInt(selectedPosition.initTop + (moveY - y0) + 0.5)
294+
295+
if (preLeft !== nextLeft || preTop !== nextTop) {
296+
this.setState({
297+
selectedPosition: {
298+
...selectedPosition,
299+
left: nextLeft,
300+
top: nextTop
301+
}
302+
})
303+
}
298304
};
299305

300306
move = (fromKey, toKey, vy, isDiffline) => {
@@ -472,7 +478,7 @@ AnySizeDragSortableView.propTypes = {
472478

473479
AnySizeDragSortableView.defaultProps = {
474480
areaOverlapRatio: 0.55, // Must be greater than 0.5
475-
autoThrottle: 5,
481+
autoThrottle: 2,
476482
autoThrottleDuration: 10,
477483
scrollIndicatorInsets: {
478484
top: 0,

lib/AnySizeDragSortableView.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ export default class AnySizeDragSortableView extends React.PureComponent {
174174
const key = keyExtractor(item, index);
175175
const curLayout = this.layoutMap.get(key)
176176
const firstOffsetY = (this.curScrollData && this.curScrollData.offsetY) || 0
177-
const initTop = curLayout.y - firstOffsetY + headerViewHeight
177+
const initTop = parseInt(curLayout.y - firstOffsetY + headerViewHeight + 0.5)
178178
this.setState({
179179
scrollEnabled: false,
180180
selectedItem: item,
181181
selectedKey: key,
182182
selectedOriginLayout: {...curLayout},
183183
selectedPosition: {
184-
left: curLayout.x,
184+
left: parseInt(curLayout.x+0.5),
185185
top: initTop,
186186
initTop,
187187
width: curLayout.width,
@@ -251,7 +251,6 @@ export default class AnySizeDragSortableView extends React.PureComponent {
251251
}
252252
}
253253
}
254-
255254
if (!this.isUpdating) {
256255
const moveX1 = selectedOriginLayout.x + dx + childMarginLeft
257256
const moveX2 = moveX1 + selectedOriginLayout.width - childMarginRight
@@ -288,13 +287,20 @@ export default class AnySizeDragSortableView extends React.PureComponent {
288287
}
289288
}
290289

291-
this.setState({
292-
selectedPosition: {
293-
...selectedPosition,
294-
left: selectedOriginLayout.x + dx,
295-
top: selectedPosition.initTop + (moveY - y0)
296-
}
297-
})
290+
const preLeft = selectedPosition.left
291+
const preTop = selectedPosition.top
292+
const nextLeft = parseInt(selectedOriginLayout.x + dx + 0.5)
293+
const nextTop = parseInt(selectedPosition.initTop + (moveY - y0) + 0.5)
294+
295+
if (preLeft !== nextLeft || preTop !== nextTop) {
296+
this.setState({
297+
selectedPosition: {
298+
...selectedPosition,
299+
left: nextLeft,
300+
top: nextTop
301+
}
302+
})
303+
}
298304
};
299305

300306
move = (fromKey, toKey, vy, isDiffline) => {
@@ -472,7 +478,7 @@ AnySizeDragSortableView.propTypes = {
472478

473479
AnySizeDragSortableView.defaultProps = {
474480
areaOverlapRatio: 0.55, // Must be greater than 0.5
475-
autoThrottle: 5,
481+
autoThrottle: 2,
476482
autoThrottleDuration: 10,
477483
scrollIndicatorInsets: {
478484
top: 0,

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-drag-sort",
3-
"version": "2.4.3",
3+
"version": "2.4.4",
44
"description": "Drag and drop sort control for react-native",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)