Grid View With different Hight and width in react Native - react-native

enter image description here
Can anyone give idea how I can create this design in React-Native
here is the design image

Your array -
[item1, item2, item3, ....]
Modify it as -
[
{id, type:1, data:[item1, item2, item3] },
{id, type:2, data:[item4, item5, item6] },
{id, type:3, data:[item7, item8, item9] },
{id, type:1, data:[item10, item11, item12] }
{id, type:2, data:[item13, item14, item15] }
...
...
]
Like, Each item will have a type field and array of 3 items..
Use this data inside flatlist or scrollview and you can classify it according to the type(1,2,3) field.
If type == 1
return below component.
If type == 2
return
If type == 3
return
So the final result will look like what you are expecting..!
It's just an idea! Try to implement.
For better performance you can build data in this format before sending from the backend.(if the data is coming as part of any APIs)
data format function
const modifyData = (arr) => {
let finalData = [];
for (let i=0; i < arr.length; i+=3){
let j = 0;
let data = []
while(j<3){
arr[i+j] && data.push(arr[i+j]);
j+=1;
}
finalData.push({id: Math.random().toString(), type: (i/3)%3 + 1, data})
}
return finalData;
}
use some random id generator for id (crypto or Math.random().toString())

I found a similar thread hope this helps
Grid layout with different height items (React Native)
The markup will look like this:
<View style={styles.wrapper}>
<ScrollView contentContainerStyle={styles.container}>
<ListView
contentContainerStyle={styles.list}
dataSource={this.state.dataSourceColumn1}
/>
<ListView
contentContainerStyle={styles.list}
dataSource={this.state.dataSourceColumn2}
/>
<ListView
contentContainerStyle={styles.list}
dataSource={this.state.dataSourceColumn3}
/>
</ScrollView>
</View>
Wrapper View will help you stretch the block to fit the entire area. From the styling point of view you can resolve the task with flexbox:
wrapper: {
flex: 1
},
container: {
flexDirection: 'row',
paddingHorizontal: 5
},
list: {
flex: 1,
flexDirection: 'column',
paddingVertical: 10,
paddingHorizontal: 5
}
The trick is that we treat every column as a row inside the container. Play around with paddings and alignItems styling to achieve consistent spacing.
Note that the handy onEndReached that comes with the ListView won't be available here, so you'll have to catch the user reaching the end of the ScrollView in order to know when new fetch is required. But that's a different question which I believe has already been answered elsewhere.
In case the grid is finite and you don't need to throw more items into it, things are simpler. Just split the data the way described above and use 3 View-s with nested items instead of the ListView-s.
Now you can edit as per your requirements and easily find the way through this.

Related

How interpolate works? REACT NATIVE ANIMATED

I kind of new using animation, im using react native reanimated to create some animations, but i really dont understand how interpolate works, the input range, output range, the animation that i want its inside a scrollview, each item should has its own Animated.View, so the first one will be 100% width and the rest will be like 20%, i want to create this design:
for now my code is this:
const animatedWidth = useAnimatedStyle(() => {
const width_ = interpolate(
cardWidth,
[0,1, 2],
[4, 4, 5],
);
return {
width: width_,
};
});
return (
<>
<Animated.ScrollView horizontal = {true}>
{
extras.map((extra) => {
return (
<>
<Animated.View style = {[{height: 220, margin: 5}, animatedWidth]}>
<ImageBackground
source = {require("../assets/images/hawai.jpeg")}
style = {{width: "100%", height: "100%"}}
imageStyle = {{borderRadius: 30
}}>
[![</Animated.View][2]][2]>
)}
}
<Animated.ScrollView>
)
and this is the result:
i can scroll and it "works" and i want to make it work like the first design, the first full width and the others 20% width, but i really dont understand the interpol and the inputRange to make them appears, if someone knows and can explain to me in simple works really greateful, searched for videos and documentation but like i said still dont understand, thanks :)

React-native - How to create a scrollable flatList in which the chosen item is the one at the middle?

