[ANDROID] KOTLIN – RECYCLER VIEW
2020. 12. 10. 10:45ㆍprogramming/android
728x90
RecyclerView는 안드로이드의 ListView의 장/단점을 보완하고자 사용하는 위젯입니다.
SupportLibrary에 포함되며 v7 이상에서 사용 가능합니다.
RecyclerView는 LayoutManager를 통해서 View를 그리는 방법을 정의합니다.
res/layout/activity_main.xml
<xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
res/layout/item_list.xml
<xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tvItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
kr.kotlin.test.main.activity.MainActivity
package kr.kotlin.test.main.activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kr.kotlin.test.vo
import kr.kotlin.test.R
import kr.kotlin.test.main.adapter.MainAdapter
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//레이아웃매니저 설정
rvList.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
rvList.setHasFixedSize(true)
//어답터 설정
rvList.adapter = MainAdapter(initData())
}
fun initData(): MutableList<BoardVO> {
var itemList: MutableList<BoardVO> = arrayListOf()
itemList.add(BoardVO("KOTLIN"))
itemList.add(BoardVO("PROGRAMMING"))
itemList.add(BoardVO("TEST"))
itemList.add(BoardVO("FINISH"))
return itemList
}
}
kr.kotlin.test.main.adapter.MainAdapter
package kr.kotlin.test.main.adapter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kr.kotlin.test.R
import kotlinx.android.synthetic.main.item_list.view.*
import kr.kotlin.test.main.vo
class mainAdapter(initData: MutableList<BoardVO>) : RecyclerView.Adapter<MainAdapter.CustomHolder>() {
private var items: MutableList<BoardVO> = initData
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = MainAdapter.CustomHolder(parent)
override fun onBindViewHolder(holder: CustomHolder, position: Int) {
items[position].let { item ->
holder!!.tvItem.text = item.title
}
}
override fun getItemCount() = items.size
fun addItems(items: List<Service>) {
this.items.addAll(items.toMutableList())
}
fun clearItems() {
this.items.clear()
notifyDataSetChanged()
}
class CustomHolder constructor(parent: ViewGroup) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_list, parent, false)) {
val context = itemView.context
val tvItem = itemView.tvItem
}
}
kr.kotlin.test.main.vo.BoardVO
package kr.kotlin.test.main.vo
class BoardVO(title:String) {
var title: String = title
}
728x90
'programming > android' 카테고리의 다른 글
[ANDROID] KOTLIN – SCROLLVIEW CAN HOST ONLY ONE DIRECT CHILD (0) | 2020.12.10 |
---|---|
[ANDROID] KOTLIN – FLOATINGACTIONBUTTON SETVISIBILITY (0) | 2020.12.10 |
[ANDORID] KOTLIN – LAYOUTINFLATER (0) | 2020.12.10 |
[ANDORID] KOTLIN – APPLICATIONCONTEXT VS BASECONTEXT (0) | 2020.12.10 |
[Android] Grid View (0) | 2011.11.06 |