Android Application

Kotlin 어댑터 뷰(Adaptor View) : 그리드뷰(GridView)

작성자 임베디드코리아 작성일23-09-25 01:59 조회1,627회 댓글0건

첨부파일

------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.031"
        />
    <GridView
        android:id="@+id/grid"
        android:numColumns="auto_fit"
        android:columnWidth="100dp"
        android:layout_width="531dp"
        android:layout_height="773dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintVertical_bias="0.839"/>

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
 **    layout/item_spinner.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listItem"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:paddingTop="10dp"
    android:paddingStart="30dp"
    android:textColor="@android:color/darker_gray"
    android:textSize="15sp"
    android:paddingLeft="30dp"/>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    var items = arrayOf(
        "서울", "부산", "대구", "광주", "인천", "목포", "여수", "태백",
        "서울", "부산", "대구", "광주", "인천", "목포", "여수", "태백",
        "서울", "부산", "대구", "광주", "인천", "목포", "여수", "태백",
        "서울", "부산", "대구", "광주", "인천", "목포", "여수", "태백"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textView = findViewById<View>(R.id.textView) as TextView
        val grid = findViewById<View>(R.id.grid) as GridView

        grid.setAdapter(
            ArrayAdapter(this,R.layout.item_spinner, items)
            //layout폴더에 있는 item_spinner.xml파일과 지역명 적인 배열 items 연결하는
            //어댑터를 grid에 세팅
        )

        grid.setOnItemClickListener{parent, view, position,id ->
            textView.text = items[position]

            //textView에 선택된 지역명 표시되도록 연결
        }

    }
}