一:左右联动List,在工作中很常见。
注意:本例子必须修改源码,参考本文第三条。
二:Coding
ParcelPage.js:
var { width,height } = Dimensions.get('window');
let Headers = [];
export default class ParcelPage extends Component {
static navigationOptions = ({ navigation }) => ({
headerTitle : '联动List',});
componentDidMount() {
ParcelData.map((item,i) => {
Headers.push(item.section);
});
};
componentWillUnmount() {
Headers = [];
};
renderLRow = (item) => {
return (
<TouchableOpacity style={[ styles.lItem,{backgroundColor: item.index == this.state.cell ? 'white' : null} ]}
onPress={()=>this.cellAction(item)}>
cellAction = (item) => {
if (item.index <= ParcelData.length) {
this.setState({
cell : item.index
});
if (item.index > 0) {
var count = 0;
for (var i = 0;
i < item.index;
i++) {
count += ParcelData[ i ].data.length + 1
}
this.refs.sectionList.scrollToIndex({ animated : false,index : count })
} else {
this.refs.sectionList.scrollToIndex({ animated : false,index : 0 });
}
}
};
itemChange = (info) => {
let section = info.viewableItems[ 0 ].section.section;
if (section) {
let index = Headers.indexOf(section);
if (index < 0) {
index = 0;
}
this.setState({ cell : index });
}
};
state = {
cell : 0
};
renderRRow = (item) => {
return (
<View style={ styles.rItem }>
<Image style={ styles.icon } source={{ uri : item.item.img }}/>
<View style={ styles.rItemDetail }>
<Text style={ styles.foodName }>{ item.item.name }
<View style={ styles.saleFavorite }>
<Text style={ styles.saleFavoriteText }>{ item.item.sale }
<Text style={ [styles.saleFavoriteText,{ marginLeft:15 }]}>{ item.item.favorite }
sectionComp = (section) => {
return (
<View style={{height:30,backgroundColor:'#DEDEDE',justifyContent:'center',alignItems:'center'}}>
separator = () => {
return (
<View style={{height:1,backgroundColor:'gray'}}/>
)
};
render() {
return (
<View style={ styles.container }>
<FlatList
ref='FlatList'
style={ styles.leftList }
data={ ParcelData }
renderItem={(item) => this.renderLRow(item)}
ItemSeparatorComponent={ () => this.separator() }
keyExtractor={ (item) => item.section }
/>
<SectionList
ref='sectionList'
style={ styles.rightList }
renderSectionHeader={ (section) => this.sectionComp(section) }
renderItem={ (item) => this.renderRRow(item) }
sections={ ParcelData }
keyExtractor={ (item) => item.name }
onViewableItemsChanged={ (info) => this.itemChange(info) }
/>
const styles = StyleSheet.create({
container : {
flexDirection : 'row'
},leftList : {
width : 1 width / 4,backgroundColor : '#E9E9EF'
},lItem : {
minHeight : 44,justifyContent : 'center',},lText : {
marginLeft : 10,marginRight : 10,fontSize : 16,rightList : {
width : 3 width / 4
},rItem : {
flexDirection : 'row'
},rItemDetail : {
flex : 1,marginTop : 10,marginLeft : 5
},icon : {
height : 60,width : 60,marginBottom : 10,marginLeft : 8,borderWidth : 1,borderColor : '#999999'
},foodName : {
fontSize : 18,saleFavorite : {
flexDirection : 'row',marginTop : 5,marginBottom : 5,saleFavoriteText : {
fontSize : 13,moneyText : {
color : 'orange'
},});
ParcelData.js
三:修改源码
1-:SectionList
class SectionList<SectionT: SectionBase
extends React.PureComponent<DefaultProps,Props
props: Props
static defaultProps: DefaultProps = defaultProps;
render() {
const List = this.props.legacyImplementation ? MetroListView : VirtualizedSectionList;
return <List
ref={this._captureRef}
{...this.props} />;
}
_captureRef = (ref) => {
this._listRef = ref;
};
scrollToIndex = (params: { animated?: ?boolean,index: number,viewPosition?: number }) => {
this._listRef.scrollToIndex(params);
}
}
2-:VirtualizedSectionList
路径在node_modules/react-native/Libraries/Lists/VirtualizedSectionList.js,大概253行处修改如下:
; }_captureRef = (ref) => {
this._listRef = ref;
};
scrollToIndex = (params: { animated?: ?boolean,viewPosition?: number }) => {
this._listRef.scrollToIndex(params);
}