Skip to content

Commit e03d23c

Browse files
Add Android Support
1 parent 79e5fda commit e03d23c

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

lib/KeyboardAwareListView.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React from 'react'
44
import createReactClass from 'create-react-class'
5-
import { ListView } from 'react-native'
5+
import { ListView, Platform } from 'react-native'
66
import PropTypes from 'prop-types'
77
import KeyboardAwareMixin from './KeyboardAwareMixin'
88

@@ -13,6 +13,7 @@ const KeyboardAwareListView = createReactClass({
1313
x: PropTypes.number,
1414
y: PropTypes.number,
1515
}),
16+
enableOnAndroid: React.PropTypes.bool,
1617
},
1718
mixins: [KeyboardAwareMixin],
1819

@@ -22,6 +23,22 @@ const KeyboardAwareListView = createReactClass({
2223
},
2324

2425
render: function () {
26+
const {
27+
enableOnAndroid,
28+
contentContainerStyle,
29+
} = this.props
30+
31+
const {
32+
keyboardSpace,
33+
} = this.state
34+
35+
let newContentContainerStyle
36+
37+
if (Platform.OS === 'android' && enableOnAndroid) {
38+
newContentContainerStyle = {}
39+
newContentContainerStyle.paddingBottom = (contentContainerStyle ? contentContainerStyle.paddingBottom : 0) + keyboardSpace
40+
}
41+
2542
return (
2643
<ListView
2744
ref='_rnkasv_keyboardView'
@@ -30,6 +47,7 @@ const KeyboardAwareListView = createReactClass({
3047
showsVerticalScrollIndicator={true}
3148
scrollEventThrottle={0}
3249
{...this.props}
50+
contentContainerStyle={newContentContainerStyle || contentContainerStyle}
3351
onScroll={e => {
3452
this.handleOnScroll(e)
3553
this.props.onScroll && this.props.onScroll(e)

lib/KeyboardAwareMixin.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import PropTypes from 'prop-types'
4-
import ReactNative, { TextInput, Keyboard, UIManager } from 'react-native'
4+
import ReactNative, { TextInput, Keyboard, UIManager, Platform } from 'react-native'
55
import TimerMixin from 'react-timer-mixin'
66

77
import type { Event } from 'react-native'
@@ -72,8 +72,28 @@ const KeyboardAwareMixin = {
7272
if (isAncestor) {
7373
// Check if the TextInput will be hidden by the keyboard
7474
UIManager.measureInWindow(currentlyFocusedField, (x, y, width, height) => {
75-
if (y + height > frames.endCoordinates.screenY - this.props.extraScrollHeight - this.props.extraHeight) {
76-
this.scrollToFocusedInputWithNodeHandle(currentlyFocusedField)
75+
const textInputBottomPosition = y + height
76+
const keyboardPosition = frames.endCoordinates.screenY
77+
const totalExtraHeight = this.props.extraScrollHeight + this.props.extraHeight
78+
79+
if (Platform.OS === 'ios') {
80+
if (textInputBottomPosition > keyboardPosition - totalExtraHeight) {
81+
this.scrollToFocusedInputWithNodeHandle(currentlyFocusedField)
82+
}
83+
} else {
84+
85+
// on android, the system would scroll the text input just above the keyboard
86+
// so we just neet to scroll the extra height part
87+
if (textInputBottomPosition > keyboardPosition) {
88+
// since the system already scrolled the whole view up
89+
// we should reduce that amount
90+
keyboardSpace = keyboardSpace - (textInputBottomPosition - keyboardPosition)
91+
this.setState({keyboardSpace})
92+
93+
this.scrollForExtraHeightOnAndroid(totalExtraHeight)
94+
} else if (textInputBottomPosition > keyboardPosition - totalExtraHeight) {
95+
this.scrollForExtraHeightOnAndroid(totalExtraHeight - (keyboardPosition - textInputBottomPosition))
96+
}
7797
}
7898
})
7999
}
@@ -108,8 +128,13 @@ const KeyboardAwareMixin = {
108128

109129
componentDidMount: function () {
110130
// Keyboard events
111-
this.keyboardWillShowEvent = Keyboard.addListener('keyboardWillShow', this.updateKeyboardSpace)
112-
this.keyboardWillHideEvent = Keyboard.addListener('keyboardWillHide', this.resetKeyboardSpace)
131+
if (Platform.OS === 'ios') {
132+
this.keyboardWillShowEvent = Keyboard.addListener('keyboardWillShow', this.updateKeyboardSpace)
133+
this.keyboardWillHideEvent = Keyboard.addListener('keyboardWillHide', this.resetKeyboardSpace)
134+
} else if (Platform.OS === 'android' && this.props.enableOnAndroid) {
135+
this.keyboardWillShowEvent = Keyboard.addListener('keyboardDidShow', this.updateKeyboardSpace)
136+
this.keyboardWillHideEvent = Keyboard.addListener('keyboardDidHide', this.resetKeyboardSpace)
137+
}
113138
},
114139

115140
componentWillUnmount: function () {
@@ -131,6 +156,10 @@ const KeyboardAwareMixin = {
131156
responder && responder.scrollResponderScrollToEnd({animated: animated})
132157
},
133158

159+
scrollForExtraHeightOnAndroid(extraHeight: number) {
160+
this.scrollToPosition(0, this.position.y + extraHeight, true)
161+
},
162+
134163
/**
135164
* @param keyboardOpeningTime: takes a different keyboardOpeningTime in consideration.
136165
* @param extraHeight: takes an extra height in consideration.

lib/KeyboardAwareScrollView.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React from 'react'
44
import createReactClass from 'create-react-class'
5-
import { ScrollView } from 'react-native'
5+
import { ScrollView, Platform } from 'react-native'
66
import PropTypes from 'prop-types'
77
import KeyboardAwareMixin from './KeyboardAwareMixin'
88

@@ -14,6 +14,7 @@ const KeyboardAwareScrollView = createReactClass({
1414
x: PropTypes.number,
1515
y: PropTypes.number,
1616
}),
17+
enableOnAndroid: React.PropTypes.bool,
1718
},
1819
mixins: [KeyboardAwareMixin],
1920

@@ -23,6 +24,22 @@ const KeyboardAwareScrollView = createReactClass({
2324
},
2425

2526
render: function () {
27+
const {
28+
enableOnAndroid,
29+
contentContainerStyle,
30+
} = this.props
31+
32+
const {
33+
keyboardSpace,
34+
} = this.state
35+
36+
let newContentContainerStyle
37+
38+
if (Platform.OS === 'android' && enableOnAndroid) {
39+
newContentContainerStyle = {}
40+
newContentContainerStyle.paddingBottom = (contentContainerStyle ? contentContainerStyle.paddingBottom : 0) + keyboardSpace
41+
}
42+
2643
return (
2744
<ScrollView
2845
ref='_rnkasv_keyboardView'
@@ -31,6 +48,7 @@ const KeyboardAwareScrollView = createReactClass({
3148
showsVerticalScrollIndicator={true}
3249
scrollEventThrottle={0}
3350
{...this.props}
51+
contentContainerStyle={newContentContainerStyle || contentContainerStyle}
3452
onScroll={e => {
3553
this.handleOnScroll(e)
3654
this.props.onScroll && this.props.onScroll(e)

0 commit comments

Comments
 (0)