[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-2

2022. 6. 8. 20:21·안드로이드 프로그래밍/코틀린
반응형

[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
        }
 
    }
}
Colored by Color Scripter
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>
Colored by Color Scripter
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>
Colored by Color Scripter
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
'안드로이드 프로그래밍/코틀린' 카테고리의 다른 글
  • [코틀린 안드로이드 프로그래밍] Spinner OnItemSelectedListener사용법
  • [코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-3
  • [코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-1
  • [코틀린을 활용한 안드로이드 프로그래밍] 9장 직접 풀어보기 9-3
슥지니
슥지니
개발 블로그
  • 슥지니
    슥지니의 코딩노트
    슥지니
  • 전체
    오늘
    어제
    • 분류 전체보기 (199)
      • 알고리즘 문제풀이 (158)
        • 백준 (158)
      • 알고리즘 (6)
      • Node.js (2)
        • MongoDB (1)
        • 기타 (1)
      • spring (0)
      • 가상화폐 (1)
        • 바이낸스(Binance) (1)
      • C++ 테트리스 게임 (1)
      • C++ (10)
      • 안드로이드 프로그래밍 (21)
        • 코틀린 (21)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    자료구조
    구현
    시뮬레이션
    dfs
    BFS
    그리디
    C++
    그래프
    다이나믹 프로그래밍
    코틀린
    백트랙킹
    알고리즘
    dp
    우선순위 큐
    백준
    C
    코틀린을 활용한 안드로이드 프로그래밍
    콘솔
    콘솔 테트리스 게임
    Kotlin
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
슥지니
[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-2
상단으로

티스토리툴바