๐Ÿ“ฆ Chango/๐Ÿฃ EDOC

[Android Studio / Kotlin] ์ฝ”ํ‹€๋ฆฐ๊ณผ ๋ฆฌ์ŠคํŠธ๋ทฐ (List View)

์„ ๋‹ฌ 2021. 5. 2. 04:20
๋ฐ˜์‘ํ˜•

๋ฆฌ์ŠคํŠธ๋ทฐ,, ๋ฆฌ์ŠคํŠธ๋ทฐ,,,,, ๋ฆฌ์ŠคํŠธ๋ธ…ใ…‡ใ„ดใ…‡๋ฏธใ…;์ž„;!!!

www.youtube.com/watch?v=ao0Iqfhy0oo&list=PLC51MBz7PMywN2GJ53aF0UO5fnHGjW35a&index=5

 

 

1. ๊ฐ„๋‹จํ•˜์ง€๋งŒ ์ œ์•ฝ์ด ์žˆ๋Š” 1๋ฒˆ๋ฐฉ๋ฒ• (์•Œ์•„๋งŒ๋‘์ž)

๋”๋ณด๊ธฐ

๋ฐฐ์—ด๋‚ด์šฉ์€,, ์ตœ๊ทผ ๋ณธ ์›นํˆฐ ์ฃผ์ธ๊ณต๋“ค ์ด๋ฆ„..

override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sub)
        
        val item = arrayOf("์ •ํ™ฉ์ง€","์œ„์„ฑ์—ฐ","๋ฏธ์€","๊น€์‚ฌ๋Šฅ")
        //context : ํ•œ ์—‘ํ‹ฐ๋น„ํ‹ฐ์˜ ๋ชจ๋“  ์ •๋ณด๋ฅผ ๋‹ด๊ณ ์žˆ์Œ.
        
        listView.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, item)
        
     }

 

2. ์“ธ๋งŒํ•˜์ง€๋งŒ ๋ณต์žกํ•œ 2๋ฒˆ๋ฐฉ๋ฒ•

 

1) ListView ๋งŒ๋“ค๊ธฐ

 : ๋ฆฌ์ŠคํŠธ๋ทฐ๋ฅผ xml ํŒŒ์ผ์— ๋งŒ๋“ ๋‹ค

์ด๋ ‡๊ฒŒ

 

2) ๋ชจ๋ธ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ

: ์ฝ”ํ‹€๋ฆฐํŒŒ์ผ(.kt) ์ƒ์„ฑ

//ํด๋ž˜์Šค ๋ชจ๋ธ ๊ฐ์ฒด

class Content (val ๋ณ€์ˆ˜1: Int, val ๋ณ€์ˆ˜2: String)

๋ณ€์ˆ˜๋Š” ๋‹น์—ฐํžˆ ๋งŽ์•„๋„ ๋จ

์—ฌ๊ธฐ์„œ๋Š” Content ๋ผ๋Š” ์ด๋ฆ„์˜ ์ฝ”ํ‹€๋ฆฐ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ํด๋ž˜์Šค ์ด๋ฆ„๋„ Content๋กœ ํ•ด๋†จ๋‹ค.

 

3) ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ ๊พธ๋ฏธ๊ธฐ

: ๋ ˆ์ด์•„์›ƒ ์— ๋ฆฌ์†Œ์ŠคํŒŒ์ผ(.xml) ์ƒ์„ฑ

: ๋‹ค๋ฅธ ๋ฆฌ์†Œ์ŠคํŒŒ์ผ ๊พธ๋ฏธ๋“ฏ์ด ๊พธ๋ฏธ๋ฉด ๋œ๋‹ค.

 

> >๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ ํฌ๊ธฐ ์กฐ์ •

ConstraintLayout ํด๋ฆญ - Layout - Layout_width, Layout_height ํ•ญ๋ชฉ ์กฐ์ •

 

4) Adapter ๋งŒ๋“ค๊ธฐ

- ์ฝ”๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐœ๋…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž

- ๋ฆฌ์ŠคํŠธ๋ทฐ๋Š” ์–ด๋Žํ„ฐ ์—ฐ๊ฒฐ์„ ๊ผญํ•ด์•ผํ•˜๊ณ  ์•„๋ž˜ ํ•จ์ˆ˜๋“ค์„ ํ˜ธ์ถœํ•œ๋‹ค (๊ณ„์†)!

 

: ์ฝ”ํ‹€๋ฆฐํŒŒ์ผ(.kt) ์ƒ์„ฑ

