我正在嘗試使用List組件來處理大量的輸入,但是請注意,它在輸入輸入後會一直滾動回頂端。NativeBase(React Native)避免滾動回到頂部
不知道這是有關ListView always scrolls back to the top in react-native - 我曾試圖<List style={{flex> 1}} ..>
,但沒有運氣..
UPDATE
認爲它可能是更容易幫助我,如果我扔在一些代碼
import React, { Component } from 'react'
import { View } from 'react-native'
import { List, ListItem, InputGroup, Input, Icon, Button } from 'native-base'
export default class AddInformation extends Component {
constructor(props) {
super(props)
this.state = {
items:
[
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"},
{value: "", keyboardType: "default"}]}
}
render() {
return (
<List
dataArray={this.state.items}
renderRow={
(obj) => {
console.log(obj)
return (
<ListItem>
<InputGroup>
<Input
placeholder={`${obj.keyboardType} keyboard`}
onChangeText={ (text)=> {
//TODO
} }
keyboardType={obj.keyboardType}
/>
</InputGroup>
</ListItem>
)
}}>
</List>
)
}
}
更新2
仍然沒有工作..
import React, { Component } from 'react'
import { View, ListView, Text, TextInput } from 'react-native'
import { FormLabel, FormInput } from 'react-native-elements'
export default class AddInformation extends Component {
constructor(props) {
super(props)
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
}
componentDidMount() {
this.state = {
items: ds.cloneWithRows([
{hint: "foo", value: "", keyboardType: "default"},
...
{hint: "bar", value: "", keyboardType: "numeric"}
])
}
}
...
而且渲染方法:
render() {
return (
<View style={{flex: 1}}>
<ListView
dataSource={this.state.Specifications}
renderRow={(rowData) =>
<View>
<FormLabel>{rowData.hint}</FormLabel>
<FormInput
placeholder={`Keyboard: ${rowData.keyboardType}`}
/>
<TextInput />
</View>
}/>
</View>
)
}
}
不知道它有什麼做與NativeBase佈局..?
import React, { Component } from 'react'
import { Container, Content, Header, Title, Button, Icon } from 'native-base'
import AddInformation from './AddInformation'
export default class ScreenAddItemInformation extends Component {
render() {
return (
<Container>
<Header>
<Button transparent onPress={() => this.props.navigator.pop() }>
<Icon name='ios-backspace' />
</Button>
<Title>Add New Item</Title>
</Header>
<Content>
<AddInformation />
</Content>
</Container>
);
}
}
更新3
我只是嘗試了用硬一NB <List>
編碼<ListItem>
並沒有動態渲染。它仍然是,一旦鍵盤出現故障「查看」滾動回頂部的問題。
你有'rowHasChanged'正確實現,以便它不會渲染已經渲染的行嗎? –
@TudorConstantin不,我不知道。我使用NativeBase動態列表http://nativebase.io/docs/v0.5.13/components#list,它只能傳遞數組。 – Norfeldt
您可以在更新this.state.items的位置添加代碼。都鐸評論似乎是正確的。您應該嘗試直接使用ListView而不是List小部件。根據設置項目狀態的方式,ListView可能會通過List小部件重新呈現整個列表。 – while1