안드로이드 프로그래밍[Kotiln Code]/안드로이드 초기 강좌

[안드로이드] 리싸이클러뷰 4 ( 버튼, 체크박스 연동)

훈츠 2020. 4. 13. 21:18
반응형

안녕하세요. 훈츠 입니다. 리싸이클러뷰에 안에서 버튼 과 체크박스를 연동해서 사용하는 방법에 대해 포스팅합니다. 


리싸이클러 뷰 

리싸이클러뷰 안에 체크박스 와 버튼 등을 넣는 방법입니다. 

  1. viewHolder 안에 button 과 checkBox 를 찾습니다. 

  2. 어댑터 뷰홀더에서 찾은 button 과 checkBox 에 익명 함수로, 오버라이드 리스너 등록을 합니다.

코 드

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
//훈스 블로그---------------------------------------------------------------------------------------------------코드//
class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView) , LayoutContainer
{
    val btn  = containerView.findViewById(R.id.button) as Button
    val chkBox  = containerView.findViewById(R.id.checkBox) as CheckBox
}
 
 
class Adapter(val list: MutableList<Model>,val layout:Int,val context:Context) : RecyclerView.Adapter<ViewHolder>(){
    val selectionList = mutableListOf<Long>()
    val onItemClickListener : ((MutableList<Long>-> Unit)?= null
 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        view.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                val id = v?.tag
                if(selectionList.contains(id)) selectionList.remove(id)
                else selectionList.add(id as Long)
                notifyDataSetChanged()
                onItemClickListener?.let{it(selectionList)}
            }
        })
        return ViewHolder(view)
    }
 
    override fun getItemCount(): Int {
        return list.count()
    }
 
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }
 
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.containerView.textView.text = list[position].name
        holder.containerView.textView2.text = list[position].number.toString()
        holder.containerView.tag = getItemId(position)
        holder.containerView.isActivated = selectionList.contains(getItemId(position))
 
        holder.btn.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                Log.d("sss","Test btn${list[position].number}")
                if(list.isNotEmpty())
                list.remove(list[position])
                notifyDataSetChanged()
            }
        })
        holder.chkBox.setOnCheckedChangeListener(object :CompoundButton.OnCheckedChangeListener{
            override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
 
                if(isChecked){
                    Log.d("sss","Check btn ON${list[position].number}")
                    Toast.makeText(context,"Check btn ON${list[position].number}",Toast.LENGTH_LONG).show()
 
                }else {
                    Log.d("sss","Check btn OFF${list[position].number}")
                    Toast.makeText(context,"Check btn OFF${list[position].number}",Toast.LENGTH_LONG).show()
 
                }
            }
        })
 
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

코드 실행화면

https://youtu.be/9CkVoJ48LGU

코드 공유

https://github.com/rain2002kr/recycler_exam4.git

 

반응형