๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | ๋ฉ€ํ‹ฐ๋ทฐํƒ€์ž…

hyihyiยท2024๋…„ 1์›” 28์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
42/70

๋ฉ€ํ‹ฐ๋ทฐํƒ€์ž…์ด๋ž€?

๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ์˜ ๊ฐ ํ•ญ๋ชฉ์ด ์„œ๋กœ ๋‹ค๋ฅธ ๋ ˆ์ด์•„์›ƒ์„ ๊ฐ€์ ธ์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ

<์˜ˆ์‹œ>
1. ์ฑ„ํŒ…

๋‚ด๊ฐ€ ๋ณด๋‚ธ ๋ฉ”์‹œ์ง€์™€ ์ƒ๋Œ€๋ฐฉ์ด ๋ณด๋‚ธ ๋ฉ”์‹œ์ง€๋ฅผ ๊ตฌ๋ถ„ํ•ด์•ผ ํ•  ๋•Œ
๋‚ด๊ฐ€ ๋ณด๋‚ธ ๋ฉ”์‹œ์ง€๋Š” ํ™”๋ฉด์˜ ์˜ค๋ฅธ์ชฝ์—, ์ƒ๋Œ€๋ฐฉ์ด ๋ณด๋‚ธ ๋ฉ”์‹œ์ง€๋Š” ํ™”๋ฉด ์™ผ์ชฝ์— ๋ฐฐ์น˜ํ•ด์•ผ ํ•œ๋‹ค. 
์†ก์‹ ์ž์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ทฐํƒ€์ž…์„ ์ ์šฉํ•ด ๋‹ค๋ฅธ ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•œ๋‹ค. 
  1. ๋‰ด์Šค
๊ธฐ์‚ฌ ๋ชฉ๋ก
๊ธฐ์‚ฌ์— ํ…์ŠคํŠธ๋งŒ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ํ…์ŠคํŠธ๋งŒ ํ‘œ์‹œํ•˜๊ณ , ๋‹ค๋ฅธ ๊ธฐ์‚ฌ๋Š” ์ด๋ฏธ์ง€๋‚˜ ๋™์˜์ƒ ์ธ๋„ค์ผ์ด ์žˆ์„ ๋•Œ

๋ทฐํƒ€์ž…์ด๋ž€?

๊ฐ ํ•ญ๋ชฉ์ด ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ํ‘œ์‹œ๋˜๋Š”์ง€๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ์‹

๋ฉ€ํ‹ฐ ๋ทฐํƒ€์ž… ๊ตฌํ˜„ ๊ณผ์ •

1. RecyclerView์™€ Adapter ์„ค์ •

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>

2) ์–ด๋Œ‘ํ„ฐ ํด๋ž˜์Šค ์ƒ์„ฑ

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) }
        }
    }
}

2. ๋ฐ์ดํ„ฐ ๋ชจ๋ธ ์ƒ์„ฑ

data class MyData(val type: String, val text: String? = null, val imageResId: Int? = null)

3. ๋ ˆ์ด์•„์›ƒ ํŒŒ์ผ ์ƒ์„ฑ

๊ฐ ๋ทฐํƒ€์ž…์— ๋งž๋Š” ๋ ˆ์ด์•„์›ƒ ํŒŒ์ผ ์ƒ์„ฑ
ํ…์ŠคํŠธ๋งŒ ์žˆ๋Š” ๋ ˆ์ด์•„์›ƒ๊ณผ ์ด๋ฏธ์ง€๋งŒ ์žˆ๋Š” ๋ ˆ์ด์•„์›ƒ 2๊ฐœ ์ƒ์„ฑ

ํ…์ŠคํŠธ ์•„์ดํ…œ ๋ ˆ์ด์•„์›ƒ (item_text.xml)

<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>

์ด๋ฏธ์ง€ ์•„์ดํ…œ ๋ ˆ์ด์•„์›ƒ (item_image.xml)

<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>

4. MainActivity์—์„œ RecyclerView ์„ค์ •

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
    }
}

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€