목록 화면을 만들 때 사용한다.
구성요소
build.gradle 에 아래와 같이 등록한다.
dependencies {
...
implementation 'androidx.recyclerview:recyclerview:1.2.1'
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView">
</androidx.recyclerview.widget.RecyclerView>
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_root"
android:orientation="horizontal"
android:padding="16dp">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="16dp"
android:id="@+id/item_data"/>
</androidx.appcompat.widget.LinearLayoutCompat>
class MyViewHolder(val binding: ItemMainBinding): RecyclerView.ViewHolder(binding.root)
class MyAdapter(val datas:MutableList<String>): RecyclerView.Adapter<RecyclerView.ViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return MainActivity.MyViewHolder(
ItemMainBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
Log.d("lumineko", "onBindViewHolder $position")
val binding = (holder as MainActivity.MyViewHolder).binding
// 뷰 데이터 출력
binding.itemData.text = datas[position]
// 뷰 이벤트 추가
binding.itemRoot.setOnClickListener{
Log.d("lumineko", "click $position")
}
}
override fun getItemCount(): Int {
return datas.size
}
}
val datas = mutableListOf<String>()
for(i in 1..10) {
datas.add("item $i")
}
binding.recyclerView.layoutManager = LinearLayoutManager(this)
binding.recyclerView.adapter = MyAdapter(datas)
binding.recyclerView.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL))
datas.add("new data")
adapter.notifyDataSetChanged()
binding.recyclerView.layoutManager = LinearLayoutManager(this)
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.HORIZONTAL
binding.recyclerView.layoutManager = layoutManager
val layoutManager = GridLayoutManager(this, 2)
binding.recyclerView.layoutManager = layoutManager
val layoutManager = GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false)
binding.recyclerView.layoutManager = layoutManager
val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
binding.recyclerView.layoutManager = layoutManager
class MyDecoration(val context : Context): RecyclerView.ItemDecoration(){
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
// 항목이 배치되기 전 호출
super.onDraw(c, parent, state)
c.drawBitmap(BitmapFactory.decodeResource(context.resources, R.drawable.stadium), 0f, 0f, null)
}
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
// 항목이 모두 배치 된 후 호출
super.onDrawOver(c, parent, state)
val width = parent.width
val height = parent.height
val dr: Drawable? = ResourcesCompat.getDrawable(context.resources, R.drawable.kbo, null)
val drWidth = dr?.intrinsicWidth
val drHeight = dr?.intrinsicHeight
val left = width/2 - drWidth?.div(2) as Int
val top = height/2 - drHeight?.div(2) as Int
c.drawBitmap(BitmapFactory.decodeResource(context.resources, R.drawable.kbo), left.toFloat(), top.toFloat(), null)
}
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
// 개별 항목을 꾸밀 때 호출
super.getItemOffsets(outRect, view, parent, state)
val index = parent.getChildAdapterPosition(view) + 1
if(index % 3 == 0)
outRect.set(10,10,10,60)
else
outRect.set(10,10,10,10)
view.setBackgroundColor(Color.LTGRAY)
ViewCompat.setElevation(view, 20.0f)
}
}
binding.recyclerView.addItemDecoration(MyDecoration(this))