Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Several times, we have seen scrollable items in a Mobile application. Sometimes the list items are very large and dynamic. With the help of React Native, we can easily implement those scrollable lists.
In React Native, there are several ways to build a scrolling list, the most notable of which are the ScrollView and the ListView. Each has advantages and disadvantages.
In this blog, we will briefly discuss about ListView component of React Native.
What is ListView?
ListView in React Native is a view component that displays a list of items in a vertical scrollable list. ListView.DataSource is the very minimal API for creating a list view.
You will need two things to use the ListView component effectively:-
1.) ListView.DataSource: It does a lot of work behind the scenes to turn a basic array into an efficient data blob that the ListView component can understand.
2.) renderRow: It is a function that just returns a component that renders the row. As a prop to the function, the data for that row is passed.
Syntax
A ListView in Android is defined in an XML layout using:
Some of the handiest features of ListView are given below:-
1.) It is entirely Cross-Platform.
2.) Header support.
3.) Footer support.
4.) Separator support.
5.) Sticky Section Headers support.
ListView Methods
getMetrics(): This method retrieves the metrics of the ListView, such as height, width, and other display properties, which are useful for layout calculations and adjustments.
scrollTo(int x, int y): This method scrolls the ListView to the specified x and y coordinates, enabling precise control over the scroll position programmatically.
scrollToEnd(): This method scrolls the ListView to the last item, ensuring that the end of the list is visible, which is particularly useful for displaying new data at the bottom.
flashScrollIndicators(): This method temporarily displays the scroll indicators, providing a visual cue to the user that the ListView is scrollable, enhancing user experience
React Native ListView Examples
Example1
In this example, we establish a data source and populate it with an array of data blobs. Create a ListView component using that array as the data source and feed it to the renderRow callback. The renderRow function returns a component for rendering the row.
In the above code, we create an instance of ListViewDataSource in the constructor. The ListViewDataSource is a parameter that accepts any of the following four arguments:-
We will add separation and perform action on listview items in this example.
Separation is added to create a separate block and improve list item appearance. The renderSeparator prop is used to add a separator between ListView elements (data).
Use the onPress props over Text to perform an action when a list item is clicked. We create an alert message and display the list items that a user selects.
ListView in React Native is a view component that displays a list of items in a vertical scrollable list. ListView.DataSource is the very minimal API for creating a list view.
What is the difference between ListView and FlatList in React Native?
ListView is an older, deprecated component for displaying lists in React Native, requiring more manual setup for performance optimizations. FlatList is the modern alternative, offering better performance out-of-the-box with features like lazy loading, recycling, and support for header/footer components.
What is renderRow in ListView?
renderRowis a function that just returns a component that renders the row. As a prop to the function, the data for that row is passed.