How to change button color when pagerview index changed - react-native

<PagerView
style={styles.viewPager}
initialPage={0}
ref={ref}
orientation="horizontal"
offscreenPageLimit={2}
onPageSelected={index => {
setData({
...data,
viewPosition:index.nativeEvent.position
})
}}>
<View key="1">
<Goods/>
</View>
<View key="2">
<Goods/>
</View>
</PagerView>
<View style={styles.bottomView}>
<TouchableOpacity
style={data.viewPosition == 0 ? styles.btnNormal : styles.btnPress}
onPress={() => ref.current.setPage(0)}>
<Text
style={{
alignSelf: 'center',
color: data.viewPosition == 0 ? '#c38063' : '#aaa',
fontSize: 12,
fontWeight: 'bold',
letterSpacing: 1,
}}>
Goods
</Text>
</TouchableOpacity>
<TouchableOpacity
style={data.viewPosition == 0 ? styles.btnPress : styles.btnNormal}
onPress={() => ref.current.setPage(1)}>
<Text
style={{
alignSelf: 'center',
color: data.viewPosition == 0 ? '#aaa' : '#c38063',
fontSize: 12,
fontWeight: 'bold',
letterSpacing: 1,
}}>
Service
</Text>
</TouchableOpacity>
</View>
I am implementing pagerview in my React application and also I have two buttons at the bottom of the page. When user clicks the button I want to change pagerview, also I want to change the button style.
The above code is working with my expectation but the issue is it's taking some amount of time to change button color when pagerview is changed.
How can I fix the delay of changing button colors?

Related

Huge gap between TouchableOpacity after FlatList

I am trying to create a TouchableOpacity after a FlatList in react native. Although it's coming under the view but there is a huge gap between the list and the button.
Following is the corresponding code :
<FlatList keyExtractor={(payment) => payment.iconFont} style={{ alignSelf: "center" }} data={payments} renderItem={({ item }) => {
return <View style={{ width: '100%', marginTop: 32 }}>
<View style={{ flexDirection: "row" }}>
<View>
<FontAwesome5 name={item.iconFont} size={32} />
</View>
<View>
<Text style={{ fontSize: 18, marginLeft: 16 }}>{item.mainText}</Text>
<Text style={{ fontSize: 12, marginLeft: 12 }}>{item.SubText}</Text>
</View>
</View>
</View>
}} />
<TouchableOpacity style={styles.button}>
<Text style={{ color: "#FFF", fontWeight: "500", fontSize: 20 }}>PAY</Text>
</TouchableOpacity>
I am doing something wrong over here ?
P.S. => Although FlatList is working fine
the corresponding styles :
button: {
backgroundColor: "#000000",
height: 40,
alignItems: "center",
justifyContent: "center",
borderRadius: 20
}
Move the Touchable Opacity into Flatlist's listfootercomponent prop. Like this:
<FlatList
ListFooterComponent={<TouchableOpacity> .... </TouchableOpacity>}
/>
This should solve the spacing problem. And place the TouchableOpacity component always at the bottom of the FlatList.

Swipe row dosent work in react native flatlist

I am using a SwipeRow from 'react-native-swipe-list-view' but when i try to swipe it dose not do anything.I tried to implement it in this way.Tried to call the swipe row function outside the flatlist but doesn't work either. Both method does not work can any one help me.This is the documentation of the library.
<FlatList
data={this.state.data.slice(0, 5)}
maxToRenderPerBatch={6}
renderItem={({ item, index }) => (
<SwipeRow
leftOpenValue={75}
rightOpenValue={-75}>
<View >
<Text >Left</Text>
<Text>Right</Text>
</View>
<CardItem
style={{
paddingLeft: 5,
paddingRight: 5,
paddingBottom: 0,
backgroundColor: this.state.backgroundColor
}}>
<Left>
<Body>
<TouchableOpacity
onPress={() => {
this.props.navigation.navigate('NewsPage', {
newsData: (this.state.data),
url: (item.news_page_url),
title: (item.news_title),
news: (item.mobile_news_summary),
image: (item.image_url),
index: (index),
id: item.id,
newsImage: (item.news_image),
//related_news : (item.related_news)
});
}}
>
<Text
numberOfLines={3}
style={{
fontSize: 18,
fontWeight: "700",
color: this.state.component ? 'white' : '#343434',
fontFamily: 'sans-serif-light'
}}>
{item.news_title}
</Text>
</TouchableOpacity>
<Text
style={{
fontSize: 14,
color: '#B6B6B4',
fontWeight: 'bold',
}}>
{item.news_provider}
</Text>
<Text
style={{
fontSize: 12,
color: '#B6B6B4',
fontWeight: 'bold',
}}>
{item.news_datetime}
</Text>
</Body>
</Left>
</CardItem>
</SwipeRow>
)}
keyExtractor={({ id }, index) => id}
refreshing={this.state.refreshing}
onRefresh={this.handleLoadLess}
/>
They said row that is generally used in a SwipeListView so it's not work if u don't wrap all of them in SwipeListView

