Home / Demo / Implementing a Recyclable List View in Unity

Implementing a Recyclable List View in Unity

/
/
/
3122 Views

This week, when working on a country selection list, I’ve decided to have some fun trying to implement a recyclable list view in Unity 3d (something inspired in Android’s ListView recycling mechanism).

  Complete project on GitHub

The main advantage of using a recycling mechanism is that, instead of instantiating a new row for each list item, a minimum number of rows is instantiated and when necessary, each existing row is updated to represent a new list item. In this way, the number of rows becomes independent of the list length.

Recyclable List View Breakdown

  • The List.cs is the main list controller that handles the list behaviour and the recycling mechanism;
  • The ListItemBase.cs is the base item to extend when implementing your own list item component;
  • The List.cs requires a prefab with a ListItemBase.cs (or subclass) assigned;

How to use it

  • Drag the List prefab to a Canvas gameObject; IMAGE1
  • Implement your own ListItem component – extend the ListItemBase.cs – and assign it to the List component (use the ListItem prefab and replace the component); IMAGE2
  • Create a controller with a reference to the List component and the ListItem prefab – check the example below; IMAGE3
Links:

 

UPDATE #1

This week goal was to implement a Photo Gallery controller with Fullscreen and Page Control Dots on top of my List controller. But unfortunately, the List changes took more time than I was expecting. So wait for it on a next update.

This week, I was able to add the following features to my recyclable list view project:

  • Horizontal Scrolling: choose the scroll orientation on the List.cs Inspector; IMAGE4
  • Fit Item to Viewport: automatically adapt the list item size to the viewport rect; IMAGE5
  • Center on Item: automatically center on the closest item when releasing the scroll rect; IMAGE6

Check the Gallery.scene examples to see a Horizontal List with Fit Item to Viewport and Center on Item features enabled.

I realize the List.cs code is ugly af but I promise I’ll try to make it better on a following update 🙂

Happy Coding 🙂

 

2 Comments

  1. Hey. I love all of your tutorials! I just finished the one on Android plugins. Do you know if it’s possible to make an android plugin for Unity that uses features in Android that require Android studio UI? I’m trying to make a video player in Android Studio that I can put into Unity. Thank you so much for making these tutorials!

    • Hey,

      Thanks, I’m glad you like it 🙂

      AFAIK you can place Android UI components on the Unity Android Activity. I’ve tried it before with some InputFields and it worked. But I never tried it with a video player component.

Leave a Comment

Your email address will not be published. Required fields are marked *