Home 6월 30일 Today I Learned
Post
Cancel

6월 30일 Today I Learned

오늘한 일

  • A·아이 프로젝트를 진행하였다
  • 블로그 TIL을 작성하였다
  • 1일 1커밋을 하였다

느낌 점

오늘까지 개발을 하며 느낀 것인데 이제는 슬슬 백엔드가 api를 만든 후 개발해야겠다는 생각이 들었다 내 스스로 목업 데이터를 넣는 것도 힘들고 어차피 나중에 실제 api로 교체하면서 여러 부분을 수정해야 되는데 지금 하는 것이 뭔가 비효율적이라는 생각이 든다

배운 점

Android 같은 adapter에서 두 가지 ViewHolder 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class ChildrenInfoAdapter(private val children: ArrayList<ChildInfo>, private val type: ChildInfoType): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            ChildInfoType.NEW.ordinal -> {
                val binding = NewChildrenInfoItemBinding.inflate(inflater, parent, false)
                NewChildrenInfoViewHolder(binding).also {
                    binding.apply {
                        newChildrenInfoItem.setOnClickListener {

                        }
                    }
                }
            }
            ChildInfoType.DEFAULT.ordinal -> {
                val binding = ChildrenInfoItemBinding.inflate(inflater, parent, false)
                ChildrenInfoViewHolder(binding).also {
                    binding.apply {
                        viewDetailsButton.setOnClickListener {

                        }
                    }
                }
            }
            else -> throw IllegalArgumentException("Invalid view type")
        }

    }
    override fun getItemCount(): Int {
        return when (type) {
            ChildInfoType.NEW -> {
                3
            }

            ChildInfoType.DEFAULT -> {
                children.size
            }
            else -> throw IllegalArgumentException("Invalid view type")
        }
    }

    override fun getItemViewType(position: Int): Int {
        return type.ordinal
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val childInfo = children[position]
        when (holder) {
            is NewChildrenInfoViewHolder -> {
                holder.bind(childInfo)
            }
            is ChildrenInfoViewHolder -> {
                holder.bind(childInfo)
            }
        }
    }

    class NewChildrenInfoViewHolder(private val binding: NewChildrenInfoItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(childrenInfo: ChildInfo) {
            binding.apply {
                // 내용
            }
        }
    }

    class ChildrenInfoViewHolder(private val binding: ChildrenInfoItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(childrenInfo: ChildInfo) {
            binding.apply {
                // 내용
            }
        }
    }
}

안드로이드 앱을 만들다가 문득 ChildrenInfoAdapter와 새로 만들려는 NewwChildrenInfoAdapter는 둘이 비슷한 역할을 하는데 adapter를 굳이 두 가지를 만들어야 할까? 라는 생각이 들었다 그래서 위와 같이 type에 따라 다른 ViewHolder가 선택되는 adapter를 개발하였다

지금은 2개일 때 예시지만 더 많이도 할 수 있을 거라는 생각이 든다 하지만 너무 많아지거나 불필요하게 하나로 합친다면 코드 가독성이 떨어질 거 같으니 적절한 상황에만 사용해야겠다

내일 계획

내일은 A·아이 프로젝트를 진행해야겠다

This post is licensed under CC BY 4.0 by the author.

6월 29일 Today I Learned

7월 1일 Today I Learned