Coder Social home page Coder Social logo

carouselview's People

Contributors

usaidather avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

carouselview's Issues

flatlist

this.flatList.scrollToOffset({ animated: true, offset: scrollValue })

Problems with dynamic data obtained by axios

Hello, in advance thank you very much for the publication of your code, it has helped me a lot, however I present a problem when the data is dynamic. I'm getting the data from an API but this presents me with the following problem:

Scroll to the next image but always return to the first image, jump to the second image, then return to the first image, then jump to the third image and then jump to the first image, and then jump to the fourth image, it is not scrolling from the first to the second and then the third and fourth, before passing, you are in the first, do you know why I am getting this behavior?

I share only the file where I am obtaining the data, since the rest is equal to its code.

I hope you can help me.

HomeScreen.js

import React, { useEffect, useState } from "react";
import { View } from "react-native";
import CategoriesScreen from "./Categories/CategoriesScreen";
import { ScrollView } from "react-native-gesture-handler";
import Carousel from "./Banner/BannerOficial";
import { axiosClient } from "../../config/axios";

export default function HomeScreen({ navigation }) {


  const [banners, setBanners] = useState([]);

  useEffect(() => {
    getBannersAPI();
  }, []);

  function getBannersAPI(){
    axiosClient
    .get("/service/banner_available")
    .then(async function (response) {
      setBanners(response.data);
    })
    .catch(function (error) {
      console.log("Error cargando los banners: ", error);
    });
  }
  
  return (
    <View style={{ flex: 1 }}>
      <ScrollView>
        <Carousel data={banners} />
        <CategoriesScreen navigation={navigation} />
      </ScrollView>
    </View>
  );
}

BannerOficial.js

import React, { useState, useEffect } from 'react'
import { View, Text, StyleSheet, Dimensions, FlatList, Animated } from 'react-native'
import CarouselItem from './BannerItem'


const { width, heigth } = Dimensions.get('window')
let flatList

function infiniteScroll(dataList){
    const numberOfData = dataList.length
    let scrollValue = 0, scrolled = 0

    setInterval(function() {
        scrolled ++
        if(scrolled < numberOfData)
        scrollValue = scrollValue + width

        else{
            scrollValue = 0
            scrolled = 0
        }

        this.flatList.scrollToOffset({ animated: true, offset: scrollValue})
        
    }, 3000)
}


const Carousel = ({ data }) => {
    const scrollX = new Animated.Value(0)
    let position = Animated.divide(scrollX, width)
    const [dataList, setDataList] = useState(data)

    useEffect(()=> {
        setDataList(data)
        infiniteScroll(dataList)
    })


    if (data && data.length) {
        return (
            <View>
                <FlatList data={data}
                ref = {(flatList) => {this.flatList = flatList}}
                    keyExtractor={(item, index) => 'key' + index}
                    horizontal
                    pagingEnabled
                    scrollEnabled
                    snapToAlignment="center"
                    scrollEventThrottle={16}
                    decelerationRate={"fast"}
                    showsHorizontalScrollIndicator={false}
                    renderItem={({ item }) => {
                        return <CarouselItem item={item} />
                    }}
                    onScroll={Animated.event(
                        [{ nativeEvent: { contentOffset: { x: scrollX } } }]
                    )}
                />

                <View style={styles.dotView}>
                    {data.map((_, i) => {
                        let opacity = position.interpolate({
                            inputRange: [i - 1, i, i + 1],
                            outputRange: [0.3, 1, 0.3],
                            extrapolate: 'clamp'
                        })
                        return (
                            <Animated.View
                                key={i}
                                style={{ opacity, height: 10, width: 10, backgroundColor: '#595959', margin: 8, borderRadius: 5 }}
                            />
                        )
                    })}

                </View>
            </View>
        )
    }

    console.log('Please provide Images')
    return null
}

const styles = StyleSheet.create({
    dotView: { flexDirection: 'row', justifyContent: 'center'}
})

export default Carousel

BannerItem.js

import React from "react";
import { View, StyleSheet, Text, Image, Dimensions} from 'react-native';

const { width, height} = Dimensions.get('window')

const CarouselItem = ({item}) => {
    return(
        <View style={styles.cardView}>
            <Image style={styles.image} source = {{ uri: item.imagePath}}/>
        </View>
    )
}

const styles = StyleSheet.create({
    cardView:{
        flex:1,
        width: width -20,
        height: height / 7,
        backgroundColor: "white",
        margin: 10,
        borderRadius: 10,
        shadowColor: "#000",
        shadowOffset: {width: 0.5, height: 0.5},
        shadowOpacity: 0.5, 
        shadowRadius: 3,
        elevation: 5,
    },
    image: {
        width: width-20,
        height: height / 3,
        borderRadius: 10
    }
})

export default CarouselItem

axios.js

import axios from 'axios';

const axiosClientLogin = axios.create({
  baseURL: 'http://192.168.1.10:8080',
  headers: {
    'Content-Type':'application/x-www-form-urlencoded',
    "Authorization" : `Basic aWRfY2xpZW50ZToxMjM0NQ==`
  }
});

const axiosClientFormData = axios.create({
  baseURL: 'http://192.168.1.3:8080',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
});

const axiosClient = axios.create({
  baseURL: 'http://192.168.1.10:8080'
});

export {axiosClientLogin, axiosClient, axiosClientFormData};

It should be noted that when I have static data as you have in the youtube video, it works well. but with dynamic data obtained from the api it presents the problem that I tell you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.