ScrollView not showing everything and is cut off

I need this screen to show all the contents that is inside the scrollview, I've tried everything setting ScrollView's flexGrow to 1, parent view's flex to 100. The scrollview is scrolling down until iteration of Bark Central Dog Park & Cafe but it is cut off to half and can't scroll any further, also I noticed that the text HELP ME can not be found too. Thank you so much, as this problem has been bothering me and my progress in react native.
Please tell me what I should do and where I have gone wrong. Thank you again!
NotificationScreen.js
export default class NotificationScreen extends Component
{
static navigationOptions = ({ navigation }) => {
const { state } = navigation;
const {} = state;
return {
header: null,
};
}
render() {
return (
<View style={styles.parentContainer}>
<View style={{flexDirection: 'row', marginLeft: '5%', marginTop: '5%'}}>
<TouchableOpacity onPress={() => this.props.navigation.navigate('JobFeedScreen')}>
<Entypo name="chevron-thin-left" color={Colors.red} size={30} />
</TouchableOpacity>
<View style={{marginLeft: 80,}}>
<Text style={{ fontFamily: 'CoreSansD65Heavy',color: Colors.semiGray, fontSize: 25,}}> Ty, Next</Text>
</View>
<TouchableOpacity onPress={()=> this.props.navigation.navigate('LaunchScreenStack')} style={{marginLeft: 87,marginRight: '5%',}}>
<SimpleLineIcons name='bubbles' color={Colors.red} size={30} />
</TouchableOpacity>
</View>
<View style={{marginLeft: '5%'}}>
<Text style={styles.notificationHeader}> Notifications</Text>
</View>
<ScrollView horizontal={false} overScrollMode={'auto'}>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Anteriore, Inc.</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>The Palace Manila</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Boozy.ph</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Pet warehouse PH</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Bark Central Dog Park & Cafe</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Pet warehouse PH</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Bark Central Dog Park & Cafe</Text>
</TouchableOpacity>
<Text> PLEASE HELP ME </Text>
<Text> PLEASE HELP ME </Text>
<Text> PLEASE HELP ME </Text>
<Text> PLEASE HELP ME </Text>
</ScrollView>
</View>
);
}
NotificationScreenStyles.js
import { StyleSheet } from 'react-native'
import { Metrics, ApplicationStyles, Colors } from '../../Themes/'
export default StyleSheet.create({
...ApplicationStyles.screen,
container: {
paddingBottom: Metrics.baseMargin
},
logo: {
marginTop: Metrics.doubleSection,
height: Metrics.images.logo,
width: Metrics.images.logo,
resizeMode: 'contain'
},
centered: {
alignItems: 'center'
},
parentContainer: {
flex:1,
backgroundColor: Colors.white
},
notificationHeader: {
justifyContent: 'flex-start',
fontFamily: 'CoreSansD45Medium',
marginTop: 40,
marginBottom: 20,
fontSize: 20,
color: Colors.gray,
textAlign: 'left',
},
notifPlace: {
flexGrow: 0,
flexDirection: 'row',
backgroundColor: Colors.white,
width: '100%',
height: '12%',
borderWidth: 0.5,
borderColor: Colors.buttonGray,
},
notifTextPlace: {
width:60,
height:60,
borderRadius: 60/2,
backgroundColor:Colors.buttonGray ,
alignSelf: 'center',
justifyContent: 'flex-start',
marginLeft: '5%',
marginRight: 10,
marginBottom: 20,
marginTop: 20,
},
text: {
fontFamily: 'CoreSansD45Medium',
fontSize: 15,
alignSelf: 'center',
color: Colors.gray,
marginBottom: 10,
},
text2: {
alignSelf: 'center',
fontFamily: 'CoreSansD35Regular',
fontSize: 15,
marginBottom: 10,
},
})
Screenshot 1:
SC1
Screenshot 2: ( it only scrolls up to here )
SC2
This is a known issue in scroll view of react native, use a paddingBottom : 100 in the styles of the scroll view.
The 100 can be set to any value according the content you have
Please provide your StyleSheet and screenshot of what you are seeing.
Test to add
height = Dimensions.get('window').height
to your parentContainer
Also try paddingBottom to your scrollview StyleSheet
Just ran into this problem and what finally fixed it for me was making sure my scrollview wasn't the last component in my layout. So I added an invisible component (with a set height to offset the amount of scrollview content I couldn't see) right after my scrollview.
In my case, I have added a height to the wrapped component of the ScrollView.It worked for me
eg:-
<View style={{ height: hp(500) }}>
<ScrollView showsVerticalScrollIndicator={false}>
{filteredItems.map((item) => <Text style={styles.listItemText}>{item.title}</Text>)}
</ScrollView>
</View>
What fixed it for me was adding contentContainerStyle={{flexGrow: 1,paddingRight:100}} to my scrollview.
Hope it helps someone too!!
just add like this :
<ScrollView contentContainerStyle={{ width:'100%',height:'500%'}}>

