ListView  |  Android Developers

<aside> 💡 ListView 목록을 세로로 배치하여 스크롤 가능한 보기 모음을 표시한다. 더 유연하며 성능이 뛰어난 목록 표시 방법을 사용하려면 RecyclerView 를 사용을 권장한다.

</aside>

Adapter Pattern

Untitled

ListView의 동작원리

Untitled

Untitled

Untitled

XML <ListView>

<ListView
      android:id="@+id/list_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

<aside> 💡 목록 항목을 표시하려면 Activity 에서 setAdapter() 를 이용하여 어댑터를 목록과 연결하도록 호출해야 한다.

데이터 셋의 각 항 목에 대해 더 많은 사용자 정의 보기를 표시하려면 ListAdapter 를 권장한다.

</aside>

layout 리소스 만들기(리스트 아이템)

Untitled

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/white">
  <ImageView
      android:id="@+id/list_view_image"
      android:layout_width="90dp"
      android:layout_height="90dp"
      android:src="@drawable/dock"
      android:scaleType="centerCrop"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"/>

  <TextView
      android:id="@+id/list_view_title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:text="Title"
      android:textColor="@color/black"
      android:textSize="20sp"
      app:layout_constraintStart_toEndOf="@+id/list_view_image"
      app:layout_constraintTop_toTopOf="parent"/>

  <TextView
      android:id="@+id/list_view_desc"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:text="text text text text text text text text text text text text text text text text text text "
      android:textColor="@color/black"
      android:textSize="15sp"
      app:layout_constraintStart_toEndOf="@+id/list_view_image"
      app:layout_constraintTop_toBottomOf="@+id/list_view_title"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Adapter

BaseAdapter 를 상속받아 어댑터 클래스를 생성한다.