사용된 Activity
package com.example.myapplication
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
class PhoneBookActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_phone_book)
val phoneBook = createFakePhoneBook(30)
createPhoneBookList(phoneBook)
// 줄이면 -> createPhoneBookList(createFakePhoneBook())
}
fun createFakePhoneBook(fakeNumber: Int = 10, phoneBook: PhoneBook = PhoneBook()):PhoneBook {
for (i in 0 until fakeNumber) {
phoneBook.addPerson(
Person(
name = ""+i+"번째 사람",
number = ""+i+"번째 사람의 전화번호"
)
)
}
return phoneBook
}
fun createPhoneBookList(phoneBook: PhoneBook){
val layoutInflater = LayoutInflater.from(this@PhoneBookActivity)
val container = findViewById<LinearLayout>(R.id.phonebook_list_container)
for(i in 0 until phoneBook.personList.size){
val view = layoutInflater.inflate(R.layout.phonebook_item, null)
val personNameView = view.findViewById<TextView>(R.id.person_name)
personNameView.setText(phoneBook.personList.get(i).name)
addSetOnClickListener(phoneBook.personList.get(i), view)
container.addView(view)
}
}
fun addSetOnClickListener(person: Person, view: View){
view.setOnClickListener {
val intent = Intent(this@PhoneBookActivity, PhoneBookDetailActivity::class.java)
intent.putExtra("name", person.name)
intent.putExtra("number", person.number)
startActivity(intent)
}
}
}
// 전화번호부
class PhoneBook() {
val personList = ArrayList<Person>();
fun addPerson(person: Person) {
personList.add(person)
}
}
class Person(val name: String, val number: String) {
}
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_phone_book_detail.*
class PhoneBookDetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_phone_book_detail)
getPersonInfoAndDraw()
back.setOnClickListener{
//뒤로가기
onBackPressed()
}
}
fun getPersonInfoAndDraw(){
val name = intent.getStringExtra("name")
val number = intent.getStringExtra("number")
person_detail_name.setText(name)
person_detail_number.setText(number)
}
}
사용된 XML
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context=".PhoneBookActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="전화번호부"
android:textSize="40dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/phonebook_list_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#605656"
android:orientation="vertical"
tools:context=".PhoneBookDetailActivity">
<ImageView
android:id="@+id/back"
android:src="@drawable/cat"
android:scaleType="centerCrop"
android:layout_width="50dp"
android:layout_height="50dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="16dp"
android:src="@drawable/man" />
<TextView
android:id="@+id/person_detail_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textColor="#ffffff"
android:textSize="50dp"
android:gravity="center"
android:layout_marginBottom="16dp"/>
<TextView
android:id="@+id/person_detail_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="01012341234"
android:textColor="#ffffff"
android:textSize="50dp"
android:gravity="center"
android:layout_marginBottom="16dp"/>
</LinearLayout>
<?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="wrap_content"
android:orientation="vertical"
android:background="#6A5D5D"
android:padding="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/man"
android:layout_marginRight="16dp"/>
<TextView
android:id="@+id/person_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:gravity="center"
android:textStyle="bold"
android:text="사용자"
android:textColor="#ffffff"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffffff"/>
</LinearLayout>
- 전화번호부를 위한 클래스 생성
// 전화번호부
class PhoneBook() {
val personList = ArrayList<Person>();
fun addPerson(person: Person) {
personList.add(person)
}
}
class Person(val name: String, val number: String) {
}
- for문으로 전화번호부 생성
fun createFakePhoneBook(fakeNumber: Int = 10, phoneBook: PhoneBook = PhoneBook()):PhoneBook {
for (i in 0 until fakeNumber) {
phoneBook.addPerson(
Person(
name = ""+i+"번째 사람",
number = ""+i+"번째 사람의 전화번호"
)
)
}
return phoneBook
}
- 생성된 전화번호부를 받아 화면에 달아주는 기능
fun createPhoneBookList(phoneBook: PhoneBook){
val layoutInflater = LayoutInflater.from(this@PhoneBookActivity)
val container = findViewById<LinearLayout>(R.id.phonebook_list_container)
for(i in 0 until phoneBook.personList.size){
val view = layoutInflater.inflate(R.layout.phonebook_item, null)
val personNameView = view.findViewById<TextView>(R.id.person_name)
personNameView.setText(phoneBook.personList.get(i).name)
addSetOnClickListener(phoneBook.personList.get(i), view)
container.addView(view)
}
}
- 클릭시 디테일로 넘어가는 기능
fun addSetOnClickListener(person: Person, view: View){
view.setOnClickListener {
val intent = Intent(this@PhoneBookActivity, PhoneBookDetailActivity::class.java)
intent.putExtra("name", person.name)
intent.putExtra("number", person.number)
startActivity(intent)
}
}
- 디테일 엑티비에서 받기
fun getPersonInfoAndDraw(){
val name = intent.getStringExtra("name")
val number = intent.getStringExtra("number")
person_detail_name.setText(name)
person_detail_number.setText(number)
}
- 디테일 엑티비티의 뒤로가는 기능
back.setOnClickListener{
//뒤로가기
onBackPressed()
}
- 메인에서의 실행구문
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_phone_book)
val phoneBook = createFakePhoneBook(30)
createPhoneBookList(phoneBook)
// 줄이면 -> createPhoneBookList(createFakePhoneBook())
}
RecyclerView (0) | 2020.11.19 |
---|---|
ListView (0) | 2020.11.19 |
addView (0) | 2020.11.18 |
Library (0) | 2020.11.18 |
Thread (0) | 2020.11.18 |
댓글 영역