본문 바로가기

Mobile/Android

[Android / Kotlin] 강좌1 - 쇼핑몰앱 레이아웃 잡기


[Android / Kotlin] 강좌1 -  쇼핑몰앱 레이아웃 잡기

프로젝트 생성 후 package 폴더 만들기(이름 model)


컨트롤러와 모델 분리하여주기


카테고리 클래스와 프로덕트 클래스 생성


Dataservice 오브젝트 생성(싱글톤 생성)

1//DataService.kt
2...............
3..............
4object DataService {
5 val categories = listOf(
6 Category("SHIRTS","shirtimage"),
7 Category("HOODIES","hoodieimage"),
8 Category("HATS","hatimage"),
9 Category("DIGITAL","digitalgoodimage")
10 )
11
12 var hats = listOf(
13 Product("", "1000", "hat01"),
14 Product("", "1500", "hat02"),
15 Product("", "2000", "hat03"),
16 Product("", "5500", "hat04")
17 )
18
19 var shirts = listOf(
20 Product("", "1000", "shirt01"),
21 Product("", "1500", "shirt02"),
22 Product("", "2000", "shirt03"),
23 Product(" ", "5500", "shirt04"),
24 Product(" ", "5900", "shirt05")
25 )
26}
27

서버와 통신하지 않으므로 데이터 셋을 직접 만들어주자.


카테고리 텍스트를 넣어준 후


리스트를 만들어주자

1class MainActivity : AppCompatActivity() {
2
3 //lazy initializing .
4 //lazy init
5 lateinit var adapter: ArrayAdapter<Category>
6
7
8 override fun onCreate(savedInstanceState: Bundle?) {
9 super.onCreate(savedInstanceState)
10 setContentView(R.layout.activity_main)
11 }
12}

그 후 조금전 만들어준 listView의 id를 categoryListView로 바꾸어 준 후

1//
2 adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1,DataService.categories)
3 categoryListView.adapter = adapter;


실행하면 메모리 위치값도 보인다... 다음으로 넘어가자

1//Category
2class Category(val title:String, val image: String){
3 override fun toString(): String{
4 return title
5 }
6}


정상출력


새로운 layout 만들기


이미지 추가후 레이아웃을 위처럼 변경

중앙의 텍스트 배치