본문 바로가기

Android/Kotlin

[Kotlin] RecyclerView에 CustomDialog 추가하기

이전에 recyclerview를 만들어보았다.

여기에 클릭이벤트를 추가해서 미리 만들어놓은 xml을 띄어보려고 한다.

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
 itemView.setOnClickListener {
                val dialogView = LayoutInflater.from(con).inflate(R.layout.selectchrdialog, null , false)
                val alertDialog = AlertDialog.Builder(con)
                    .setView(dialogView)
                    .create()
 
                val btnstart : Button = dialogView.findViewById(R.id.startgame)
                val btnnostart : Button = dialogView.findViewById(R.id.nostartgame)
                val chrname : TextView = dialogView.findViewById(R.id.charname)
                val chrimg1 : ImageView = dialogView.findViewById(R.id.charimg1)
                val chrprofile : TextView = dialogView.findViewById(R.id.chrprofile)
 
                chrname.setText(item.name)
                chrimg1.setImageResource((item.profile))
                chrprofile.setText(item.story)
                //버튼 클릭시 다음 액티비티로 넘어가고 data를 넘겨준다.
                btnstart.setOnClickListener{
                    alertDialog.dismiss()
                    val intent1 = Intent(con, GameActivity ::class.java)
                    dialogView.getContext().startActivity(intent1)
                    intent1.putExtra("Data",item)
                    activity.finish()
 
                }
                //다이얼로그 종료 버튼
                btnnostart.setOnClickListener{
                    alertDialog.dismiss()
                }
                alertDialog.show()
                //다이얼로그 너비 조정
                alertDialog.window?.setLayout(630, WindowManager.LayoutParams.WRAP_CONTENT)
                //외부 클릭시 다이얼로그 종료 방지
                alertDialog.setCancelable(false)
            }
cs

위 코드를 viewholder에 fun bind 안에 추가했다.

adapter와 data class의 전체 코드

 

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
76
77
78
79
 
class Data(val profile:Int, val name:String, val story:String) : Serializable
 
class SelectchrAdapter(val DataList:ArrayList<Data>,var con : Context,val activity : Activity) : RecyclerView.Adapter<SelectchrAdapter.chrViewHolder> (){
 
    inner class chrViewHolder(v : View) : RecyclerView.ViewHolder(v){
        private  val profile: ImageView = itemView.findViewById(R.id.charimg)
        private  val text: TextView = itemView.findViewById(R.id.charname)
 
        fun bind(item:Data){  //바인드 함수를 집어넣어서 저장된 요소와 뷰와 바인딩을 하였다.
            text.text = item.name
            profile.setImageResource(item.profile)
            itemView.setOnClickListener {
                val dialogView = LayoutInflater.from(con).inflate(R.layout.selectchrdialog, null , false)
                val alertDialog = AlertDialog.Builder(con)
                    .setView(dialogView)
                    .create()
 
                val btnstart : Button = dialogView.findViewById(R.id.startgame)
                val btnnostart : Button = dialogView.findViewById(R.id.nostartgame)
                val chrname : TextView = dialogView.findViewById(R.id.charname)
                val chrimg1 : ImageView = dialogView.findViewById(R.id.charimg1)
                val chrprofile : TextView = dialogView.findViewById(R.id.chrprofile)
 
                chrname.setText(item.name)
                chrimg1.setImageResource((item.profile))
                chrprofile.setText(item.story)
                //버튼 클릭시 다음 액티비티로 넘어가고 data를 넘겨준다.
                btnstart.setOnClickListener{
                    alertDialog.dismiss()
                    val intent1 = Intent(con, GameActivity ::class.java)
                    dialogView.getContext().startActivity(intent1)
                    intent1.putExtra("Data",item)
                    activity.finish()
 
                }
                //다이얼로그 종료 버튼
                btnnostart.setOnClickListener{
                    alertDialog.dismiss()
                }
                alertDialog.show()
                //다이얼로그 너비 조정
                alertDialog.window?.setLayout(630, WindowManager.LayoutParams.WRAP_CONTENT)
                //외부 클릭시 다이얼로그 종료 방지
                alertDialog.setCancelable(false)
            }
 
            println("바인드함수")
 
 
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): chrViewHolder { // viewType 형태의 아이템 뷰를 위한 뷰홀더 객체 생성
        val cellForRow = LayoutInflater.from(parent.context).inflate(R.layout.selectchr,parent,false)
        println("온크리트뷰홀더")
        return chrViewHolder(cellForRow)
    }
 
    override fun onBindViewHolder(holder: chrViewHolder, position: Int) { //position에 해당하는 데이터를 뷰홀더의 아이템 뷰에 표시 얘는 내용을 수정하는 역할
        holder.bind(DataList[position])
        println("바인드뷰홀더")
    }
    override fun getItemCount(): Int { //전체 아이템 개수 리턴.
        return DataList.size
    }
}
 
class VerticalItemDecorator(private val Height : Int) : RecyclerView.ItemDecoration(){
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.top = Height
        outRect.bottom = Height
    }
}
cs

 

dialog를 클래스로 만들지 않고 recyclerview안에서 만들어서 다이얼로그 너비를 조정하는 부분이라던지 intent를 구현하는대 고생했다..

하지만 결국 구글링으로 극복

코드가 별로 좋아 보이진 않지만 원하던 기능은 작동한다.

다음에 만들땐 더 깔끔하게 해봐야겠다.