Horizontal scrollview inside another horizontalscroll view in react-native - reactjs

I'm designing UI in react native where I've a HorizontalScrollView inside another HorizontalScrollView. However, the child HorizontalScrollView doesn't work.
Is it possible to disable the parent HorizontalScrollView whenever a gesture is made in child HorizontalScrollView. I know this sounds tricky but I need to implement this in my UI.
note: working fine in IOS
code snippet:
import React from 'react';
import { StyleSheet, Text, View, Dimensions, SafeAreaView, TouchableHighlight, ScrollView,
TouchableOpacity, TouchableWithoutFeedback, Image, Linking, Platform } from 'react-native';
import Carousel, { Pagination } from 'react-native-snap-carousel';
import { Input, Button, Divider, Card, Overlay } from 'react-native-elements';
export default class Cart extends React.Component {
state = {
data: [
{ title: 'a', url: require("./Light-Hover34x34.png") },
{ title: 'b', url: require("./Light-Hover34x34.png") },
{ title: 'c', url: require("./Light-Hover34x34.png") },
{ title: 'd', url: require("./Light-Hover34x34.png") },
{ title: 'e', url: require("./Light-Hover34x34.png") },
],
activeSlide: 0,
room: ["one", "two", "three"]
}
_renderItem = ({ item, index }) => {
console.log("active slide", this.state.activeSlide)
const isActive = index === this.state.activeSlide;
return (
<TouchableHighlight
// onPress={() => Linking.openURL(item.url)}
style={{
backgroundColor: isActive ? '#FFD845' : null,
width: 60,
height: 60,
borderRadius: 60/2,
justifyContent: 'center',
alignItems: 'center',
}}>
<Image
source={item.url}
style={{ width: '50%', height: '40%' }}
/>
</TouchableHighlight>
);
};
render() {
return (
<View style={{alignItems: 'center', justifyContent: 'center', paddingVertical: 100}}>
<ScrollView horizontal={true}>
{
this.state.room.map((r, i) => {
return(
<View key={i}>
<TouchableWithoutFeedback>
<Card containerStyle={[styles.cardEleBig,]}>
<SafeAreaView style={{ height: 150 }}>
<Carousel
data={this.state.data}
renderItem={this._renderItem}
sliderWidth={120}
itemWidth={50}
onSnapToItem={index => this.setState({ activeSlide: index })} //for pagination
/>
</SafeAreaView>
</Card>
</TouchableWithoutFeedback>
</View>
)
})
}
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
cardCircleParking: {
backgroundColor: "yellow",
// marginBottom: 10,
// marginLeft: '5%',
width: 50,
height: 50,
borderRadius: 50/2,
borderColor: 'white',
},
cardEleBig: {
borderRadius: 20,
borderBottomWidth: 0,
borderColor: 'white',
width: 159,
height: Platform == "ios" ? 210 : 200,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 3.84,
elevation: 5,
marginLeft: 5,
marginRight: 5,
marginTop: 10,
marginBottom: 2
},
})
But this code is working fine with IOS
Thanks in advance

Related

React Native TouchableHighlight onFocus onBlur

I am creating a menu for an Android TV, the Menu expands when it get focus, but when moving from one menu item to the other I get a flicker because I can't find a way to make the menu focus independent from the menu item focus.
I've tried to use a Touchable around the Flat list but then i cant move focus to the items.
import { FlatList, StyleSheet, Text, TouchableHighlight, View } from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import { MaterialIcons } from '#expo/vector-icons';
import React from 'react';
interface MenuListOption {
name: string;
icon: string;
}
interface MenuItemProps {
setMenuFocus: (v: boolean) => void;
item: MenuListOption;
menuFocus: boolean;
}
export const homeLists: MenuListOption[] = [
{
name: 'HOME',
icon: 'home-filled',
},
{
name: 'MOVIES',
icon: 'local-movies',
},
{
name: 'TV SHOWS',
icon: 'movie',
},
];
const MenuItem = ({ setMenuFocus, item, menuFocus }: MenuItemProps): JSX.Element => {
const [focus, setFocus] = React.useState(false);
const onFocus = React.useCallback(() => {
setFocus(true);
setMenuFocus(true);
}, [setMenuFocus]);
const onBlur = React.useCallback(() => {
setFocus(false);
setMenuFocus(false);
}, [setMenuFocus]);
return (
<TouchableHighlight onFocus={onFocus} onBlur={onBlur} style={[styles.item, focus ? styles.itemFocused : null]}>
<View style={styles.itemWrapper}>
<MaterialIcons name={item.icon} size={50} color="red" />
{menuFocus && <Text style={styles.text}>{item.name}</Text>}
</View>
</TouchableHighlight>
);
};
const Menu = (): JSX.Element => {
const [focus, setFocus] = React.useState(false);
const colorsArray = focus ? ['gray', 'gray', 'transparent'] : ['gray', 'gray'];
const renderItem = ({ item }: { item: MenuListOption }) => (
<MenuItem setMenuFocus={setFocus} item={item} menuFocus={focus} />
);
return (
<View style={[styles.wrapper, focus ? styles.wrapperFocused : null]}>
<LinearGradient colors={colorsArray} start={{ x: 0, y: 0.9 }} end={{ x: 1, y: 0.9 }}>
<View style={focus ? styles.logoFocus : styles.logo}>
{focus && <MaterialIcons style={{ paddingRight: 20 }} name={'tv'} size={40} color={'white'} />}
<Text style={[styles.title, focus && styles.textFocus]}>MyApp</Text>
</View>
<FlatList
contentContainerStyle={{ justifyContent: 'center', flex: 1 }}
style={styles.list}
data={homeLists}
renderItem={renderItem}
keyExtractor={(item) => String(item.name)}
/>
</LinearGradient>
</View>
);
};
const styles = StyleSheet.create({
wrapper: {
height: '100%',
position: 'absolute',
top: 0,
zIndex: 1,
left: -200,
transform: [{ translateX: 200 }],
},
title: {
fontSize: 15,
lineHeight: 38,
fontWeight: '400',
textAlign: 'left',
},
wrapperFocused: {
width: 900,
},
logo: {
justifyContent: 'center',
flexDirection: 'row',
marginTop: 20,
},
list: {
flexGrow: 0,
height: '100%',
padding: 10,
},
logoFocus: {
justifyContent: 'flex-start',
flexDirection: 'row',
marginTop: 60,
marginLeft: 20,
},
textFocus: {
fontSize: 35,
},
item: {
maxWidth: 250,
marginBottom: 40,
alignSelf: 'stretch',
left: 0,
padding: 10,
},
itemFocused: {
borderBottomColor: 'yellow',
borderBottomWidth: 5,
},
itemWrapper: {
maxWidth: 250,
flexDirection: 'row',
},
text: {
color: 'white',
fontSize: 28,
lineHeight: 41,
fontWeight: '600',
marginLeft: 50,
marginTop: 5,
},
});
export default Menu;
Here is an animation of the problem:
menu

How can i display the following output in react native snap carousel

How can i display the following output in react native snap carousel
<Carousel
autoplayInterval={4000}
// slideStyle={{ flex: 1,
// marginRight:10}}
ref={ (c) => { this._carousel = c; } }
data={this.state.featuredBusinessData}
scrollInterpolator={stackScrollInterpolator}
slideInterpolatedStyle={stackAnimatedStyles}
useScrollView={true}
renderItem={this._renderItemFeaturedBusiness.bind(this)}
onSnapToItem={this.handleSnapToItem.bind(this)}
sliderWidth={deviceWidth}
// itemWidth={deviceWidth/2.5 }
itemWidth={deviceWidth/3 }
layout={'default'}
// loopClonesPerSide={this.state.videos.length-1}
firstItem={2}
autoplay={true}
loop={true}
useScrollView={true}
enableMomentum={true}
lockScrollWhileSnapping={false}
/>
as I'm new to RN can please anybody tell me how can i display the following output in react native snap carousel
Output:
#Waheed Akhtar gave you the perfect suggestion, but if you still have some difficulties in implementation here is the full working example:
import * as React from 'react';
import { Text, View, StyleSheet, FlatList, Image } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
export default function App() {
const videos = [
{
id: 'WpIAc9by5iU',
thumbnail: 'https://source.unsplash.com/random',
title: 'Led Zeppelin - Stairway To Heaven',
},
{
id: 'sNPnbI1arSE',
thumbnail: 'https://img.youtube.com/vi/sNPnbI1arSE/hqdefault.jpg',
title: 'Eminem - My Name Is',
},
{
id: 'VOgFZfRVaww',
thumbnail: 'https://img.youtube.com/vi/VOgFZfRVaww/hqdefault.jpg',
title: 'some title',
},{
id: 'WpIAc9by5iU',
thumbnail: 'https://source.unsplash.com/random',
title: 'Led Zeppelin - Stairway To Heaven',
},
{
id: 'sNPnbI1arSE',
thumbnail: 'https://source.unsplash.com/random',
title: 'Eminem - My Name Is',
},
{
id: 'VOgFZfRVaww',
thumbnail: 'https://img.youtube.com/vi/VOgFZfRVaww/hqdefault.jpg',
title: 'some title',
},
];
return (
<View style={styles.container}>
<FlatList
horizontal
data={videos}
renderItem={({ item }) => (
<View style={styles.card_template}>
<Image
style={styles.card_image}
source={{
uri:
item.thumbnail,
}}
/>
<View style={styles.text_container}>
<Text style={styles.card_title}>{item.title.toUpperCase().substr(0, 10)+"..."}</Text>
<Text style={styles.card_subtitle}>{item.id}</Text>
</View>
</View>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
card_template: {
width: 150,
height: 150,
marginLeft: 10,
marginTop: 20,
boxShadow: '20px 20px 17px -12px rgba(0,0,0,0.75)',
borderRadius: 10,
},
card_image: {
width: 150,
height: 150,
borderRadius: 10,
},
text_container: {
position: 'absolute',
width: 150,
height: 50,
bottom: 0,
paddingVertical: 2,
backgroundColor: 'rgba(255,255,255, 1)',
borderBottomLeftRadius: 10,
borderBottomRightRadius: 10,
},
card_title: {
color: 'black',
fontSize: 14,
marginLeft: 10,
},
card_subtitle:{
color: 'grey',
fontSize: 14,
marginLeft: 10,
}
});
You can play with working code here: Expo Snack
Use Scrollview or Flatlist to render the above ListItems with horizontal={true} props
https://reactnative.dev/docs/scrollview#horizontal

How to move down a object using button in react native?

Actually I want to do something like that (Image attached):
There have a box, and two buttons. If I press button 1 then the box moved left. And if I press button 2 then the box moved right.
I want, when the box move right and overcome the bar 1 then the box moved down on the bar 2.
But I have no idea how to do it.
Here is my code:
import React, {Component} from 'react';
import {
View,
Text,
StyleSheet,
Animated,
TouchableOpacity,
ScrollView,
Image,
} from 'react-native';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
left: 20,
};
}
moveRight = () => {
this.setState({left: this.state.left + 10}); // 10 is value of change.
};
moveLeft = () => {
this.setState({left: this.state.left - 10}); // 10 is value of change.
};
render() {
return (
<View style={styles.container}>
<Image
style={{left: this.state.left, height: 120, width: 120}}
source={require('./assets/box.png')}
/>
<Image
style={{height: 20, width: 180, marginTop: -12, marginLeft: 25}}
source={require('./assets/log.png')}
/>
<Image
style={{height: 20, width: 200, marginTop: 150, marginLeft: 185}}
source={require('./assets/log.png')}
/>
<View style={styles.buttonsContainer}>
<TouchableOpacity onPress={this.moveLeft}>
<Image
style={{height: 60, width: 60}}
source={require('./assets/left-button.png')}
/>
</TouchableOpacity>
<TouchableOpacity onPress={this.moveRight}>
<Image
style={{height: 60, width: 60}}
source={require('./assets/right-button.png')}
/>
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
textCenter: {
alignSelf: 'center',
textAlign: 'center',
},
levelHeading: {
fontWeight: 'bold',
fontSize: 35,
color: '#CC8A4F',
},
buttonsContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginTop: 200,
paddingLeft: 80,
paddingRight: 80,
},
});
Please help me!
Thanks!
import React, { useEffect } from "react";
import { Animated, Text, View, StyleSheet, Button } from "react-native";
const App = () => {
const moveAnimation = new Animated.ValueXY({ x: 10, y: 450 });
useEffect(() => {
moveAnimation;
}, []);
const _moveBallLeft = () => {
Animated.spring(moveAnimation, {
toValue: { x: 250, y: 450 },
}).start();
};
const _moveBallRight = () => {
Animated.spring(moveAnimation, {
toValue: { x: 10, y: 450 },
}).start();
};
return (
<View style={styles.container}>
<Animated.View style={[styles.tennisBall, moveAnimation.getLayout()]}>
<View style={styles.button}>
<Text style={styles.buttonText}>ball</Text>
</View>
</Animated.View>
<View
style={{
flexDirection: "row-reverse",
justifyContent: "space-evenly",
}}
>
<View style={{ alignSelf: "center" }}>
<Button title=">" onPress={_moveBallLeft}></Button>
</View>
<View style={{ alignSelf: "center", marginLeft: "2%" }}>
<Button title="<" onPress={_moveBallRight}></Button>
</View>
</View>
</View>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 2,
backgroundColor: "#ecf0f1",
},
tennisBall: {
display: "flex",
justifyContent: "center",
alignItems: "center",
backgroundColor: "greenyellow",
borderRadius: 100,
width: 100,
height: 100,
},
button: {
paddingTop: 24,
paddingBottom: 24,
},
buttonText: {
fontSize: 24,
color: "#333",
},
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

How can we create Side Tabs in React Native

Creating Side Menu in React Application.
This would work both with Expo and react
Create a Navigation Service(Navigation-service.ts)
import { NavigationActions } from 'react-navigation';
function navigate(routeName: any, params?: any) {
NavigationActions.navigate({
routeName,
params
});
setRoute(routeName);
}
export default {
navigate
}
After this
create a new file sidemnu.tsx
import NavigationService from Navigation-service.ts;
import { View, TouchableHighlight, Image } from 'react-native';
openHome() {
NavigationService.navigate('Home');
}
render(){
return (
<View style={[this.props.isLoggedIn ? styles.container : styles.containerHidden]}>
<View style={[this.props.isLoggedIn ? styles.tabContainer : styles.tabContainerHidden]}>
<View>
<TouchableHighlight
style={this.props.currentTab === 'Home' ? styles.activeTab : styles.menuBtnContainer}
onPress={() => this.openHome()}
disabled={!this.props.isLoggedIn}
underlayColor='red'
>
<Image
source='any image url'
/>
</TouchableHighlight>
</View>
</View>
</View>
);
}
Add style
import { StyleSheet } from 'react-native';
const styles = StyleSheet.create({
container: {
width: 108,
backgroundColor: '#002A5C',
borderTopColor: '#002457',
borderWidth: 1
},
containerHidden: {
width: 108,
backgroundColor: '#002A5C'
},
tabContainer: {
flex: 1,
flexDirection: 'column'
},
tabContainerHidden: {
flex: 1,
display: 'none'
},
activeTab: {
backgroundColor: '#008cc3',
height: 108,
width: 108,
padding: 10,
alignSelf: 'center',
justifyContent: 'center'
},
menuBtnContainer: {
height: 80,
width: 80,
alignSelf: 'center',
justifyContent: 'center'
},
});
Call in app.tsx file
<Sidemnu />
Whenever you want to display this side menu.
If you have any queries, please write back.

Why is extended stylesheet not rendering?

Brand new project. react-native-extended-stylesheet installed. everything working fine, and then out of nowhere it stops working. here's the full component:
import React from 'react';
import { Image, FlatList, View, Text, StyleSheet, SafeAreaView, TouchableOpacity, Dimensions } from 'react-native';
import EStyleSheet from 'react-native-extended-stylesheet';
const { height, width } = Dimensions.get('window');
const data = [
{
title: 'Johnny Appleseed',
subtitle: 'November 12, 2019',
img: require('./src/assets/img/johnny_appleseed.png'),
type: 'video'
},
{
title: 'Johnathan Doe',
subtitle: 'November 19, 2019',
img: require('./src/assets/img/johnny_appleseed.png'),
type: 'voice'
}
];
const AgendaItem = (props) => {
return (
<TouchableOpacity onPress={props.onPress}>
<View style={styles.container}>
<View style={styles.leftContainer}>
<Image
resizeMethod={'scale'}
source={
props.type === 'voice' ? (
require('./src/assets/img/voice_icon.png')
) : (
require('./src/assets/img/video_icon.png')
)
}
style={styles.icon}
/>
<View style={styles.textContainer}>
<Text style={styles.title}>{props.title}</Text>
<Text style={styles.subtitle}>{props.subtitle}</Text>
</View>
</View>
<Image source={props.img} style={styles.img} />
</View>
</TouchableOpacity>
);
};
const ProfileList = () => {
return (
<SafeAreaView>
<FlatList
data={data}
renderItem={({ item }) => (
<AgendaItem title={item.title} subtitle={item.subtitle} img={item.img} type={item.type} />
)}
keyExtractor={(item) => item.id}
/>
</SafeAreaView>
);
};
const styles = EStyleSheet.create({
$rem: width > 380 ? 18 : 16,
container: {
flexDirection: 'row',
backgroundColor: '#fff',
borderRadius: 25,
elevation: 3,
marginBottom: 5,
width: '22rem',
justifyContent: 'space-between',
marginHorizontal: '0.5rem',
paddingVertical: '.75rem',
paddingHorizontal: '1.5rem'
},
textContainer: {
justifyContent: 'space-around'
},
leftContainer: {
flexDirection: 'row',
alignItems: 'center'
},
title: {
fontWeight: 'bold',
fontSize: 20,
color: '#454A66'
},
subtitle: {
color: '#454A66',
fontSize: 12
},
img: {
height: '3rem',
width: '3rem',
borderWidth: 1,
borderColor: '#169C75',
borderRadius: 30
},
icon: {
height: '1.5rem',
width: '1.5rem'
}
});
export default ProfileList;
I was just messing with some of the properties and all of a sudden it stopped rendering any style in the styles object. ive tried closing/opening the project. ive tried rebooting system. this just seems buggy because its happened before.
any suggestions or something I missed?

Resources