React Native Flatlist不会动态渲染项目的宽度

时间:2018-05-28 12:07:02

标签: javascript css react-native react-native-flatlist

我正在尝试使用ui kit nachos-ui中的平面列表和消息气泡样式创建消息传递接口,但是我无法根据文本量使平板列表呈现不同宽度的文本气泡。每当我第一次发送消息时,气泡似乎都是一个不错的宽度:

enter image description here

然而,每当我发送另一条消息时,虽然在这些图片中很难看到,但它会改变上一条消息的宽度并将新消息收缩到似乎是最大宽度

enter image description here

以下是我的平面列表代码:

<FlatList
          data={this.state.messages}
          style={{marginLeft: 280}}
          ref={ref => this.flatList = ref}
          onContentSizeChange={() => this.flatList.scrollToEnd({animated: true})}
          onLayout={() => this.flatList.scrollToEnd({animated:true})}
          keyExtractor = {item => item.timestamp}
          renderItem={({item}) => <Bubble style={{marginTop: 20}} color="#FFC800"
           >{item.contents}</Bubble>}
        />

整个组件的代码也可能有助于诊断问题:

<KeyboardAvoidingView style={styles.container} 
      behavior="padding"
      keyboardVerticalOffset={64}>
      <KeyboardAvoidingView style={styles.inputContainer}>

      <FlatList
          data={this.state.messages}
          style={{marginLeft: 280}}
          ref={ref => this.flatList = ref}
          onContentSizeChange={() => this.flatList.scrollToEnd({animated: true})}
          onLayout={() => this.flatList.scrollToEnd({animated:true})}
          keyExtractor = {item => item.timestamp}
          renderItem={({item}) => <Bubble style={{marginTop: 20}} color="#FFC800"
           >{item.contents}</Bubble>}
        />
      <View style={{flexDirection:'row', backgroundColor: 'transparent'}}>
      <Input
        containerStyle={{marginVertical: 10, width:300, marginLeft: 20}}
        inputStyle={styles.textInput}
        keyboardAppearance="dark"
        placeholder=""
        autoCorrect={false}
        onChangeText={(message) => { this.setState({message})}}
        value={this.state.message}
        />
        <Button 
          buttonStyle={{borderRadius: 25, marginTop: 40, marginLeft: 10, paddingVertical: 5, backgroundColor: "#9214FF"}}
          icon={{name: 'arrow-up', type: 'feather', color:'white'}}
          onPress={()=>{Keyboard.dismiss; this.onPressButton()}} 
          title=""/> 
        </View>
        </KeyboardAvoidingView>
      </KeyboardAvoidingView>

如何使用基于提交的文本数量的动态大小呈现每条消息的平面列表,而不更改任何先前的消息,并且没有预定的最大宽度?

1 个答案:

答案 0 :(得分:1)

问题在于您的FlatList样式:

style={{marginLeft: 280}}

它“压缩”了右边的所有物品,总是宽度相同。

更好的方法是删除此marginLeft并将右边的气泡与:

对齐

<FlatList style= {{ alignItems: 'flex-end' }} ...>