ANDROID RECYCLERVIEW - FUNDAMENTALS 03

Dmitry Klokov·2020년 12월 27일
0

ANDROID-KOTLIN-FUNDAMENTALS

목록 보기
11/19
post-thumbnail

SUMMARY

  • Displaying a list or grid of data is one of the most common UI tasks in Android. RecyclerView is designed to be efficient even when displaying extremely large lists.

  • RecyclerView does only the work necessary to process or draw items that are currently visible on the screen.

  • When an item scrolls off the screen, its views are recycled. That means the item is filled with new content that scrolls onto the screen.

  • The adapter pattern in software engineering helps an object work together with another API. RecyclerView uses an adapter to transform app data into something it can display, without the need for changing how the app stores and processes data.

To display your data in a RecyclerView, you need the following parts :

  • RecyclerView
    To create an instance of RecyclerView, define a <RecyclerView> element in the layout file.

  • LayoutManager
    A RecyclerView uses a LayoutManager to organize the layout of the items in the RecyclerView, such as laying them out in a grid or in a linear list.

    In the <RecyclerView> in the layout file, set the app:layoutManager attribute to the layout manager (such as LinearLayoutManager or GridLayoutManager).

    You can also set the LayoutManager for a RecyclerView programmatically. (This technique is covered in a later codelab.)

  • Layout for each item
    Create a layout for one item of data in an XML layout file.

  • Adapter
    Create an adapter that prepares the data and how it will be displayed in a ViewHolder. Associate the adapter with the RecyclerView.

    When RecyclerView runs, it will use the adapter to figure out how to display the data on the screen.

    The adapter requires you to implement the following methods:
    – getItemCount() to return the number of items.
    – onCreateViewHolder() to return the ViewHolder for an item in the list.
    – onBindViewHolder() to adapt the data to the views for an item in the list.

  • ViewHolder
    A ViewHolder contains the view information for displaying one item from the item's layout.

  • The onBindViewHolder() method in the adapter adapts the data to the views. You always override this method. Typically, onBindViewHolder() inflates the layout for an item, and puts the data in the views in the layout.

  • Because the RecyclerView knows nothing about the data, the Adapter needs to inform the RecyclerView when that data changes. Use notifyDataSetChanged()to notify the Adapter that the data has changed.

LEARN MORE

Android developer documentation :
Create a List with RecyclerView

Learn more :
Android Kotlin Fundamentals: RecyclerView fundamentals

profile
Power Weekend

0개의 댓글