목록 — Android Compose의 기존 방식과 새로운 방식

Feb 17 2023
거의 20년 동안(2008년 이후) Android용 앱에 목록을 추가해야 할 때 ListView를 사용했습니다. 이 목록은 나중에 Android 지원 라이브러리의 머티리얼 디자인의 일부로 RecyclerView(2014년)로 업그레이드되고 나중에 추가되었습니다. androidX(2018년)의 일부로. 그런 다음 2019년에 Google IO에서 UI를 사용하는 새로운 작업 방식이 발표되었고 2년 후인 2021년에 Google은 Jetpack Compose의 첫 번째 안정적인 버전을 출시했습니다.

거의 20년 동안(2008년부터) Android용 앱에 목록을 추가해야 할 때 우리는 ListView 를 사용했습니다. 이 목록 은 나중에 Android 지원 라이브러리의 머티리얼 디자인의 일부로 RecyclerView (2014년) 로 업그레이드되고 나중에 추가되었습니다. androidX (2018년) 의 일부로 .

그런 다음 2019년에 Google IO 에서 UI로 작업하는 새로운 방식이 발표되었고 2년 후인 2021년에 Google은 Jetpack Compose 의 첫 번째 안정 버전을 출시했습니다 .
3가지 주요 이점을 언급하는 것 외에는 작성 작업과 xml을 사용하는 이전 방식의 차이점에 대해 설명하지 않겠습니다.

  • 적은 코딩
  • 하나의 진실 소스(아래 예에서 볼 수 있음)
  • 새로운 기능/버그 수정 받기

옛날 방식

  1. 화면 레이아웃을 나타내는 xml에 재활용 보기 요소를 추가합니다.
  2. <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:listitem="@layout/series_item" />
    

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:context=".MainActivity"
        android:gravity="center">
    
        <ImageView
            android:id="@+id/imageview"
            android:layout_width="150dp"
            android:layout_height="150dp" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="15dp"
                android:text="Item"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:textStyle="bold" />
    
            <TextView
                android:id="@+id/description"
                android:layout_below="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="15dp"
                android:text="Item"
                android:textColor="@color/white"
                android:textSize="14sp"
                 />
        </RelativeLayout>
    
    
    
    </LinearLayout>
    

    class SeriesItemAdapter(private val list: List<Series>) : RecyclerView.Adapter<SeriesItemAdapter.SeriesDataHolder>() {
    
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SeriesDataHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.series_item, parent, false)
    
            return SeriesDataHolder(view)
        }
    
        override fun onBindViewHolder(holder: SeriesDataHolder, position: Int) {
            val series = list[position]
    
            holder.imageView.setImageResource(series.imageId)
            holder.titleView.text = series.title
            holder.descriptionView.text = series.description
        }
    
        override fun getItemCount(): Int {
            return list.size
        }
    
        class SeriesDataHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
            val imageView: ImageView = itemView.findViewById(R.id.imageview)
            val titleView: TextView = itemView.findViewById(R.id.title)
            val descriptionView: TextView = itemView.findViewById(R.id.description)
        }
    
    }
    

    val seriesList = findViewById<RecyclerView>(R.id.recyclerview)
            seriesList.layoutManager = LinearLayoutManager(this)
            val adapter = SeriesItemAdapter(SeriesDataProvider.seriesList)
            seriesList.adapter = adapter
    

  3. 목록에 대해 구성해야 하는 2개의 xml입니다.
  4. 해당 XML을 연결하는 코드의 위치입니다.
  5. 데이터 홀더와 바인더를 나타내는 어댑터 클래스입니다.
  6. 새로운 길

    1. 목록에 LazyColumn 컴포저블 사용
    2. @Composable
          fun SeriesList() {
              LazyColumn(
                  modifier = Modifier.background(Color.Black)
              ) {
                  items(
                      items = SeriesDataProvider.seriesList,
                      itemContent = {
                          Series(series = it)
                      }
                  )
              }
          }
      

      Row() {
                  Image(
                      modifier =  Modifier.size(150.dp).align(Alignment.CenterVertically),
                      painter =  painterResource(id = series.imageId),
                      contentDescription = "")
                  Column(
                      modifier = Modifier
                      .padding(16.dp)
                      .align(Alignment.CenterVertically)
                  ) {
                      Text(color = Color.White, text = series.title, style = typography.h6)
                      Text(color = Color.White,text = series.description, style = typography.caption)
                  }
              }