class ContentAdapter (val context: Context, val ContentList : ArrayList<Content/*๋ชจ๋ธ๋ช…*/>) : BaseAdapter() {

}

ContentAdapter ์€ ์–ด๋Žํ„ฐ์˜ ์ด๋ฆ„

ContentList ๋Š” ๋ฐฐ์—ด์ด๋ฆ„

<>์•ˆ์˜ Content ๋Š” ๋ชจ๋ธ์ด๋ฆ„

 

** ์ค‘์š” : ์ด๋•Œ ๋‚˜์˜ค๋Š” ์˜ค๋ฅ˜์— ๋Œ€ํ•ด ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค๊ฐ€ ์ œ์•ˆํ•˜๋Š” ๋ชจ๋“  ํ•ด๊ฒฐ์ฑ…์„ ์ˆ˜ํ–‰ํ•˜๋ฉด (option + enter) ์•„๋ž˜์˜ ์ฝ”๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ํด๋ž˜์Šค ์•ˆ์— ๋งŒ๋“ค์–ด์ง„๋‹ค.

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    }

override fun getItem(position: Int): Any {
    }

override fun getItemId(position: Int): Long {
    }

override fun getCount(): Int {
    }

 

4-1. getView ํ•จ์ˆ˜ ์•ˆ์— ์“ธ ์ฝ”๋“œ

 

4-1-1. ๋ณ€์ˆ˜๊ฐ€ ํ…์ŠคํŠธ๋ฟ์ด๋ผ๋ฉด

 override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        // getView : "position" (์ธ๋ฑ์Šค์ฒ˜๋Ÿผ 0๋ถ€ํ„ฐ์‹œ์ž‘) ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜๋“ค์„ ๋งŒ๋“ ๋‹ค.

        val view: View = LayoutInflater.from(context).inflate(R.layout.๋ฆฌ์ŠคํŠธ์ด๋ฆ„, null)
        // ๋ฆฌ์†Œ์Šค.๋ ˆ์ด์•„์›ƒ."xmlํŒŒ์ผ"๋กœ๋ถ€ํ„ฐ ๋ทฐ๋ฅผ ๋ถ™์ด๋Š” ๊ณผ์ •
        // R.layout.๋ฆฌ์ŠคํŠธ์ด๋ฆ„ : ๋ฆฌ์†Œ์Šคํด๋”(res) - ๋ ˆ์ด์•„์›ƒํด๋”(layout) - "๋ฆฌ์ŠคํŠธ์ด๋ฆ„"์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฆฌ์ŠคํŠธ

        val ๋ณ€์ˆ˜1 = view.findViewById<TextView>(R.id."ํ…์ŠคํŠธ๋ทฐ์•„์ด๋””")
        val ๋ณ€์ˆ˜2 = view.findViewById<TextView>(R.id."ํ…์ŠคํŠธ๋ทฐ์•„์ด๋””")
        // "๋ณ€์ˆ˜" ๋Š” "๋ทฐ์•„์ด๋””" ๋กœ๋ถ€ํ„ฐ ๋ทฐ๋ฅผ ์ฐพ์•„๋ผ
        // <>์•ˆ์—๋Š” ํ˜•ํƒœ ex.TextView, ImageView...
      
        val content = ContentList[position]
        // val ๋ณ€์ˆ˜์ด๋ฆ„ = ๋ฐฐ์—ด์ด๋ฆ„[position]
        // ๋ฐฐ์—ด์˜ 'position+1' ๋ฒˆ์งธ ํ•ญ๋ชฉ
 
        ๋ณ€์ˆ˜1.text = content.๋ณ€์ˆ˜1
        ๋ณ€์ˆ˜2.text = content.๋ณ€์ˆ˜2
        //๋ณ€์ˆ˜.text ๋Š” content๋ชจ๋ธ์—์„œ ๋Œ์–ด์˜ค๋Š” ๋ณ€์ˆ˜

        return view
    }

4-1-2. ๋ณ€์ˆ˜์— ์ด๋ฏธ์ง€ ํฌํ•จํ•œ๋‹ค๋ฉด ๋‹ค์Œ ์ฝ”๋“œ ์‚ฌ์šฉ

val ๋ณ€์ˆ˜ = view.findViewById<ImageView>(R.id."์ด๋ฏธ์ง€๋ทฐ์•„์ด๋””")

์ด๋ฏธ์ง€.setImageResource(content.์ด๋ฏธ์ง€)

 

4-2. ๋‚˜๋จธ์ง€ ํ•จ์ˆ˜

 override fun getItem(position: Int): Any {
        return ContentList[position]
    }
 override fun getItemId(position: Int): Long {
        return 0
    }
    override fun getCount(): Int {
        return ContentList.size
    }

 

