首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当ScrollView中的end滚动时更新进度条值

当ScrollView中的end滚动时更新进度条值
EN

Stack Overflow用户
提问于 2022-11-02 14:02:59
回答 1查看 51关注 0票数 0

我已经创建了一个功能,在其中显示进度条和它的值根据滚动视图的变化。进度条值应取决于滚动是否结束,如果滚动是结束,则进度条应完全填充。我累了,但不起作用了。这里我的代码:

代码语言:javascript
运行
复制
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',
   
  }
});

请提出解决办法。

EN

Stack Overflow用户

发布于 2022-11-02 14:59:14

代码语言:javascript
运行
复制
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',
   
  }
});
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74290460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档