-
Notifications
You must be signed in to change notification settings - Fork 1
[정현수_Android] 6-1주차 과제 제출 #5
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,86 @@ | ||
| package com.example.android_2026_1 | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.enableEdgeToEdge | ||
| import androidx.appcompat.app.AlertDialog | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.core.view.ViewCompat | ||
| import androidx.core.view.WindowInsetsCompat | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import com.example.android_2026_1.databinding.ActivityMainBinding | ||
| import com.example.android_2026_1.databinding.DialogDeleteBinding | ||
| import com.example.android_2026_1.databinding.DialogEditBinding | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| private val nameList = mutableListOf<String>() | ||
| private lateinit var nameAdapter: NameAdapter | ||
| private lateinit var binding: ActivityMainBinding | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| enableEdgeToEdge() | ||
| setContentView(R.layout.activity_main) | ||
| ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> | ||
| val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | ||
| v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | ||
| insets | ||
| binding = ActivityMainBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
|
|
||
| nameAdapter = NameAdapter( | ||
| nameList, | ||
| onItemClick = { position -> showDeleteDialog(position) }, | ||
| onItemLongClick = { position -> showEditDialog(position) } | ||
| ) | ||
|
|
||
| binding.recyclerView.layoutManager = LinearLayoutManager(this) | ||
| binding.recyclerView.adapter = nameAdapter | ||
|
|
||
| binding.floatingActionButton.setOnClickListener { | ||
| val inputName = binding.editTextText.text.toString() | ||
| if (inputName.isNotEmpty()) { | ||
| nameList.add(inputName) | ||
| nameAdapter.notifyItemInserted(nameList.size - 1) | ||
| binding.editTextText.text.clear() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun showDeleteDialog(position: Int) { | ||
| val dialogBinding = DialogDeleteBinding.inflate(layoutInflater) | ||
| val dialog = AlertDialog.Builder(this) | ||
| .setView(dialogBinding.root) | ||
| .create() | ||
|
|
||
| dialogBinding.btnDeleteCancel.setOnClickListener { | ||
| dialog.dismiss() | ||
| } | ||
|
|
||
| dialogBinding.btnDeleteConfirm.setOnClickListener { | ||
| nameList.removeAt(position) | ||
| nameAdapter.notifyItemRemoved(position) | ||
| nameAdapter.notifyItemRangeChanged(position, nameList.size) | ||
| dialog.dismiss() | ||
| } | ||
|
|
||
| dialog.show() | ||
| } | ||
|
|
||
| private fun showEditDialog(position: Int) { | ||
| val dialogBinding = DialogEditBinding.inflate(layoutInflater) | ||
| val dialog = AlertDialog.Builder(this) | ||
| .setView(dialogBinding.root) | ||
| .create() | ||
|
|
||
| dialogBinding.etDialogName.setText(nameList[position]) | ||
| dialogBinding.etDialogName.requestFocus() | ||
| dialogBinding.etDialogName.selectAll() | ||
|
|
||
| dialogBinding.btnEditCancel.setOnClickListener { | ||
| dialog.dismiss() | ||
| } | ||
|
|
||
| dialogBinding.btnEditConfirm.setOnClickListener { | ||
| val updatedName = dialogBinding.etDialogName.text.toString() | ||
| if (updatedName.isNotEmpty()) { | ||
| nameList[position] = updatedName | ||
| nameAdapter.notifyItemChanged(position) | ||
| } | ||
| dialog.dismiss() | ||
| } | ||
|
|
||
| dialog.show() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.example.android_2026_1 | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.ViewGroup | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import com.example.android_2026_1.databinding.ItemNameBinding | ||
|
|
||
| class NameAdapter( | ||
| private val nameList: MutableList<String>, | ||
| private val onItemClick: (Int) -> Unit, | ||
| private val onItemLongClick: (Int) -> Unit | ||
| ) : RecyclerView.Adapter<NameAdapter.ViewHolder>() { | ||
|
|
||
| class ViewHolder(val binding: ItemNameBinding) : RecyclerView.ViewHolder(binding.root) | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
| val binding = ItemNameBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
| return ViewHolder(binding) | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
| holder.binding.tvName.text = nameList[position] | ||
|
|
||
| holder.itemView.setOnClickListener { | ||
| onItemClick(position) | ||
|
Collaborator
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. 중간에 있는 아이템을 삭제하고 그 뒤에 있는 아이템을 빠르게 클릭했을 때, 만약 뒤에 있는 아이템이 재바인딩되지 않은 상태라면 유효하지 않은 position이 전달되어 버그가 발생할 수 있습니다. 클릭했을 때 유효성 검사를 하는 것이 좋아보입니다. |
||
| } | ||
|
|
||
| holder.itemView.setOnLongClickListener { | ||
| onItemLongClick(position) | ||
| true | ||
| } | ||
| } | ||
|
|
||
| override fun getItemCount(): Int = nameList.size | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,4 +7,35 @@ | |
| android:layout_height="match_parent" | ||
| tools:context=".MainActivity"> | ||
|
|
||
| <EditText | ||
| android:id="@+id/editTextText" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:ems="10" | ||
| android:inputType="text" | ||
| android:textColorHighlight="@color/light_gray_highlight" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintHorizontal_bias="0.497" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toTopOf="parent" /> | ||
|
|
||
| <com.google.android.material.floatingactionbutton.FloatingActionButton | ||
| android:id="@+id/floatingActionButton" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:clickable="true" | ||
| android:contentDescription="@string/btn_add_name" | ||
| app:layout_constraintBottom_toBottomOf="parent" | ||
| app:layout_constraintEnd_toEndOf="@id/recyclerView" | ||
| app:srcCompat="@android:drawable/ic_input_add" /> | ||
|
|
||
| <androidx.recyclerview.widget.RecyclerView | ||
| android:id="@+id/recyclerView" | ||
| android:layout_width="409dp" | ||
| android:layout_height="610dp" | ||
|
Comment on lines
+34
to
+35
Collaborator
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. dp 고정값을 주신 이유가 따로 있으실까요? |
||
| app:layout_constraintBottom_toTopOf="@+id/floatingActionButton" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@+id/editTextText" /> | ||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?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:padding="24dp"> | ||
|
|
||
| <TextView | ||
| android:id="@+id/tv_delete_title" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:text="@string/dialog_delete_title" | ||
| android:textColor="@color/black" | ||
| android:textSize="20sp" | ||
| android:textStyle="bold" /> | ||
|
|
||
| <TextView | ||
| android:id="@+id/tv_delete_message" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginTop="12dp" | ||
| android:text="@string/dialog_delete_message" | ||
| android:textColor="@color/black" | ||
| android:textSize="14sp" /> | ||
|
|
||
| <LinearLayout | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginTop="24dp" | ||
| android:gravity="end" | ||
| android:orientation="horizontal"> | ||
|
|
||
| <Button | ||
| android:id="@+id/btn_delete_cancel" | ||
| style="?android:attr/borderlessButtonStyle" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="@string/btn_cancel" | ||
| android:textColor="@color/purple_button" /> | ||
|
|
||
| <Button | ||
| android:id="@+id/btn_delete_confirm" | ||
| style="?android:attr/borderlessButtonStyle" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginStart="8dp" | ||
| android:text="@string/btn_delete" | ||
| android:textColor="@color/purple_button" /> | ||
|
|
||
| </LinearLayout> | ||
|
|
||
| </LinearLayout> |
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.
요구 사항에는 없었지만 isNotEmpty()는 공백이 있을 경우 검증하지 못합니다. 따라서 trim()을 사용해서 공백 제거 후 검증하는 것이 좋아보입니다.