๋ฆฌ์ฌ์ดํด๋ฌ๋ทฐ์ ๊ฐ ํญ๋ชฉ์ด ์๋ก ๋ค๋ฅธ ๋ ์ด์์์ ๊ฐ์ ธ์ผ ํ ๋ ์ฌ์ฉ
<์์>
1. ์ฑํ
๋ด๊ฐ ๋ณด๋ธ ๋ฉ์์ง์ ์๋๋ฐฉ์ด ๋ณด๋ธ ๋ฉ์์ง๋ฅผ ๊ตฌ๋ถํด์ผ ํ ๋
๋ด๊ฐ ๋ณด๋ธ ๋ฉ์์ง๋ ํ๋ฉด์ ์ค๋ฅธ์ชฝ์, ์๋๋ฐฉ์ด ๋ณด๋ธ ๋ฉ์์ง๋ ํ๋ฉด ์ผ์ชฝ์ ๋ฐฐ์นํด์ผ ํ๋ค.
์ก์ ์์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ทฐํ์
์ ์ ์ฉํด ๋ค๋ฅธ ๋ ์ด์์์ ์ฌ์ฉํ๋ค.
๊ธฐ์ฌ ๋ชฉ๋ก
๊ธฐ์ฌ์ ํ
์คํธ๋ง ์์ ๊ฒฝ์ฐ์๋ ํ
์คํธ๋ง ํ์ํ๊ณ , ๋ค๋ฅธ ๊ธฐ์ฌ๋ ์ด๋ฏธ์ง๋ ๋์์ ์ธ๋ค์ผ์ด ์์ ๋
๊ฐ ํญ๋ชฉ์ด ํ๋ฉด์ ์ด๋ป๊ฒ ํ์๋๋์ง๋ฅผ ์ ์ํ๋ ๋ฐฉ์
activity_main.xml ํ์ผ์์ RecyclerView๋ฅผ ๋ฐฐ์นํ๋ค.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
RecyclerView์ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๊ณ ๊ฐ ํญ๋ชฉ์ด ํ๋ฉด์ ์ด๋ป๊ฒ ํ์๋ ์ง๋ฅผ ์ ์ํ๋ค.
class MyAdapter(private val data: List<MyData>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val VIEW_TYPE_TEXT = 1
const val VIEW_TYPE_IMAGE = 2
}
override fun getItemViewType(position: Int): Int {
return when (data[position].type) {
"text" -> VIEW_TYPE_TEXT
"image" -> VIEW_TYPE_IMAGE
else -> throw IllegalArgumentException("Invalid type of data at position $position")
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
VIEW_TYPE_TEXT -> {
val binding = ItemTextBinding.inflate(LayoutInflater.from(parent.context), parent, false)
TextViewHolder(binding)
}
VIEW_TYPE_IMAGE -> {
val binding = ItemImageBinding.inflate(LayoutInflater.from(parent.context), parent, false)
ImageViewHolder(binding)
}
else -> throw IllegalArgumentException("Invalid view type")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = data[position]
when (holder) {
is TextViewHolder -> holder.bind(item)
is ImageViewHolder -> holder.bind(item)
}
}
override fun getItemCount(): Int = data.size
class TextViewHolder(private val binding: ItemTextBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(data: MyData) {
binding.textView.text = data.text
}
}
class ImageViewHolder(private val binding: ItemImageBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(data: MyData) {
data.imageResId?.let { binding.imageView.setImageResource(it) }
}
}
}
data class MyData(val type: String, val text: String? = null, val imageResId: Int? = null)
๊ฐ ๋ทฐํ์
์ ๋ง๋ ๋ ์ด์์ ํ์ผ ์์ฑ
ํ
์คํธ๋ง ์๋ ๋ ์ด์์๊ณผ ์ด๋ฏธ์ง๋ง ์๋ ๋ ์ด์์ 2๊ฐ ์์ฑ
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
class MainActivity : AppCompatActivity() {
// ๋ทฐ๋ฐ์ธ๋ฉ ๊ฐ์ฒด ์ ์ธ
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ๋ทฐ๋ฐ์ธ๋ฉ ์ด๊ธฐํ
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// RecyclerView ์ค์
binding.recyclerView.layoutManager = LinearLayoutManager(this)
// ์ํ ๋ฐ์ดํฐ ์์ฑ
val data = listOf(
MyData(type = "text", text = "This is a text item"),
MyData(type = "image", imageResId = R.drawable.sample_image),
MyData(type = "text", text = "Another text item"),
MyData(type = "image", imageResId = R.drawable.sample_image)
)
// ์ด๋ํฐ ์ค์
val adapter = MyAdapter(data)
binding.recyclerView.adapter = adapter
}
}