일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 코틀린 문자열
- 코틀린
- 컬렉션 함수
- add view
- 코틀린 람다함수
- 객체의 동일성
- Kotlin
- 코틀린 lateinit
- kotlin listview
- 내용의동일성
- 코틀린 data class
- class
- kakao blind
- kotlin recyclerview
- 해시
- collection function
- 프로그래머스 #탐욕법 #큰수만들기 #join #python
- 컬렉션함수
- kotlin collection
- Kotlin Generic
- 카카오 순위검색
- programmers # 프로그래머스 #큰수비교 #cmp_to_key()
- 코틀린 내부 클래스
- isNullOrBlank
- 코틀린 컬렉션함수
- 코틀린 중첩클래스
- 프로그래머스
- 코틀린 제너릭
- kotlin addview
- lateinit
- Today
- Total
엔지니어 규의 IT 프로그래밍 다이어리
Android List View(1) - Add view 본문
안드로이드 에서 리스트 뷰 란 유사하게 반복되는 뷰를 그리기 위한 도구이다.
리스트뷰를 그리는 방법은 실제로 여러가지 인데 addview, Listview, Recyclerview 방식이 있는데
첫번째로 addview 방식은 실제로 리스트뷰를 그리기위해서는 잘 사용되지 않으며,
두번째로 Listview 는 예전에 많이 사용되었으며,
세번째로 RecyclerView 는 최근에 가장 많이 사용되고 있고 가장 효율이 높다.
Listview와 Recyclerview 는 addview 방식을 효과적으로 만든것이므로,
addview 방식을 잘 알아야 Listview, Recyclerview를 잘 알 수 있다.
궁극적인 우리의 목적은 결국은 Recyclerview 를 만드는 것이나 이해를 돕기위해 Addview 방식을 배워보고자 한다.
위 처럼 scroll 이되는 listview 를 addview 방식으로 구현해보자.
<AddviewActivity 코드>
class AddviewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val carList = ArrayList<CarForList>()
for(i in 0 until 20){
carList.add(CarForList( "" + i + "번쨰자동차","" + i + "번째 순위 엔진"))
}
val container = findViewById<LinearLayout>(R.id.addview_container)
val inflater = LayoutInflater.from(this@AddviewActivity)
for (i in 0 until carList.size ){
val itemview =inflater.inflate(R.layout.item_view, null)
val carNameView = itemview.findViewById<TextView>(R.id.car_name)
val carEngineView = itemview.findViewById<TextView>(R.id.car_engine)
carNameView.setText(carList.get(i).name)
carEngineView.setText(carList.get(i).engine)
container.addView(itemview)
}
}
}
class CarForList(val name: String, val engine: String){
}
|
cs |
<activitymain xml>
<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=".AddviewActivity"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addview_container"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
|
cs |
<itemview xml>
<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>
|
cs |

위와 같은 모식도를 보면 이해가 좀 편할것이다.
쉽게 정리하면
carNameView, carEngineView 에 for 문을돌려 itemview의 car_name, car_engine 을 지속적으로 update 해서 값을 넣고,
val inflater 에서 이것을 val itemview 에 지속적으로 inflate 해주고,
itemview 에서는 이렇게 추가가 되는 itemview를 val container에 addview 하면,
val container 는 activity_main.xml 의 addview_conatiner를 받고 있기 때문에
우리는 activity_main.xml 의 addview_container 에서 view 가 add 된것을 확인할 수 있다.
addview 는 화면을 넘어가면 스크롤 하는 기능이 자동으로 완성되지 않으므로,
ScrollView 안에 addview_container 를 넣어줘야 한다.
상당히 복잡해보이지만 차근차근 보면 다 이해할 수 있다.
다음에는 ListView 를 알아보자.
'안드로이드 앱개발 with Kotlin > 안드로이드 프레임 워크' 카테고리의 다른 글
Android List View(2) - List view (0) | 2022.07.10 |
---|