반응형
[Gradle 설정]
1
|
id 'kotlin-android-extensions'
|
cs |
<MainActivity>
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
|
package com.cookandroid.project11_2
import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.Gallery
import android.widget.ImageView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toast1.view.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var galAdapter = MyGalleryAdapter(this)
gallery1.adapter = galAdapter
}
inner class MyGalleryAdapter(var context: Context) : BaseAdapter(){
var posterID = arrayOf(
R.drawable.mov11, R.drawable.mov12, R.drawable.mov13, R.drawable.mov14,
R.drawable.mov15, R.drawable.mov16, R.drawable.mov17, R.drawable.mov18,
R.drawable.mov19, R.drawable.mov20,
)
var posterName = arrayOf(
"영화 제목1", "영화 제목2", "영화 제목3", "영화 제목4",
"영화 제목5", "영화 제목6", "영화 제목7", "영화 제목8",
"영화 제목9", "영화 제목10",
)
override fun getCount(): Int {
return posterID.size
}
override fun getItem(position: Int): Any {
return 0
}
override fun getItemId(position: Int): Long {
return 0;
}
@SuppressLint("ClickableViewAccessibility")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var imageview = ImageView(context)
imageview.layoutParams = Gallery.LayoutParams(200, 300)
imageview.scaleType = ImageView.ScaleType.FIT_CENTER
imageview.setPadding(5, 5, 5, 5)
imageview.setImageResource(posterID[position])
imageview.setOnTouchListener { v, event ->
ivPoster.scaleType = ImageView.ScaleType.FIT_CENTER
ivPoster.setImageResource(posterID[position])
var toast = Toast(this@MainActivity)
var toastView = View.inflate(this@MainActivity, R.layout.toast1, null)
toastView.toastText1.text = posterName[position]
toast.view = toastView
toast.show()
false
}
return imageview
}
}
}
|
cs |
<activity_main.xml>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?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:orientation="vertical">
<Gallery
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gallery1"
android:spacing="5dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ivPoster"
android:padding="20dp"/>
</LinearLayout>
|
cs |
<toast1.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
|
<?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:orientation="horizontal"
android:gravity="center"
android:background="#0000FF">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/movie_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:id="@+id/toastText1"
android:textColor="@color/white"
android:textSize="20dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/movie_icon"/>
</LinearLayout>
|
cs |
반응형
'안드로이드 프로그래밍 > 코틀린' 카테고리의 다른 글
[코틀린 안드로이드 프로그래밍] Spinner OnItemSelectedListener사용법 (0) | 2022.06.08 |
---|---|
[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-3 (0) | 2022.06.08 |
[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-1 (0) | 2022.06.08 |
[코틀린을 활용한 안드로이드 프로그래밍] 9장 직접 풀어보기 9-3 (0) | 2022.05.26 |
[코틀린을 활용한 안드로이드 프로그래밍] 9장 직접 풀어보기 9-2 (0) | 2022.05.26 |