Olive Study Room
[Android Studio] RecyclerView 본문
- recyclerview는 view 재사용을 통해 메모리를 아낄 수 있는 view!
- 이전 메모리, 현재 메모리, 이후 메모리만 저장한다.
- adapter를 통해 작동한다.
- viewholder는 cell 내의 항목을 채운다.
구현 방법은 간단히 세 단계로 정리할 수 있다.
1. recyclerview 위젯이 있는 레이아웃과 recyclerview를 채울 cell의 레이아웃을 만들고,
2. RecyclerView.Adapter와 RecyclerView.ViewHolder를 생성하고
3. mainActivity에서 데이터를 만들고 adapter를 설정해주면 끝!
1. 하나의 작은 view를 구성할 cell의 레이아웃과 메인 레이아웃 생성
// activity_main.xml
<?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:background="@color/white"
android:orientation="horizontal"
android:padding="10dp"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
// custom_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:padding="10dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="TITLE"
android:textColor="@color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/user_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="content"
android:textColor="@color/black" />
</LinearLayout>
<ImageView
android:id="@+id/user_profile"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:src="@mipmap/ic_launcher" />
</LinearLayout>


2. RecyclerView.Adapter, RecyclerView.ViewHolder 생성
Adapter는 1. 넣을 데이터 리스트를 갖고
2. Viewholder를 갖는 RecyclerView.Adapter를 상속받는다.
메소드는 크게 생성, 수정, 표시할 cell수로 나뉜다.
cell의 내용에 직접 접근하지 않고 cell 레이아웃을 받는 viewholder에 변수로 항목을 지정해서 변수를 채우는 방식으로 생성한다.
- 생성(onCreateViewHolder)
cell 레이아웃을 받은 viewholder를 반환한다.
- 수정(onBindViewHolder)
viewholder의 변수에 접근해 항목을 채운다.
- cell 수(getItemCount)
데이터 개수에 맞춰 cell 수를 반환한다.
// v는 custom_list를 의미
class CustomViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val profile = v.user_profile
val name = v.user_name
val age = v.user_age
}
class CustomAdapter(val DataList: ArrayList<MainActivity.Data>): RecyclerView.Adapter<CustomViewHolder>() {
// 생성
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val cellForRow = LayoutInflater.from(parent.context).inflate(R.layout.custom_list, parent, false)
return CustomViewHolder(cellForRow)
}
// 수정
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
holder.profile.setImageResource(DataList[position].profile)
holder.name.text = "이름 : " + DataList[position].name
holder.age.text = DataList[position].age + "세"
}
override fun getItemCount(): Int {
return DataList.size
}
}
3. 데이터 생성 및 MainActivity에서 adapter 설정
class MainActivity : AppCompatActivity() {
class Data(val profile: Int, val name: String, val age: String)
val DataList = arrayListOf(
Data(R.drawable.profilesize, "올리브", "5"),
Data(R.drawable.profilesize, "올리브", "6"),
Data(R.drawable.profilesize, "올리브", "38"),
Data(R.drawable.profilesize, "올리브", "94"),
Data(R.drawable.profilesize, "올리브", "24"),
Data(R.drawable.profilesize, "올리브", "22"),
Data(R.drawable.profilesize, "올리브", "56"),
Data(R.drawable.profilesize, "올리브", "5")
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyView: RecyclerView = findViewById(R.id.recyclerView)
recyView.layoutManager = LinearLayoutManager(this)
// view.adapter
recyView.adapter = CustomAdapter(DataList)
}
}
왜인지 gradle에 recylerview가 자동 implemention되지 않아 그냥 최신 버전의 recyclerview를 입력해주었다.
implementation "androidx.recyclerview:recyclerview:1.2.1"
'Coding > Android' 카테고리의 다른 글
[Android Studio] Fragment, ViewPager2 (0) | 2021.07.20 |
---|---|
[Android Studio] persistentState: PersistableBundle (0) | 2021.07.17 |
[Android Studio] 레이아웃 (0) | 2021.07.10 |
[Android Studio] 설치, AVD(Android Virtual Device) 세팅, 환경 설명 (0) | 2021.07.09 |
[Kotlin] 기본 문법 & 필요 지식 (0) | 2021.07.07 |