vielleicht weiß jemand bessere Lösung, aber ich benutze folgende Dienstprogramm-Klasse zum Scrollen. Angenommen, Sie haben bereits eine Referenz auf das Kind, besteht die Grundidee darin, dass Sie das Kind in der Bildlaufansicht messen können, um seine Position zu bestimmen, und dann können Sie den benötigten Offset berechnen.
/**
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});
}
}
});
});
});
}
note - es funktioniert nur mit RN0.27 + (bis sie Interna von Scroll wieder ändern :))
Danke, ich so etwas versucht, aber 'child.measureLayout' den Fehler wirft "Versucht, das Layout zu messen, aber Offset oder Abmessungen waren NaN". Irgendeine Idee, worum es geht? – nicholas
hm. ist mir nie passiert. vielleicht http://stackoverflow.com/questions/31617206/react-native-get-the-position-of-a-component-in-the-view - aber ich scrollt nur als Reaktion auf Benutzeraktion, so sollte es nicht passieren zu mir, dass ich zu früh messe. – sodik