5. Adapter ๋ฅผ Activity ์— ์—ฐ๊ฒฐํ•˜๊ธฐ

5-1. ๋ฆฌ์ŠคํŠธ ์„ ์–ธ

: ๋ทฐ๋ฆฌ์ŠคํŠธ๊ฐ€ ํฌํ•จ๋œ .xml ์— ํฌํ•จ๋œ ์ฝ”ํ‹€๋ฆฐํŒŒ์ผ(.kt)์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ ์ž‘์„ฑ

//๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ธฐ

var ContentList = arrayListOf<๋ชจ๋ธํด๋ž˜์Šค์ด๋ฆ„>(
        ๋ชจ๋ธํด๋ž˜์Šค์ด๋ฆ„ (R.drawable.์ด๋ฏธ์ง€, "์œ„์„ฑ์—ฐ", "๋ถˆ"),	//์ฝค๋งˆ ๋นผ๋จน์ง€๋ง๊ธฐ
        ๋ชจ๋ธํด๋ž˜์Šค์ด๋ฆ„(R.drawable.์ด๋ฏธ์ง€, "๋ฏธ์€", "๋‚ฉ"),	//๋ณ€์ˆ˜ ์ˆœ์„œ ๋งž์ถ”๊ธฐ ์ฃผ์˜
        ๋ชจ๋ธํด๋ž˜์Šค์ด๋ฆ„(R.drawable.์ด๋ฏธ์ง€, "์ •ํ™ฉ์ง€", "๋‹ค์žฌ๋‹ค๋Šฅ")	//์ฝค๋งˆ ์•ˆ๋ถ™์ž„ ์ฃผ์˜
       )

์—ฌ๊ธฐ์„œ command + click ์œผ๋กœ ๋ชจ๋ธํด๋ž˜์Šค์ด๋ฆ„ ํด๋ฆญํ•˜๋ฉด ๋ชจ๋ธํด๋ž˜์Šค๋กœ ๋„˜์–ด๊ฐ„๋‹ค -> ์ž˜์—ฐ๊ฒฐ๋จ

 

5-2. ๋ฆฌ์ŠคํŠธ ํ™œ์šฉ

: override fun onCreate ํ•จ์ˆ˜ ์•ˆ์—์„œ ํ™œ์šฉ !

 val Adapter = ์–ด๋Žํ„ฐํด๋ž˜์Šค์ด๋ฆ„(this, ContentList)	
  //์—ฌ๊ธฐ์„œ ์–ด๋Žํ„ฐ์ชฝ์œผ๋กœ ์˜๋ฉด ์–ด๋Žํ„ฐ ํด๋ž˜์Šค์—์„œ val ContentList ์ชฝ์œผ๋กœ ๋ฐ›์•„์„œ ๋“ฑ๋ก๋˜๋Š” ์›๋ฆฌ

 listView.adapter = Adapter
 //์–ด๋Žํ„ฐ ์‹คํ–‰
        

 

6. ๊ฐ ๋ฆฌ์ŠคํŠธ ํด๋ฆญํ–ˆ์„ ๋•Œ ํŠน์ • ์ผ ํ•˜๋„๋ก ๋งŒ๋“ค๊ธฐ

์—ฌ๊ธฐ์„œ๋Š” ํ† ์ŠคํŠธํŒ์—…๋œจ๋Š” ๊ฒƒ๋งŒ ๊ตฌํ˜„ํ•จ

๋ฆฌ์ŠคํŠธ๋ทฐ์ด๋ฆ„.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
//ํŠน์ • ๋ฆฌ์ŠคํŠธ ํด๋ฆญ ์‹œ ์‹คํ–‰

            val selection = parent.getItemAtPosition(position) as Content
            //ํด๋ฆญํ•œ ํฌ์ง€์…˜์„ ๊ฐ€์ง„ ๋ณ€์ˆ˜๊ฐ€ ๋งŒ๋“ค์–ด์ง
            
            Toast.makeText(this, "${selection.๋ณ€์ˆ˜1}", Toast.LENGTH_SHORT).show()
            //ํ† ์ŠคํŠธ ํŒ์—…์œผ๋กœ ํด๋ฆญํ•œ ๋ฆฌ์ŠคํŠธ์˜ ๋ณ€์ˆ˜๊ฐ’์ด ๋ฟ…ํ•˜๊ณ  ๋‚˜์˜ด
            
        }

 

 

๋ฐ˜์‘ํ˜•