How to get currently visible index in RN flat list

前端 未结 5 1067
温柔的废话
温柔的废话 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:48

    Many thanks to the most-voted answer :) However, it does not work when I try it, raising an error saying that changing onViewableItemsChanged on the fly is not supported. After some search, I find the solution here. Here is the full code that can run without error. The key point is that the two configs should be put as class properties instead of inside the render() function.

    class MyComponent extends Component {  
      _onViewableItemsChanged = ({ viewableItems, changed }) => {
        console.log("Visible items are", viewableItems);
        console.log("Changed in this iteration", changed);
      };
    
      _viewabilityConfig = {
        itemVisiblePercentThreshold: 50
      };
    
      render() {
        return (
            
          
        );
      }
    }
    

提交回复
热议问题