position absolute in react native

click to see design
I'm having a problem positioning the white floating button on top of the blue header. The button is a TouchableOpacity inside a View which has Header and a ScrollView. How do I position the button on top of the blue header and scrollview?
<View style={{ flex: 1 }}>
<Header headerText={'Events'} />
<TouchableOpacity
style={styles.addButtonContainerStyle}
onPress={ () => {Actions.addEventMain()} }
>
<Text style={styles.addButtonTextStyle}>+</Text>
</TouchableOpacity>
<EventList />
</View>
Below code would help. Absolute position of views sometimes becomes hard to maintain when code grows. You need to customise below code as per your need.
<View style={{ backgroundColor: "blue", flexDirection: "row-reverse" }}>
<TouchableOpacity style={{ alignSelf: "flex-end" }}>
<Text style={{ justifyContent: "center", fontSize: 20, textAlign: 'center' }}>
+
</Text>
</TouchableOpacity >
<View style={{ flex: 1 }}>
<Text style={{ alignSelf: "center", fontSize: 20, textAlign: 'center' }}>
Event
</Text>
</View>
</View >

Any touch events not trigger when the TextInput is on focus

I have search input with list of results. I am trying to select the one item from the list. But I am not able to select the item on first press as the input field is on focus. First press is considered to close the keyboard. I want to trigger the touch event on first press with closing the keyboard. My code is given below-
<TextInput
placeholder='Destination'
underlineColorAndroid={'transparent'} style={[styles.input]}
value={this.state.destination}
onChangeText={(text) => this.saveDestination(text)}
/>
<View style={{ backgroundColor: WHITE, marginVertical:
(this.state.predictions.length === 0) ? 0 : 15 }}>
{this.destinationPredictation()}
</View>
destinationPredictation() {
const arr = this.state.predictions;
return arr.map((d, index) => {
return (
<TouchableOpacity
onPress={() => { this.setState({ destination: d.description });
}}
style={[{ flex: 1, paddingHorizontal: 5, paddingTop: 10,
paddingBottom: (index === this.state.predictions.length - 1)
? 10 : 0 }]}
key={d.id}
>
<View style={{ width: (WIDTH - 50), paddingHorizontal: 0,
flexDirection: 'row', alignItems: 'center' }}>
<Icon name='map-marker' size={30} color={REGULAR_OPTION}
style={{ marginHorizontal: 8 }} />
<Text style={[styles.destinationOptions, { flex: 1 }]}>
{d.description}
</Text>
</View>
</TouchableOpacity>
);
});
}