vjtechno
vjtechno

Reputation: 484

Update Progress bar value when end scrolling in ScrollView on React Native

I have created a functionality , In which show the Progress bar and it's value change according to scrolling the view. the progress bar value should depend that scrolling is end or not if the scrolling is end then the progress bar should completely filled. I have tired but it's not working. Here my code:

import React, {useState, useEffect} from 'react';
import * as Progress from 'react-native-progress';
import { Card } from 'react-native-paper';
import { Text, View, StyleSheet,ScrollView } from 'react-native';
const scrollView_height = 0;
const scrollViewContent_height = 0;

export default function App() {

 const UpdateProgressBar = (progress) => {
    setProgress(
      Math.abs(
        progress.nativeEvent.contentOffset.y /
          (scrollViewContent_height - scrollView_height),
      ),
    );
  };

  return (
    <View style={styles.container}>
              <Progress.Bar
                style={{
                  position: 'relative',
                  bottom: 6,
                  borderTopLeftRadius: 40,
                  borderTopRightRadius: 40,
                }}
                height={3}
                borderWidth={0}
                progress={progress_count}
                color="red"
                width={widthToDp('82%')}
              />
                <ScrollView
                showsVerticalScrollIndicator={false}
                bounces={false}
                contentContainerStyle={{paddingBottom: 0}}
                onContentSizeChange={(width, height) => {
                  scrollViewContent_height = height;
                }}
                onScroll={UpdateProgressBar}
                onLayout={(event) =>
                  (scrollView_height = event.nativeEvent.layout.height)
                }
                scrollEventThrottle={12}>
      <Text style={styles.paragraph}>
        Change code in the editor and watch it change on your phone! Save to get a 
        shareable url.
      </Text>
      <Text style={styles.paragraph}>
        Change code in the editor and watch it change on your phone! Save to get a 
        shareable url.
      </Text>
      <Text style={styles.paragraph}>
        Change code in the editor and watch it change on your phone! Save to get a 
        shareable url.
      </Text>
      <Text style={styles.paragraph}>
        Change code in the editor and watch it change on your phone! Save to get a 
        shareable url.
      </Text>
      <Card>
        <AssetExample />
      </Card>
     </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
   
  }
});

Please Suggest the Solution.

Upvotes: 0

Views: 686

Answers (1)

Kirill Novikov
Kirill Novikov

Reputation: 3067

import React, {useState, useEffect} from 'react';
import * as Progress from 'react-native-progress';
import { Card } from 'react-native-paper';
import { Text, View, StyleSheet,ScrollView } from 'react-native';

export default function App() {
 const [scrollView_height, setScrollView_height] = useState(0)
 const [scrollViewContent_height, setScrollViewContent_height] = useState(0)
 const [progress, setProgress] = useState(0)
 
 const UpdateProgressBar = (value) => {
    setProgress(
      Math.abs(
        value.nativeEvent.contentOffset.y /
          (scrollViewContent_height - scrollView_height),
      ),
    );
  };

  return (
    <View style={styles.container}>
              <Progress.Bar
                style={{
                  position: 'relative',
                  bottom: 6,
                  borderTopLeftRadius: 40,
                  borderTopRightRadius: 40,
                }}
                height={3}
                borderWidth={0}
                progress={progress}
                color="red"
                width={widthToDp('82%')}
              />
                <ScrollView
                showsVerticalScrollIndicator={false}
                bounces={false}
                contentContainerStyle={{paddingBottom: 0}}
                onContentSizeChange={(width, height) => {
                  setScrollView_height(height);
                }}
                onScroll={UpdateProgressBar}
                onLayout={(event) =>
                  setScrollView_height(event.nativeEvent.layout.height)
                }
                scrollEventThrottle={12}>
      <Text style={styles.paragraph}>
        Change code in the editor and watch it change on your phone! Save to get a 
        shareable url.
      </Text>
      <Text style={styles.paragraph}>
        Change code in the editor and watch it change on your phone! Save to get a 
        shareable url.
      </Text>
      <Text style={styles.paragraph}>
        Change code in the editor and watch it change on your phone! Save to get a 
        shareable url.
      </Text>
      <Text style={styles.paragraph}>
        Change code in the editor and watch it change on your phone! Save to get a 
        shareable url.
      </Text>
      <Card>
        <AssetExample />
      </Card>
     </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
   
  }
});

Upvotes: 1

Related Questions