막무가내 삽질 블로그

리싸이클러뷰 여러 아이템 타입 나누기(Multiple Content Types) 본문

Android

리싸이클러뷰 여러 아이템 타입 나누기(Multiple Content Types)

joong~ 2021. 3. 29. 16:23
728x90

서버에서 받아온 데이터를 화면에 뿌려줘야한다. 

받아온 데이터들은 서로 다른 아이템들이다.

 

1. 사용가능한 이용권

2. 정지중인 이용권

3. 만료된 이용권

 

위 3가지를 화면에 뿌려줘야한다.

 

 

 

간단하게 샘플 데이터를 통해 만들어 봤다.

 

 

아이템 만들기

interface Item

data class ItemLeft(
    val height: String
) : Item

data class ItemRight(
    val title: String,
    val address: String
) : Item

data class ItemCenter(
    val company: String,
    val age: String,
    val position: String
) : Item

data class ItemImage(
    val image: Int,
    val desc: String
) : Item

 

 

어댑터 만들기

class MultiViewTypeAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val items = mutableListOf<Item>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = when (viewType) {
        TYPE_LEFT -> {
            LeftHolder.create(parent)
        }
        TYPE_RIGHT -> {
            RightHolder.create(parent)
        }
        TYPE_CENTER -> {
            CenterHolder.create(parent)
        }
        TYPE_IMAGE -> {
            ImageHolder.create(parent)
        }
        else -> {
            throw IllegalStateException("Not Found ViewHolder Type $viewType")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is LeftHolder -> {
                holder.bind(items[position] as ItemLeft)
            }
            is RightHolder -> {
                holder.bind(items[position] as ItemRight)
            }
            is CenterHolder -> {
                holder.bind(items[position] as ItemCenter)
            }
            is ImageHolder -> {
                holder.bind(items[position] as ItemImage)
            }
        }
    }

    override fun getItemCount() = items.size

    override fun getItemViewType(position: Int) = when (items[position]) {
        is ItemLeft -> {
            TYPE_LEFT
        }
        is ItemRight -> {
            TYPE_RIGHT
        }
        is ItemCenter -> {
            TYPE_CENTER
        }
        is ItemImage -> {
            TYPE_IMAGE
        }
        else -> {
            throw IllegalStateException("Not Found ViewHolder Type")
        }
    }

    class LeftHolder(private val itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val textView = itemView.findViewById<TextView>(R.id.tv_left)

        fun bind(item: ItemLeft) {
            textView.text = item.height
        }

        companion object Factory {
            fun create(parent: ViewGroup): LeftHolder {
                val layoutInflater = LayoutInflater.from(parent.context)
                val view = layoutInflater.inflate(R.layout.item_holder_left, parent, false)

                return LeftHolder(view)
            }
        }
    }

    class RightHolder(private val itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvOne = itemView.findViewById<TextView>(R.id.tv_right_one)
        private val tvTwo = itemView.findViewById<TextView>(R.id.tv_right_two)

        fun bind(item: ItemRight) {
            tvOne.text = item.title
            tvTwo.text = item.address
        }

        companion object Factory {
            fun create(parent: ViewGroup): RightHolder {
                val layoutInflater = LayoutInflater.from(parent.context)
                val view = layoutInflater.inflate(R.layout.item_holder_right, parent, false)

                return RightHolder(view)
            }
        }
    }

    class CenterHolder(private val itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvOne = itemView.findViewById<TextView>(R.id.tv_center_one)
        private val tvTwo = itemView.findViewById<TextView>(R.id.tv_center_two)
        private val tvThree = itemView.findViewById<TextView>(R.id.tv_center_three)

        fun bind(item: ItemCenter) {
            tvOne.text = item.age
            tvTwo.text = item.company
            tvThree.text = item.position
        }

        companion object Factory {
            fun create(parent: ViewGroup): CenterHolder {
                val layoutInflater = LayoutInflater.from(parent.context)
                val view = layoutInflater.inflate(R.layout.item_holder_center, parent, false)

                return CenterHolder(view)
            }
        }
    }

    class ImageHolder(private val itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val imageView = itemView.findViewById<ImageView>(R.id.image_view)
        private val textView = itemView.findViewById<TextView>(R.id.tv_desc)

        fun bind(item: ItemImage) {
            imageView.setImageResource(item.image)
            textView.text = item.desc
        }

        companion object Factory {
            fun create(parent: ViewGroup): ImageHolder {
                val layoutInflater = LayoutInflater.from(parent.context)
                val view = layoutInflater.inflate(R.layout.item_holder_image, parent, false)

                return ImageHolder(view)
            }
        }
    }

    fun addItems(item: Item) {
        this.items.add(item)
        this.notifyDataSetChanged()
    }

    companion object {
        private const val TYPE_LEFT = 0
        private const val TYPE_RIGHT = 1
        private const val TYPE_CENTER = 2
        private const val TYPE_IMAGE = 3
    }
    
}

 

 

액티비티

class MultiViewTypeActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: MultiViewTypeAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_multiview_type)

        recyclerView = findViewById(R.id.recycler_view)
        lists = mutableListOf()

        initAdapter()

        setSampleData()
    }

    private fun initAdapter() {
        adapter = MultiViewTypeAdapter()
        recyclerView.adapter = adapter
    }

    private fun setSampleData() {
        adapter.addItems(ItemLeft("180"))
        adapter.addItems(ItemRight("하이", "지구"))
        adapter.addItems(ItemCenter("123", "18", "여기"))
        adapter.addItems(ItemImage(R.drawable.sample_icon_1, "설명1"))
        adapter.addItems(ItemLeft("170"))
        adapter.addItems(ItemRight("설명", "별"))
        adapter.addItems(ItemCenter("456", "28", "저기"))
        adapter.addItems(ItemImage(R.drawable.sample_icon_2, "설명2"))
        adapter.addItems(ItemLeft("190"))
        adapter.addItems(ItemRight("안녕", "달"))
        adapter.addItems(ItemCenter("456", "28", "저기"))
        adapter.addItems(ItemImage(R.drawable.sample_icon_3, "설명3"))
    }

}

 

 

 

참:blog.jakelee.co.uk/creating-a-recyclerview-with-multiple-content-types-and-layouts-in-kotlin/

 

 

 

 

전체소스

github.com/wj1227/Android-Sample-Test/tree/main/app/src/main/java/com/jay/androidallsampletest/multiviewtype

 

wj1227/Android-Sample-Test

Android Test App. Contribute to wj1227/Android-Sample-Test development by creating an account on GitHub.

github.com

 

Comments