React native 是否滚动到本机ListView中的一行?
在React Native ListView中有没有滚动到特定行的方法React native 是否滚动到本机ListView中的一行?,react-native,React Native,在React Native ListView中有没有滚动到特定行的方法 我有要滚动到的行的索引。下面我可以得到我想要滚动到的行的引用。我可以用它来做滚动。但我如何把它们联系在一起;我尝试查找行的偏移量的每一种方法都会导致NaNs或其他一些错误 也许有人知道更好的解决方案,但我使用下面的实用程序类进行滚动。假设已经有了对子对象的引用,基本思想是可以在scrollview中测量子对象以确定其位置,然后可以计算所需的偏移量 /** scroll child within scrollView. @p
我有要滚动到的行的索引。下面我可以得到我想要滚动到的行的引用。我可以用它来做滚动。但我如何把它们联系在一起;我尝试查找行的偏移量的每一种方法都会导致
NaN
s或其他一些错误 也许有人知道更好的解决方案,但我使用下面的实用程序类进行滚动。假设已经有了对子对象的引用,基本思想是可以在scrollview中测量子对象以确定其位置,然后可以计算所需的偏移量
/**
scroll child within scrollView.
@param scrollView reference to scrollView
@param child - measureable child (see measureLayout in https://facebook.github.io/react-native/docs/nativemethodsmixin.html )
@param opt - options - (*default*)
{
destination : *'top'*, 'bottom' : where to scroll - to the top or bottom of view
}
*/
export function scrollTo(scrollView, child, opt = {}) {
const destination = opt.destination || TOP;
const handle = React.findNodeHandle(scrollView);
child.measureLayout(handle, (x,y, width, height) => {
// kind of hack - not really documented feature
scrollView.refs.ScrollView.measure((sx,sy, sw, sh, sPageX, sPageY) => {
scrollView.refs.InnerScrollView.measure((isx,isy, isw, ish, isPageX, isPageY) => {
console.log("scrollTo", x,y, width, height, sx,sy, sw, sh, sPageX, sPageY, isx,isy, isw, ish, isPageX, isPageY);
const currentScrollPosition = sPageY - isPageY;
if (currentScrollPosition <= y && y + height <= currentScrollPosition + sh) {
// the child fits into scroll view -> noop
console.log("fitting in");
} else {
if (destination === TOP) {
if (y + sh > ish) {
// we would scroll too much (there is not so much content after child to fill whole scroll view)
scrollView.scrollTo({x:0, y: ish - sh});
} else {
scrollView.scrollTo({x:0, y});
}
} else {
scrollView.scrollTo({x:0, y: y + height - sh});
}
}
});
});
});
}
/**
在scrollView中滚动子对象。
@参数scrollView对scrollView的引用
@param child-可测量的子对象(请参见中的measureLayouthttps://facebook.github.io/react-native/docs/nativemethodsmixin.html )
@参数opt-选项-(*默认值*)
{
目标:“'top'*,'bottom'”:滚动到视图顶部或底部的位置
}
*/
导出函数scrollTo(scrollView,child,opt={}){
const destination=opt.destination | | TOP;
const handle=React.findNodeHandle(滚动视图);
小尺寸(手柄,(x,y,宽度,高度)=>{
//一种黑客行为——不是真正的文档化功能
scrollView.refs.scrollView.measure((sx、sy、sw、sh、sPageX、sPageY)=>{
scrollView.refs.InnerScrollView.measure((isx、isy、isw、ish、isPageX、isPageY)=>{
日志(“滚动到”,x,y,宽度,高度,sx,sy,sw,sh,sPageX,sPageY,isx,isy,isw,ish,isPageX,isPageY);
const currentScrollPosition=sPageY-isPageY;
如果(当前滚动位置){
//我们会滚动太多(子级后没有太多内容填充整个滚动视图)
scrollView.scrollTo({x:0,y:ish-sh});
}否则{
scrollView.scrollTo({x:0,y});
}
}否则{
scrollView.scrollTo({x:0,y:y+height-sh});
}
}
});
});
});
}
注意-它只适用于RN0.27+(直到他们再次更改scrollview的内部结构:)谢谢,我尝试了类似的方法,但是
子对象。measureLayout
抛出错误“尝试测量布局,但偏移或尺寸为NaN”。知道那是怎么回事吗?嗯,我从来没有发生过。也许吧——不过我只是根据用户的动作滚动,所以我不应该过早地进行测量。