How to get currently visible index in RN flat list

前端 未结 5 1115
温柔的废话
温柔的废话 2021-02-01 01:54

I have a horizontal flat list where each item is width:300 All I am trying to do is to get index of currently visible item.



        
5条回答
  •  自闭症患者
    2021-02-01 02:39

    UPD. thanks to @ch271828n for pointing that onViewableItemsChanged shouldn`t be updated

    You can use FlatList onViewableItemsChanged prop to get what you want.

    class Test extends React.Component {
      onViewableItemsChanged = ({ viewableItems, changed }) => {
        console.log("Visible items are", viewableItems);
        console.log("Changed in this iteration", changed);
      }
    
      render () => {
        return (
          
        )
      }
    }
    

    viewabilityConfig can help you to make whatever you want with viewability settings. In code example 50 means that item is considered visible if it is visible for more than 50 percents.

    Config stucture can be found here

提交回复
热议问题