如何在本机反应中向下滚动特定值?

路易斯

我想滚动到一个在我的可触摸不透明度下是 y 单位的元素(当我按下它时)。我尝试使用 ref 和 useref 但这不是我想要的。按下按钮时是否可以向下滚动特定长度?这是一些代码来说明:

return (
                  <View>
                    <Text
                      onPress={() => {
                          idData = data.id;
                          totalid++; // here is the total, the length 
                          //that I want to scroll down
                          alert("mon id est: " + idData);
                          viewRef.current?.focus();
                        }
                      }}
                    >
                      {Data.name}
                    </Text>
                  </View>

在下面的某个地方:

<View ref={viewRef}>
无显示

尽管这不是必需的,但我建议您将 View 组件更改为 ScrollView,因为无论何时您想要拥有可滚动的内容,无论您有多少导航按钮,都最好选择向上和向下滑动:

return (
                  <ScrollView>
                    <Text
                      onPress={() => {
                          idData = data.id;
                          totalid++; // here is the total, the length 
                          //that I want to scroll down
                          alert("mon id est: " + idData);
                          viewRef.current?.focus();
                        }
                      }}
                    >
                      {Data.name}
                    </Text>
                  </ScrollView>

现在,由于我没有您要滚动浏览的项目列表,我建议您阅读此内容,请记住,虽然示例使用的是 View,但您可以使用 ScrollView,但这一切都取决于数据:https ://aboutreact.com/scroll_to_a_specific_item_in_scrollview_list_view/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章