如何使ListView上的滑动功能(本机)? [英] How to make swipe function on listview (react-native)?

查看:73
本文介绍了如何使ListView上的滑动功能(本机)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有物品清单.我正在使用 ListView ,并且我需要能够删除通过向左或向右滑动,任意行.

I have list of items. I am using ListView for it and I need to be able to delete any row by swiping left or right.

我可以从这里开始吗?

推荐答案

如果愿意,请按照反应本机刷卡.

If you prefer, follow this guide which uses React Native Swipeout.

否则,这是我的SwipeList和SwipeListRow组件.我部分使用我的库 Cairn 进行样式设置,但如果您愿意的话,应该可以轻松地将其翻译成普通的React样式表为此:

Otherwise, here's my SwipeList and SwipeListRow component. I partially use my library Cairn for styling, but it should be easily translated into a normal React Stylesheet if you care to do so:

import React from 'react';
import { Text, ListView } from 'react-native';
import styleContext from 'app/style';

const style = styleContext.extend({
    listViewSection: {
        paddingVertical: 10,
        paddingLeft: 15,
        backgroundColor: '$greyDefault'
    },

    'text.listViewSection': {
        color: '$greyMid',
        fontSize: 16,
        marginLeft: 5
    }
});

function SwipeList({ dataSource, renderRow }) {
    function renderSectionHeader(sectionData, sectionId) {
        return (
            <View {...style('listViewSection')}>
                <Text {...style('text.listViewSection')}>{sectionId.toUpperCase()}</Text>
            </View>
        );
    }

    if (!dataSource.rowIdentities.length) {
        return (
            <Text>No items found.</Text>
        );
    }

    return (
        <ListView
            dataSource={dataSource}
            automaticallyAdjustContentInsets={false}
            directionalLockEnabled
            keyboardShouldPersistTaps={false}
            keyboardDismissMode={'on-drag'}
            renderSectionHeader={renderSectionHeader}
            renderRow={renderRow} />
    );
}

SwipeList.propTypes = {
    dataSource: React.PropTypes.shape({
        rowIdentities: React.PropTypes.array.isRequired
    }).isRequired,
    renderRow: React.PropTypes.func.isRequired
};

export default SwipeList;


SwipeListRow.js

import React from 'react';
import {
    View,
    Text,
    ScrollView,
    Animated,
    Dimensions
} from 'react-native';

import styleContext from 'app/style';

const { width } = Dimensions.get('window');
const style = styleContext.extend({
    swipeMessage: {
        position: 'absolute',
        top: 0,
        height: 75,
        justifyContent: 'center',
        alignItems: 'center'
    },

    itemContainer: {
        width
    }
});

const WHITE = 0;
const GREEN = 1;
const AnimatedScrollView = Animated.createAnimatedComponent(ScrollView);

class SwipeListRow extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            color: new Animated.Value(WHITE)
        };
    }

    animateScroll = (e) => {
        const threshold = width / 5;
        let x = e.nativeEvent.contentOffset.x;
        let swiped = null;

        x = x * -1;

        if (x > -50 && this.swiped !== WHITE) {
            swiped = WHITE;
        } else if (x < -50 && x > -threshold && this.swiped !== GREEN) {
            swiped = GREEN;
        }

        if (swiped !== null) {
            this.swiped = swiped;

            Animated.timing(this.state.color, {
                toValue: swiped,
                duration: 200
            }).start();
        }
    }

    takeAction = () => {
        if (this.swiped) {
            Animated.timing(this.state.color, {
                toValue: WHITE,
                duration: 200
            }).start();

            this.props.onSwipe();
        }
    }

    render() {
        const { swipeEnabled, swipeMessage, children } = this.props;
        const bgColor = this.state.color.interpolate({
            inputRange: [
                WHITE,
                GREEN
            ],
            outputRange: [
                'rgb(255, 255, 255)',
                'rgb(123, 204, 40)'
            ]
        });

        return (
            <View>
                <AnimatedScrollView
                    horizontal
                    directionalLockEnabled
                    automaticallyAdjustContentInsets={false}
                    onScroll={this.animateScroll}
                    scrollEventThrottle={16}
                    scrollEnabled={swipeEnabled}
                    onMomentumScrollBegin={this.takeAction}
                    style={[{ flex: 1 }, { backgroundColor: bgColor }]}>
                    <View>
                        <View {...style('itemContainer')}>
                            {children}
                        </View>
                        <View
                            {...style(
                                'swipeMessage',
                                [{ width: width / 5, right: -width / 5 - 20 }]
                            )}>
                            <Text {...style('text.bold text.white')}>{swipeMessage}</Text>
                        </View>
                    </View>
                </AnimatedScrollView>
            </View>
        );
    }
}

SwipeListRow.propTypes = {
    children: React.PropTypes.node.isRequired,
    onSwipe: React.PropTypes.func.isRequired,
    swipeEnabled: React.PropTypes.bool.isRequired,
    swipeMessage: React.PropTypes.string.isRequired
};


export default SwipeListRow;

有了这些组件,现在您所要做的就是将所需的数据源传递给普通的列表视图,如

With these components, now all you must do is pass in the required datasource as you would to a normal list view, as described on the ListView component documentation.

    <SwipeList
        dataSource={dataSource}
        renderRow={(item) => (
            <SwipeListRow
                key={item.id}
                swipeMessage={'Delete Item'}
                onSwipe={() => deleteItem(item)}
                swipeEnabled={true}>
                <<< INSERT DISPLAY OF ROW HERE >>>
            </SwipeListRow>
        )} />

这篇关于如何使ListView上的滑动功能(本机)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