Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스 #탐욕법 #큰수만들기 #join #python
- 코틀린 제너릭
- 코틀린
- 객체의 동일성
- collection function
- add view
- 코틀린 내부 클래스
- 코틀린 컬렉션함수
- Kotlin Generic
- class
- 컬렉션함수
- lateinit
- 코틀린 람다함수
- 카카오 순위검색
- kotlin addview
- kotlin collection
- 컬렉션 함수
- Kotlin
- kotlin recyclerview
- 코틀린 문자열
- kotlin listview
- kakao blind
- programmers # 프로그래머스 #큰수비교 #cmp_to_key()
- isNullOrBlank
- 내용의동일성
- 코틀린 중첩클래스
- 코틀린 lateinit
- 프로그래머스
- 코틀린 data class
- 해시
Archives
- Today
- Total
엔지니어 규의 IT 프로그래밍 다이어리
Android List View(2) - List view 본문
728x90
이전 포스팅에서 addview 로 리스트뷰를 만드는 방법을 알아봤다.
복습차원에서 addview에 필요한 요소들을 정리해보자.
-addView-
1. 리스트로 만들고 싶은 아이템의 리스트를준비한다.
2. 인플레이터를 준비한다.
3. 인프레이터로 아이템 하나에 해당하는 뷰를 만들어준다.
4. 위에만든 뷰를 컨테이너뷰에 붙여준다.
ListView에 필요한 요소는 다음과 같다.
-ListView-
1. 리스트로 만들고 싶은 아이템의 리스트를 준비한다.
2. Adapter를 이용한다.
addview와 Listview 의 차이점 을 알아보자면
1. 만드는 방식이 다르다.
2. 그리는 방식이 다르다.
- Addview -> 리스트의 갯수와 상관없이 한번에 다 그린다.
- Listview -> 보여지는 화면 + alpha 만 한번에 그리고, 필요한 경우에 더 그린다.
예를들면 10000명의 전화번호부를 만들때,
Addview 는 10000명의 리스트갯수를 다 그린다고 치면 ,
ListView 는 화면에 보여지는 전화번호(예: 10개) 에 + alpha 를 adapter를 이용해 그려 놓음으로써
스크롤을 하게 되면 해당 view가 보이게 만든다.
다시 위와 같은 android 를 ListView 를 이용하여 그려보자.
package com.example.listview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import java.util.ArrayList
class ListViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.listview)
val carList = ArrayList<CarForList>()
for (i in 0 until 20) {
carList.add(CarForList("" + i + "번째 자동차", "" + i + "번째 엔진"))
}
val adapter = ListViewAdapter(carList, LayoutInflater.from(this@ListViewActivity))
val listview = findViewById<ListView>(R.id.listView)
listview.adapter = adapter
listview.setOnItemClickListener { parent, view, position, id ->
val carName = (adapter.getItem(position) as CarForList).name
val carEngine = (adapter.getItem(position) as CarForList).engine
Toast.makeText(this@ListViewActivity, " " + carEngine + "" + carName, Toast.LENGTH_LONG)
.show()
}
}
class ListViewAdapter(
val carForList: ArrayList<CarForList>,
val layoutInflater: LayoutInflater
) : BaseAdapter() {
override fun getCount(): Int {
//그리고자 하는 아이템의 전체 갯수
return carForList.size
}
override fun getItem(position: Int): Any {
//그리고자하는 아이템 포지션에 해당하는 리스트의 하나
return carForList.get(position)
}
override fun getItemId(position: Int): Long {
// 해당 포지션에 위치해 있는 아이템 뷰의 아이디 설정
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view: View
val holder: ViewHolder
if (convertView == null) {
view = layoutInflater.inflate(R.layout.item_view, null)
holder = ViewHolder()
holder.carName = view.findViewById(R.id.car_name)
holder.carEngine = view.findViewById(R.id.car_engine)
view.tag = holder
} else {
holder = convertView.tag as ViewHolder
view = convertView
}
holder.carName?.setText(carForList.get(position).name)
holder.carEngine?.setText(carForList.get(position).engine)
return view
}
}
class ViewHolder {
var carName: TextView? = null
var carEngine: TextView? = null
}
class CarForList(val name: String, val engine: String) {}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0FC9CF"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/car_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="6dp"
android:textSize="20dp">
</TextView>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#B53E6BDC">
</TextView>
<TextView
android:id="@+id/car_engine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp">
</TextView>
</LinearLayout>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="1dp">
</androidx.appcompat.widget.LinearLayoutCompat>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListViewActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:divider="@null">
</ListView>
</LinearLayout>
728x90
'안드로이드 앱개발 with Kotlin > 안드로이드 프레임 워크' 카테고리의 다른 글
Android List View(1) - Add view (0) | 2022.06.14 |
---|
Comments