简体   繁体   English

如何使用 React Native FlatList 动画重新排序?

[英]How to animate reorder with React Native FlatList?

I have some UI that looks like this:我有一些看起来像这样的用户界面:

在此处输入图片说明

These list items can be: (1) added, (2) deleted, (3) reordered.这些列表项可以:(1) 添加,(2) 删除,(3) 重新排序。

The reordering is not drag and drop, but happens when clicking the check/empty circle icons - the checked items always stay at the top of the list.重新排序不是拖放,而是在单击选中/空圆圈图标时发生 - 选中的项目始终位于列表顶部。

There are many examples of animating adding/removing items from lists in React Native ( here's one such example ).有很多在 React Native 中为列表添加/删除项目的动画示例( 这是一个这样的示例)。

But how can I animate the ordering of a list?但是如何为列表的排序设置动画? Specifically in my case, it would be two list items swapping position.特别是在我的情况下,这将是两个列表项交换位置。

I have seen react-native-sortable-list and a few other open source projects, but I think this is probably overkill as I do not need drag and drop.我所看到的反应,本机可排序列表和其他一些开源项目,但我认为这可能是矫枉过正,因为我不需要拖放。 Also, I am already hooking into some FlatList events like onLayout , onContentSizeChange , and onScroll , so I would prefer a solution that allows me to animate the children of a FlatList directly.此外,我已经开始使用一些 FlatList 事件,如onLayoutonContentSizeChangeonScroll ,所以我更喜欢一种允许我直接为 FlatList 的子级设置动画的解决方案。

The flatlist is sorted in order of the array that you feed it through the data prop. flatlist 是按照你通过data prop 提供的数组的顺序排序的。 Whether you are using component-level state (ie this.state ) or redux to manage your state, you can reorder the array of elements and it should rerender in that order.无论您是使用组件级状态(即this.state )还是 redux 来管理您的状态,您都可以对元素数组重新排序,并且应该按该顺序重新渲染。 For example, one implementation may be:例如,一种实现可能是:

const checkedData = [...this.state.data].filter(item => item.checked === true);
const uncheckedData = [...this.state.data].filter(item => item.checked !== true);
const sortedData = checkData.concat(uncheckedData);
this.setState({ data: sortedData });

The javascript filter function should preserve original sort order of the subset. javascript 过滤器函数应保留子集的原始排序顺序。

To animate it, you can then look into LayoutAnimation in react native.要对其进行动画处理,您可以在 react native 中查看LayoutAnimation It will apply an animation type to every change in your view between renders.它将动画类型应用于渲染之间视图中的每个更改。

Just wanted to add onto Nth.gol 's answer which helped me solve the same issue.只是想添加 Nth.gol 的答案,这帮助我解决了同样的问题。

To animate the list in simple way:以简单的方式为列表设置动画:

  1. Update the state of the list to the new state you wish to animate to将列表的状态更新为您希望设置动画的新状态

  2. Add this line above the state update:在状态更新上方添加此行:

    LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);

  3. To make sure the animation also works on Andriod add this code outside of your constructor:为了确保动画也适用于 Andriod,请在构造函数之外添加以下代码:

if (Platform.OS === 'android' && UIManager.setLayoutAnimationEnabledExperimental) {
    UIManager.setLayoutAnimationEnabledExperimental(true);
}

Note: You will need to import LayoutAnimation && UIManager from react-native注意:您需要从react-native导入LayoutAnimation && UIManager

There are many options for configuring the duration & type of animation, but this is a nice & simple working solution.配置动画的持续时间和类型有很多选项,但这是一个很好且简单的工作解决方案。 You can read more about the configuration options here: LayoutAnimation您可以在此处阅读有关配置选项的更多信息: LayoutAnimation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM