mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-04 22:56:32 +08:00
Add support for updating adjacent separators on row highlight to FlatList
Summary: A nice bit of polish for apps is to update the separators between list items when press actives the highlight (things get especially noticeable/ugly when the separators are not full width and don't adjust). This can be difficult to coordinate and update efficiently, so we bake the functionality into `VirtualizedList`. The approach taken here is a little different from `ListView`. We pass a new `separators` object with `renderItem` that has easy-to-use callbacks for toggling the 'highlighted' prop on both adjacent separators - they can be wired up directly to the `onShow/HideUnderlay` props of `TouchableHighlight` (pit of success and all that - we want those RN apps to be polished!), but we also provide a more generic `separators.updateProps` method to set any custom props. This also passes in `leadingItem` so more custom wiring can be done on initial render (e.g. linking the highlight state with `Animated`). This also moves the separator rendering into the `CellRenderer`. I think this might also fix some subtle measurement bugs with the `onLayout` not capturing the height of the separator, so that's nice too, but the main reason is to have an easy place to store the state so we don't have to re-render the entire list like `ListView` does. Instead we track references to the cells and call update only on the two we care about so the feedback is instantaneous even with big, heavy lists. This diff also messes with a bunch of flow and updates the example to be more like a standard list. `SectionList` support is coming in a stacked diff. **TestPlan** Video demo: https://youtu.be/uFE7qGA0mg4 Pretty sure this is backwards compatible.... Reviewed By: thechefchen Differential Revision: D4833557 fbshipit-source-id: 685af46243ba13246bf280dae8a4223381ce8cea
This commit is contained in:
committed by
Facebook Github Bot
parent
926bfdb9f4
commit
f25df504ed
@@ -97,7 +97,6 @@ class MultiColumnExample extends React.PureComponent {
|
||||
</View>
|
||||
<SeparatorComponent />
|
||||
<FlatList
|
||||
ItemSeparatorComponent={SeparatorComponent}
|
||||
ListFooterComponent={FooterComponent}
|
||||
ListHeaderComponent={HeaderComponent}
|
||||
getItemLayout={this.state.fixedHeight ? this._getItemLayout : undefined}
|
||||
@@ -115,15 +114,18 @@ class MultiColumnExample extends React.PureComponent {
|
||||
);
|
||||
}
|
||||
_getItemLayout(data: any, index: number): {length: number, offset: number, index: number} {
|
||||
return getItemLayout(data, index);
|
||||
const length = getItemLayout(data, index).length + 2 * (CARD_MARGIN + BORDER_WIDTH);
|
||||
return {length, offset: length * index, index};
|
||||
}
|
||||
_renderItemComponent = ({item}) => {
|
||||
return (
|
||||
<ItemComponent
|
||||
item={item}
|
||||
fixedHeight={this.state.fixedHeight}
|
||||
onPress={this._pressItem}
|
||||
/>
|
||||
<View style={styles.card}>
|
||||
<ItemComponent
|
||||
item={item}
|
||||
fixedHeight={this.state.fixedHeight}
|
||||
onPress={this._pressItem}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
};
|
||||
// This is called when items change viewability by scrolling into or out of the viewable area.
|
||||
@@ -142,7 +144,18 @@ class MultiColumnExample extends React.PureComponent {
|
||||
};
|
||||
}
|
||||
|
||||
const CARD_MARGIN = 4;
|
||||
const BORDER_WIDTH = 1;
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
card: {
|
||||
margin: CARD_MARGIN,
|
||||
borderRadius: 10,
|
||||
flex: 1,
|
||||
overflow: 'hidden',
|
||||
borderColor: 'lightgray',
|
||||
borderWidth: BORDER_WIDTH,
|
||||
},
|
||||
row: {
|
||||
flexDirection: 'row',
|
||||
alignItems: 'center',
|
||||
|
||||
Reference in New Issue
Block a user