-
Notifications
You must be signed in to change notification settings - Fork 1
[김예란_Android] 6주차 과제 제출 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example.android_25_2 | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.TextView | ||
| import androidx.recyclerview.widget.RecyclerView | ||
|
|
||
| class LapAdapter (private val lapList: MutableList<String>): RecyclerView.Adapter<RecyclerView.ViewHolder>(){ | ||
| class ViewHolder(view: View) : RecyclerView.ViewHolder(view){ | ||
| } | ||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | ||
| val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false) | ||
| return ViewHolder(view) | ||
| } | ||
|
|
||
| override fun getItemCount(): Int { | ||
| return lapList.size | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | ||
| val lapTime = lapList[position] | ||
| val textViewRecord: TextView = holder.itemView.findViewById(R.id.textView_record) | ||
| textViewRecord.text = lapTime | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,31 @@ | ||
| package com.example.android_25_2 | ||
|
|
||
| import android.os.Bundle | ||
| import android.widget.Button | ||
| import android.widget.TextView | ||
| import androidx.activity.enableEdgeToEdge | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.core.view.ViewCompat | ||
| import androidx.core.view.WindowInsetsCompat | ||
| import androidx.lifecycle.lifecycleScope | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.launch | ||
| import java.util.Locale | ||
| import java.util.Timer | ||
|
|
||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| var time = 0 | ||
| var isRunning = false | ||
| var lapList = mutableListOf<String>() | ||
| var timerJob: Job? = null | ||
| var timerTask: Timer? = null | ||
| var lapAdapter = LapAdapter(lapList) | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| enableEdgeToEdge() | ||
|
|
@@ -16,5 +35,64 @@ class MainActivity : AppCompatActivity() { | |
| v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | ||
| insets | ||
| } | ||
| val textViewTime = findViewById<TextView>(R.id.textView_time) | ||
| val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) | ||
| val buttonStartOrPause = findViewById<Button>(R.id.button_start_or_pause) | ||
| val buttonStop = findViewById<Button>(R.id.button_stop) | ||
| val buttonLap = findViewById<Button>(R.id.button_lap) | ||
|
|
||
| lapAdapter = LapAdapter(lapList) | ||
| recyclerView.layoutManager = LinearLayoutManager(this) | ||
| recyclerView.adapter = lapAdapter | ||
| textViewTime.text = "00:00:00" | ||
|
|
||
| fun formatTime(millis: Int): String { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 확장함수를 사용헀다면 더 좋을 것 같아요 |
||
| val minutes = ((millis % 3600000) / 60000) | ||
| val seconds = ((millis % 60000) / 1000) | ||
| val ms = ((millis % 1000) / 10) | ||
| return String.format(Locale.US, "%02d:%02d:%02d", minutes, seconds, ms) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Locale이 US인 이유가 있나요? |
||
| } | ||
|
|
||
| fun start() { | ||
| isRunning = true | ||
| timerJob = lifecycleScope.launch { | ||
| while (isRunning) { | ||
| delay(10) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coroutine에서 delay를 10ms만큼 준다고 실제 10ms만큼만 delay가 생기지 않습니다. |
||
| time += 10 | ||
| textViewTime.text = formatTime(time) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun pause() { | ||
| isRunning = false | ||
| timerJob?.cancel() | ||
| } | ||
|
|
||
| fun stopTimer() { | ||
| isRunning = false | ||
| timerJob?.cancel() | ||
| time = 0 | ||
| textViewTime.text = "00:00:00" | ||
| } | ||
|
|
||
| buttonStartOrPause.setOnClickListener { | ||
| if (isRunning) pause() | ||
| else start() | ||
| } | ||
|
|
||
| buttonStop.setOnClickListener { | ||
| stopTimer() | ||
| lapList.clear() | ||
| lapAdapter.notifyDataSetChanged() | ||
| isRunning = false | ||
| } | ||
|
|
||
| buttonLap.setOnClickListener{ | ||
| val currentTime = textViewTime.text.toString() | ||
| lapList.add(0, currentTime) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0번 인덱스에 추가하는 방법도 좋은 방법이지만, recyclerview 자체를 반대로 뒤집을 수도 있어요 |
||
| lapAdapter.notifyItemInserted(0) | ||
| recyclerView.scrollToPosition(0) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="50dp" | ||
| xmlns:app="http://schemas.android.com/apk/res-auto" | ||
| android:background="@color/white"> | ||
|
|
||
| <TextView | ||
| android:id="@+id/textView_record" | ||
| android:layout_width="0dp" | ||
| android:layout_height="wrap_content" | ||
| android:textSize="20sp" | ||
| android:textStyle="bold" | ||
| app:layout_constraintTop_toTopOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| android:layout_marginTop="13dp" | ||
| android:layout_marginHorizontal="15dp"/> | ||
|
|
||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,7 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <resources> | ||
| <color name="purple_200">#FFBB86FC</color> | ||
| <color name="purple_500">#FF6200EE</color> | ||
| <color name="purple_700">#FF3700B3</color> | ||
| <color name="teal_200">#FF03DAC5</color> | ||
| <color name="teal_700">#FF018786</color> | ||
| <color name="purple">#FF6200EE</color> | ||
| <color name="black">#FF000000</color> | ||
| <color name="white">#FFFFFFFF</color> | ||
| <color name="gray">#F1F3F5</color> | ||
| </resources> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| <resources> | ||
| <string name="app_name">Android_25-2</string> | ||
| <string name="button_text_start_or_pause">Start/Pause</string> | ||
| <string name="button_text_stop">Stop</string> | ||
| <string name="button_text_lap">Lap</string> | ||
| </resources> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private 처리를 해도 좋을 것 같아요