左右滚动时隐藏和显示 React-Native

发布于 2025-01-20 09:21:21 字数 2425 浏览 2 评论 0原文

  1. 在React Native中,我试图创建一个具有左右箭头的扁平列表水平,我希望在按右侧或向左的侧面或左侧,直到条件,在按下它保持不可见的情况下,可以看到箭头。 更改
  2. 我希望左箭头的
  3. 在默认情况下是不可见的,当按右箭头时,右箭头应在左箭头变为可见时不可见,反之亦然,
  4. 但是如果按两到三倍,则不应该看到相同的箭头

  const leftScrollHandler = () => {
    listViewRef.scrollToOffset({ offset: 0.5, animated: true });
  };

  const rightScrollHandler = () => {
    //OnCLick of right button we scrolled the list to bottom
    listViewRef.scrollToEnd({ animated: true });
  };
  // this below is for scroller to apear and disapear funtionality

  const [backgroundColor, setBackgroundColor] = useState("transparent");
  const [backgroundColor2, setBackgroundColor2] = useState(colors.secondary);
  const [pressed, setPressed] = useState(false);
  function changeColor() {
    if (!pressed) {
      setPressed(true);
      setBackgroundColor(colors.secondary);
      setBackgroundColor2("transparent");
    } else {
      setPressed(false);
      setBackgroundColor2(colors.secondary);
      setBackgroundColor("transparent");
    }
  }

 <FlatList
        horizontal={true}
        showsHorizontalScrollIndicator={false}
        data={Data}
        keyExtractor={(item, index) => [item.id, index.toString()]}
        renderItem={renderItem}
        ref={(ref) => {
          listViewRef = ref;
        }}
      />
      <TouchableOpacity
        activeOpacity={0.5}
        onPress={() => {
          changeColor();
          leftScrollHandler();
        }}
        style={
          (web && isTablet) || isDesktop
            ? styles.leftScrollDesktop
            : styles.leftScroll
        } >
        <AntDesign
          style={styles.leftArrow}
          name="arrowleft"
          size={(web && isTablet) || isDesktop ? 50 : 35}
          color={backgroundColor}
        />
      </TouchableOpacity>
      <TouchableOpacity
        activeOpacity={0.5}
        onPress={() => {
          changeColor();
          rightScrollHandler();
        }}
        style={
          (web && isTablet) || isDesktop
            ? styles.rightScrollDesktop
            : styles.rightScroll
        }
      >
        <AntDesign
          style={styles.rightArrow}
          name="arrowright"
          size={(web && isTablet) || isDesktop ? 50 : 35}
          color={backgroundColor2}
        />
      </TouchableOpacity>
  1. In React Native, I am trying to create a Flatlist horizontal with right and left arrows, and I hope to make arrows to be visible and invisible on press on condition that it stays invisible if it is already on the side of right or left until condition changes
  2. I want left arrow be invisible in default
  3. when pressed right arrow, right arrow should be invisible while left arrow becomes visible and vice-versa
  4. however it should not be visible if you press two or three times the same arrow

  const leftScrollHandler = () => {
    listViewRef.scrollToOffset({ offset: 0.5, animated: true });
  };

  const rightScrollHandler = () => {
    //OnCLick of right button we scrolled the list to bottom
    listViewRef.scrollToEnd({ animated: true });
  };
  // this below is for scroller to apear and disapear funtionality

  const [backgroundColor, setBackgroundColor] = useState("transparent");
  const [backgroundColor2, setBackgroundColor2] = useState(colors.secondary);
  const [pressed, setPressed] = useState(false);
  function changeColor() {
    if (!pressed) {
      setPressed(true);
      setBackgroundColor(colors.secondary);
      setBackgroundColor2("transparent");
    } else {
      setPressed(false);
      setBackgroundColor2(colors.secondary);
      setBackgroundColor("transparent");
    }
  }

 <FlatList
        horizontal={true}
        showsHorizontalScrollIndicator={false}
        data={Data}
        keyExtractor={(item, index) => [item.id, index.toString()]}
        renderItem={renderItem}
        ref={(ref) => {
          listViewRef = ref;
        }}
      />
      <TouchableOpacity
        activeOpacity={0.5}
        onPress={() => {
          changeColor();
          leftScrollHandler();
        }}
        style={
          (web && isTablet) || isDesktop
            ? styles.leftScrollDesktop
            : styles.leftScroll
        } >
        <AntDesign
          style={styles.leftArrow}
          name="arrowleft"
          size={(web && isTablet) || isDesktop ? 50 : 35}
          color={backgroundColor}
        />
      </TouchableOpacity>
      <TouchableOpacity
        activeOpacity={0.5}
        onPress={() => {
          changeColor();
          rightScrollHandler();
        }}
        style={
          (web && isTablet) || isDesktop
            ? styles.rightScrollDesktop
            : styles.rightScroll
        }
      >
        <AntDesign
          style={styles.rightArrow}
          name="arrowright"
          size={(web && isTablet) || isDesktop ? 50 : 35}
          color={backgroundColor2}
        />
      </TouchableOpacity>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文