I would like to create a scrollable FlatList to select only one item among a list. After the user scroll the list, the selected item will be the one in the colored rectangle (which have a fixed position) as you can see here :
Actually I'm only able to render a basic FlatList even after some researches.
Do you know how I should do that ?
I found the solution (but it's not a FlatList) !
To do that I use :
https://github.com/veizz/react-native-picker-scrollview.
To define the background of the current selected items I added a new props highLightBackgroundColor in the ScrollPicker Class in the index file of react-native-picker-scrollview :
render(){
...
let highLightBackgroundColor = this.props.highLightBackgroundColor || '#FFFFFF';
...
let highlightStyle = {
...
backgroundColor: highLightBackgroundColor,
};
...
How to use it :
<ScrollPicker
ref={sp => {
this.sp = sp;
}}
dataSource={['a', 'b', 'c', 'd', 'e']}
selectedIndex={0}
itemHeight={50}
wrapperHeight={250}
highLightBackgroundColor={'lightgreen'}
renderItem={(data, index, isSelected) => {
return (
<View>
<Text>{data}</Text>
</View>
);
}}
onValueChange={(data, selectedIndex) => {
//
}}
/>
How it looks without others customizations:
You can implement the same setup with the very popular react-native-snap-carousel package, using the vertical prop. No need to use a smaller, poorly documented/unmaintained package for this.

React Native for loop with Array not doing what I expected

I'm trying out some things in React Native for the first time and i'm trying to roll 3 dices (text based for now).
I'm using a for loop to go over an array of the 3 dices. However i'm only seeing one dice text being updated (the 3rd one).
Also when doing some alerts to check what's going on within that for loop, i'm seeing unexpected things? the first alert says 2, the second alert says 1 and then it usually no longer alerts, seldom it also alerts a third time with a 0.
My code so far:
(file: Game.js)
import React from 'react'
import { StyleSheet, Image, Text, View, Button } from 'react-native'
export default class Home extends React.Component {
state = {
dices: [null, null, null],
rollsLeft: 3,
keepDices: [false, false, false],
}
//When this component is mounted (loaded), do some defaults
componentDidMount() {
}
//Roll dices
rollDices = () => {
for(let i = 0; i < 3; i++){
alert('for loop at ' + i);
//Math random to get random number from rolling the dice
randomNumber = Math.floor(Math.random() * 6) + 1;
//Check if the user wanted to keep a dice's value before reassigning a new value
if(this.state.keepDices[i] === false){
//User want to roll this dice, assign new random number to it
//this.setState.dices[i] = randomNumber;
let newDices = [ ...this.state.dices ];
newDices[i] = randomNumber;
this.setState({ dices : newDices });
}
}
//Deduct 1 roll from total
this.setState.rollsLeft--;
//TODO: Check if rolls equals 0, then make player2 active!
}
render() {
return (
<View style={styles.container}>
<Text> {this.state.dices[0]} ONE </Text>
<Text> {this.state.dices[1]} TWO</Text>
<Text> {this.state.dices[2]} THREE</Text>
<Text>Turns left: {this.state.rollsLeft} </Text>
<Button
title="Roll 🎲"
onPress={this.rollDices} />
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
},
})
In React Native the setState function is asynchronous.
Meaning that this.setState({ dices : newDices }); can end up setting dices to different values depending on which finishes first.
If you want to control what happens after you use setState, you can call a function after the set is done like this
this.setState({dices: newDices}, () => {
// Do something here.
});
There is some really useful information on calling function after the setState here: Why is setState in reactjs Async instead of Sync?
and some good explanations of how setState in react works and how to get around it here: https://medium.com/#wereHamster/beware-react-setstate-is-asynchronous-ce87ef1a9cf3
Together with DCQ's valuable input for async setStates bundling I also noticed that i'm always resetting the copied dice array within the for loop and thus only saving my last dice correctly.
Next up the for loop was actually counting right from 0 to 2 however the alert boxes don't interrupt the code as i'm used to in the browser therefore it looked a bit off. When doing console.log (which is also cleaner and more correct logging) I noticed things did went right there.

How to render absoulute element inside ListView or FlatList?

Here's a fun one i've been poking at for while:
I have a FlatList (same issue with ListView) and I want to render an element INSIDE the internal scrolling container with the following characteristics:
Absolutely Positioned (thus having no effect on position of list elements)
Position XX distance from top (translateY or top)
zIndex (above list elements)
The use case is i'm rendering a day view calendar grid with a horizontal bar at the current time position fixed at X distance from the beginning of the internal scrollview so it appears as the user scrolls pass that position.
So far i've tried wrapping wrapping FlatList/ListView with another ScrollView... also tried rendering this element as the header element which only works while the header/footer are visible (trashed when out of view).
Any and all ideas welcomed. :)
Thanks
Screenshot Below (red bar is what i'm trying to render):
Here's a working demo of what it sounds like you're trying to achieve: https://sketch.expo.io/BkreW1che. You can click "preview" to see it in your browser.
And here's the main code you need to measure the height of the ListView and place the indicator on top of it (visit the link above to see the full source):
handleLayout(event) {
const { y, height } = event.nativeEvent.layout;
// Now we know how tall the ListView is; let's put the indicator in the middle.
this.setState({ indicatorOffset: y + (height / 2) });
}
renderIndicator() {
const { indicatorOffset } = this.state;
// Once we know how tall the ListView is, put the indicator on top.
return indicatorOffset ? (
<View style={[{ position: 'absolute', left: 0, right: 0, top: indicatorOffset }]} />
) : null;
}
render() {
return (
<View style={styles.container}>
<ListView
onLayout={(event) => this.handleLayout(event)}
dataSource={this.state.dataSource}
renderRow={this.renderRow}
/>
{this.renderIndicator()}
</View>
);
}
Edit: I now understand that you want the indicator to scroll along with the list. That's a simple change from above, just add an onScroll listener to the ListView: https://sketch.expo.io/HkEjDy92e
handleScroll(event) {
const { y } = event.nativeEvent.contentOffset;
// Keep the indicator at the same position in the list using this offset.
this.setState({ scrollOffset: y });
},
With this change, the indicator actually seems to lag behind a bit because of the delay in the onScroll callback.
If you want better performance, you might consider rendering the indicator as part of your renderRow method instead. For example, if you know the indicator should appear at 10:30 am, then you would render it right in the middle of your 10am row.

How do i fill and remove an item into an array depending on its state?

i am developing an React Native Android App.
I am receiving data (id and name) from my API. Now i am using a ListView with MKIconToggle (react-native-material-kit) to display my list data.
With MKIconToggle i can give my displayed items two different states (clicked = color is black / unclicked = grey). Now i want to send the list of clicked items back to my server. But i just can´t figure out how i put the clicked items for example into an array or something and only send clicked items to server.
My RenderMethod with the ListView looks like this:
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderList}
horizontal={true}
renderHeader={this.renderHeader}
/>
RenderList:
<View
style={{justifyContent: 'flex-start', alignItems: 'center', padding: 10, flexDirection: 'column'}}>
<MKIconToggle
checked={this.state.initialChecked}
onCheckedChange={()=>this._onIconChecked(data)}
onPress={this._onIconClicked}
style={{flexDirection: 'row', justifyContent: 'center', alignItems: 'center'}}>
<Text state_checked={this.state.checkedState}
pointerEvents="none"
style={styles.titleChecked}
numberOfLines={3}>{data.name}</Text>
<Text pointerEvents="none"
style={styles.title}
numberOfLines={3}>{data.name}</Text>
</MKIconToggle>
</View>
Now i should handle my clicked Items in _onIconChecked:
_onIconChecked: function (data) {
// Put data.id into an array, if it way clicked (state is true)
// If date.id is unclicked again, then remove from array
},
I hope i could explain my issue clearly, otherwise just let me know. I am new in programming and writing stackoverflow issues/questions, so please give me hints if i made something wrong.
From your information I will have to make a few assumptions in order to be able to answer your question.
For now I will assume that that checked handler properly returns the 'id' of an item that is the same id as in your array of items.
So assuming your array is:
[{name: 'Luke', id: 1'}, {name: 'Darth', id: 2'}]
If I were to click on 'Luke' _onIconChecked would receive a data object that at least has id: 1 in it.
The second assumption is that you have an array somewhere where you can store those clicked items. I would just put that outside of your component seeing as MK would already take care of properly rendering a checked item. So:
var _checkedItems = []
var myComponent = React.create...
The last assumption is that the data object passed to _onIconChecked also contains information on the state of the checkbox, so date.checked is either true or false.
The precise implementation might be different for all these items, but this is what I can work off of.
Now what you could do is:
_onIconChecked: function (data) {
var id = data.id;
var checked = data.checked;
var currentIndex = _checkedItems.indexOf(id)
if(checked) {
if(currentIndex == -1) { _checkedItems.push(id) }
// the 'else' would mean the item is already in the array, so no need to add
} else {
if(currentIndex > -1) {
// This means that the item is in the array, so lets remove it:
_checkedItems.splice(currentIndex, 1) // This removes the id from the array.
}
}
}
What you'd do now to only get the items from your this.state.items array that have their ids in the checked array:
getCheckedItems: function() {
return this.state.items.map(function(item) {
if(_checkedItems.indexOf(item.id) > -1){
return item
}
})
}
I am not sure about your setup so I made lots of assumptions and probably over engineered some things, but it might get you going in the right direction.