項目背景
項目中有個需求需要刪除一個Listview的item,但是有問題。是這樣的,我有10條數(shù)據(jù),屏幕只能顯示5條,所以我滑動到最后一條,選擇刪除,然后刷新state,但是刪除的那個item沒了,留出來一塊空白,沒有其他數(shù)據(jù)補位的,用手一觸碰屏幕,整個listview都會一閃爍,布局又排正好了。
js代碼
<ListView
ref="_listView"
dataSource={this.state.dataSource}
renderRow={this.renderItemView.bind(this)}
refreshControl={
<RefreshControl
refreshing={this.state.isRefreshing}
onRefresh={this.onRefresh.bind(this)}
tintColor="#ff0000"
title="Loading..."
titleColor="#00ff00"
colors={['#ff0000', '#00ff00', '#0000ff']}
progressBackgroundColor="#ffffff"
/>}
/>
心想著如果不能正確顯示,起碼在刪除后把整個列表滾動到最后邊也行,所以搜索了一下Listview的Api,有一個scrollTo()的方法,于是就抱著試試看的態(tài)度
this.refs._listView.scrollTo({x: 0, y: 0, animated: true});
意想不到的事情發(fā)生了,列表沒有滑動到最上邊,但是沒有空白的view了,上邊的會自動填充到下邊了。于是我把這行復(fù)制了一次,調(diào)用了兩次這個方法
this.refs._listView.scrollTo({x: 0, y: 0, animated: true});
this.refs._listView.scrollTo({x: 0, y: 0, animated: true});
列表滑動到最上邊了!
謹記這次的開發(fā)教訓(xùn),畢竟React-native現(xiàn)在還不成熟。