如何阻止React-Native FlatList干扰其他组件

时间:2018-06-10 19:32:56

标签: reactjs react-native react-native-flatlist

我有一个简单的React-Native应用程序,它使用FlatList和Redux。问题是当列表变长并到达输入元素所在的屏幕底部时,即使它们位于另一个组件和容器中,它也会破坏这些输入元素。我为此尝试了一百万次修复,但似乎没有任何效果。

我怎么能做只有FlatList占据屏幕2/3的东西?

这是问题的屏幕截图(当项目到达输入框时,它会导致输入框缩小并被中断):

enter image description here

这是包含我所有组件的应用文件:

export default class App extends Component {
  render() {
    return (
  <Provider store={createStore(reducers)}>
    <View style={{ flex: 1 }}>
        <ItemsList />
        <AddItem />
    </View>
  </Provider>
    );
  }
}

这是使用FlatList的组件:

class ItemsList extends Component {

    render() {
        return (
            <List>
                <FlatList
                    data={this.props.items} 
                    renderItem={({ item }) => (
                        <ListItem 
                            name={item.item} id={item.id}
                        />
                    )}
                    keyExtractor={item => item.id.toString() }
                />
            </List>
          );

        }
    }

  const mapStateToProps = state => {
    return { items:  state.items };
  };


export default connect(mapStateToProps)(ItemsList);

addItem的代码是:

class AddItem extends Component {

    state = {
        item: "",
        quantity: ""
    }

    onButtonPress() {
        this.props.addItem(this.state)
        this.setState({
            item: "",
            quantity: 0
        })
    }

    render() {

    const { input, container, add, addText } = styles;
        return (
            <View style={container}>
                <TextInput placeholder="add item" 
                    placeholderTextColor="rgba(0, 0, 0, 0.5)" 
                    style={input} 
                    onChangeText={item => this.setState({ item })}
                    value={this.state.item}
                />
                <TextInput placeholder="add item" 
                    placeholderTextColor="rgba(0, 0, 0, 0.5)" 
                    style={input} 
                    onChangeText={quantity => this.setState({ quantity })}
                />
                <TouchableOpacity style={add} onPress={this.onButtonPress.bind(this)}>
                    <Text style={addText}>Add Item</Text>
                </TouchableOpacity>
            </View>
        );
    }
}

export default connect(null, {addItem})(AddItem);

const styles = {
    input: {
        backgroundColor: 'rgb(208, 240, 238)',
        paddingVertical: 15,
        paddingHorizontal: 10,
        marginBottom: 5
        },
    add: {
        backgroundColor: 'black',
        paddingVertical: 15,
    },
    addText: {
        textAlign: 'center',
        color: 'white'
    },
    container: {
        padding: 20,
        flex: 1,
        justifyContent: 'flex-end'
    }
  };

1 个答案:

答案 0 :(得分:0)

首先,从django-admin startproject my_project cd my_project python manage.py runserver 移除<List>,因为您已使用ItemsList。然后,为了让FlatList占据屏幕高度的2/3,请执行以下操作:

FlatList