【问题标题】:React Native Flatlist won't render item's width dynamicallyReact Native Flatlist 不会动态呈现项目的宽度
【发布时间】:2018-05-28 12:07:02
【问题描述】:

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

但是,每当我发送另一条消息时,虽然在这些图片中很难看到,但它会更改前一条消息的宽度并将新消息限制为似乎是最大宽度

这是我的平面列表代码:

<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>

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

【问题讨论】:

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


    【解决方案1】:

    问题在于您的 FlatList 样式:

    style={{marginLeft: 280}}

    它“压缩”了右侧的所有项目,始终具有相同的宽度。

    一个更好的方法是去掉这个marginLeft并将右边的气泡对齐:

    &lt;FlatList style= {{ alignItems: 'flex-end' }} ...&gt;

    【讨论】:

    • 这似乎只是让消息拉伸了整个屏幕的宽度,而且它们也只有一种尺寸,即屏幕宽度。每当我尝试在平面列表之外的视图中放置消息气泡时,宽度对应于文本的数量,但是在平面列表中它似乎只有固定大小,或者先前消息的大小会随着每条新消息而变化。跨度>
    • 好的,你可以试试&lt;Bubble style={{ alignSelf: 'flex-end', marginTop: 20}} color="#FFC800" &gt;{item.contents}&lt;/Bubble&gt;}(仍然没有marginLeft)
    • 哇,效果很好,谢谢!很抱歉一直打扰您,但您是否也知道设置气泡最大宽度的方法?
    • &lt;Bubble style={{ alignSelf: 'flex-end', marginTop: 20, maxWidth:100 }}&gt;
    • 没想到这么简单哈哈,非常感谢您的帮助。
    猜你喜欢
    • 2020-08-07
    • 2019-03-17
    • 1970-01-01
    • 2022-01-16
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2020-06-20
    相关资源
    最近更新 更多