IT
[Android] 안드로이드스튜디오(Android Studio) 리스트 만들기 RecyclerView Kotlin (1)
은그램
2021. 1. 15. 18:19
728x90
반응형
AndroidStudio 의 RecyclerView 를 사용할 일이 생겼다.
Using RecyclerView with Kotlin.
ListView에서 발전된 버전이 RecyclerView인데,
Android L (롤리팝) 출시와 함께 등장했다.
ListView의 단점인 메모리 문제, 재사용성, 커스텀하기 어려움 등을 해소한 것이 바로 이 것이다.
아래와 같이 동적으로 List를 생성한다고 하면 될 것 같다.
화면은 간단하게 아래와 같은 코드를 적당한 위치에 추가해주면 된다.
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
tools:listitem="@layout/list_layout"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>
※ 굵게 표시한 부분을 꼭 작성해 주어야 한다.
그렇지 않으면, List에 item이 제대로 보여지지 않는다.
※ ListView/ RecyclerView를 커스텀하고, 사용할 수 있도록 도와준다.
아래와 같이 원하는대로 List의 구성을 정해주는 레이아웃을 만들어준다.
list_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/titleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" android:text="Title"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView android:id="@+id/contents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="contents"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</LinearLayout>
xml 준비는 이제 끝났다.👏
다음 포스팅에서 Kotlin 코드 작성을 이어서 해보자 👊👊 !
728x90
반응형