Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FlatList] scrollToIndex () #14198

Closed
LiuCao opened this issue May 26, 2017 · 7 comments
Closed

[FlatList] scrollToIndex () #14198

LiuCao opened this issue May 26, 2017 · 7 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@LiuCao
Copy link

LiuCao commented May 26, 2017

  • RN 0.44.0
    -Description
    FlatList uses scrollToIndex () to scroll to the last few elements, and the element leaves the bottom of the list. And when scrolling to the last element, it is reported scrollToIndex should be used in conjunction with getItemLayout otherwise there is no way to know the location of an arbitrary index. error.
    e.g.
    RN FlatList
@LiuCao
Copy link
Author

LiuCao commented May 26, 2017

I have a solution, but modified the framework file, so it is not recommended to do so. And I do not know if it will cause other issue

  • /your Project/node_modules/react-native/Libraries/Lists/VirtualizedList.js
// scrollToIndex may be janky without getItemLayout prop
  scrollToIndex(params: {animated?: ?boolean, index: number, viewPosition?: number}) {
    const {data, horizontal, getItemCount, getItemLayout} = this.props;
    const {animated, index, viewPosition} = params;
    invariant(
      index >= 0 && index < getItemCount(data),
      `scrollToIndex out of range: ${index} vs ${getItemCount(data) - 1}`,
    );
    // invariant(
    //   getItemLayout || index < this._highestMeasuredFrameIndex,
    //   'scrollToIndex should be used in conjunction with getItemLayout, ' +
    //     'otherwise there is no way to know the location of an arbitrary index.',
    // );
    const frame = this._getFrameMetricsApprox(index);
    let offset;
    if((frame.offset - frame.length + this._scrollMetrics.visibleLength) 
          >= this._totalCellLength){
      offset = this._totalCellLength - this._scrollMetrics.visibleLength;
    }else{
      offset = Math.max(
        0,
        frame.offset - (viewPosition || 0) * (this._scrollMetrics.visibleLength - frame.length),
      );
    }
    this._scrollRef.scrollTo(horizontal ? {x: offset, animated} : {y: offset, animated});
  }

@annelorraineuy
Copy link

I am seeing this issue as well, but on a horizontal Flatlist. The last item could leave the screen and doesn't lock at the "bottom" (or in the case of my horizontal Flatlist, on the right).

@hramos hramos changed the title About FlatList scrollToIndex () [FlatList] scrollToIndex () Jun 2, 2017
@hramos
Copy link
Contributor

hramos commented Jun 2, 2017

Hey, thanks for reporting this issue!

It looks like your description is missing some necessary information. Can you please add all the details specified in the template? This is necessary for people to be able to understand and reproduce the issue being reported.

I am going to close this, but feel free to open a new issue with the additional information provided. Thanks!

@hramos hramos closed this as completed Jun 2, 2017
@MichaelPintos
Copy link

i have this bug in RN 0.45.1

@wj495175289
Copy link

wj495175289 commented Sep 5, 2017

scrollToIndex(params: {animated?: ?boolean, index: number, viewPosition?: number}) {
const {data, horizontal, getItemCount, getItemLayout} = this.props;
const {animated, index, viewPosition} = params;
invariant(
index >= 0 && index < getItemCount(data),
scrollToIndex out of range: ${index} vs ${getItemCount(data) - 1},
);
// invariant(
// getItemLayout || index < this._highestMeasuredFrameIndex,
// 'scrollToIndex should be used in conjunction with getItemLayout, ' +
// 'otherwise there is no way to know the location of an arbitrary index.',
// );
const frame = this._getFrameMetricsApprox(index);
let offset;
if(Platform.OS == 'ios' && (frame.offset - frame.length + this._scrollMetrics.visibleLength - (params.iosOffset || 0))
>= this._totalCellLength){
//offset = this._totalCellLength - this._scrollMetrics.visibleLength -64;
this.scrollToEnd(animated);
return;
}else{
offset = Math.max(
0,
frame.offset - (viewPosition || 0) * (this._scrollMetrics.visibleLength - frame.length),
) - (params.range || 0);
}
this._scrollRef.scrollTo(horizontal ? {x: offset, animated} : {y: offset, animated});
}

range: can set for absolute layout
iosOffset:also can set for header or fotter,iosOffset is header height
iosOffset = this._headerLength;

@martinezguillaume
Copy link
Contributor

I have this bug on 0.50.3, when scrollTo is scrolling at the end of the FlatList 😔

@spedy
Copy link

spedy commented Apr 29, 2018

I just added

onScrollToIndexFailed={()=>{}}

and it worked just fine

@facebook facebook locked as resolved and limited conversation to collaborators Jun 2, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

8 participants