add recyclerview

This commit is contained in:
adm 2024-10-31 15:35:15 +03:00
parent 9e363fed73
commit f3417b5e55
6 changed files with 61 additions and 11 deletions

View File

@ -4,7 +4,7 @@
<selectionStates> <selectionStates>
<SelectionState runConfigName="app"> <SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" /> <option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2024-10-29T14:53:18.958676800Z"> <DropdownSelection timestamp="2024-10-30T14:28:50.613358900Z">
<Target type="DEFAULT_BOOT"> <Target type="DEFAULT_BOOT">
<handle> <handle>
<DeviceId pluginId="LocalEmulator" identifier="path=C:\Users\adm\.android\avd\Medium_Phone_API_35.avd" /> <DeviceId pluginId="LocalEmulator" identifier="path=C:\Users\adm\.android\avd\Medium_Phone_API_35.avd" />

View File

@ -4,16 +4,31 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView.Adapter import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.autorization.R import com.example.autorization.R
import com.example.autorization.ui.fragments.signup.User import com.example.autorization.ui.fragments.signup.User
class UserAdapter(private val users:List<User>): Adapter<UserAdapter.UserViewHolder>() { class UserAdapter(private val users:List<User>): Adapter<UserAdapter.UserViewHolder>() {
var OnClick:((index:Int, view:View) -> Unit)? = null
private val _users = mutableListOf<User>()
init {
_users.addAll(users)
}
inner class UserViewHolder(view: View):ViewHolder(view) { inner class UserViewHolder(view: View):ViewHolder(view) {
val userNameTextView = view.findViewById<TextView>(R.id.UserNameTextView) val userNameTextView = view.findViewById<TextView>(R.id.UserNameTextView)
val userEmailTextView = view.findViewById<TextView>(R.id.UserEmailTextView) val userEmailTextView = view.findViewById<TextView>(R.id.UserEmailTextView)
val userPasswordTextView = view.findViewById<TextView>(R.id.UserPasswordTextView) val userPasswordTextView = view.findViewById<TextView>(R.id.UserPasswordTextView)
val userCardView = view.findViewById<CardView>(R.id.UserCardView)
init{
userCardView.setOnClickListener {
OnClick?.invoke(adapterPosition, view)
}
}
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
@ -21,11 +36,16 @@ class UserAdapter(private val users:List<User>): Adapter<UserAdapter.UserViewHol
.inflate(R.layout.user_item, parent, false)) .inflate(R.layout.user_item, parent, false))
} }
override fun getItemCount(): Int = users.size fun deleteUserById(index: Int){
_users.removeAt(index)
notifyItemRemoved(index)
}
override fun getItemCount(): Int = _users.size
override fun onBindViewHolder(holder: UserViewHolder, position: Int) { override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.userNameTextView.text = users[position].name holder.userNameTextView.text = _users[position].name
holder.userEmailTextView.text = users[position].email holder.userEmailTextView.text = _users[position].email
holder.userPasswordTextView.text = users[position].password holder.userPasswordTextView.text = _users[position].password
} }
} }

View File

@ -1,17 +1,15 @@
package com.example.autorization.ui.fragments package com.example.autorization.ui.fragments
import android.os.Bundle import android.os.Bundle
import androidx.fragment.app.Fragment import android.view.MenuItem
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.widget.PopupMenu
import androidx.fragment.app.viewModels import android.widget.PopupMenu.OnMenuItemClickListener
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.example.autorization.R import com.example.autorization.R
import com.example.autorization.ui.adapters.UserAdapter import com.example.autorization.ui.adapters.UserAdapter
import com.example.autorization.ui.fragments.signup.SignUpViewModel
import com.example.autorization.ui.fragments.signup.User import com.example.autorization.ui.fragments.signup.User
@ -33,6 +31,31 @@ class UsersFragment : Fragment(R.layout.fragment_users) {
recyclerView.layoutManager = LinearLayoutManager(requireContext()) recyclerView.layoutManager = LinearLayoutManager(requireContext())
val userAdapter = UserAdapter(users) val userAdapter = UserAdapter(users)
recyclerView.adapter = userAdapter recyclerView.adapter = userAdapter
userAdapter.OnClick = { index, view ->
showMenu(view, userAdapter, index)
} }
}
private fun showMenu(
view:View,
userAdapter: UserAdapter,
index:Int){
PopupMenu(requireContext(), view).apply {
setOnMenuItemClickListener { item ->
when (item?.itemId) {
R.id.DeleteAction -> {
userAdapter.deleteUserById(index)
true
}
else -> true
}
}
inflate(R.menu.user_action_menu)
show()
}
}
} }

View File

@ -2,6 +2,7 @@
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/UserCardView"
android:layout_margin="5dp" android:layout_margin="5dp"
android:layout_width="match_parent" android:layout_width="match_parent"
app:cardCornerRadius="20dp" app:cardCornerRadius="20dp"

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/DeleteAction"
android:title="@string/remove_user"/>
</menu>

View File

@ -11,4 +11,5 @@
<string name="tarms_and_policy">Im agree to The Tarms of Service and Privasy Policy</string> <string name="tarms_and_policy">Im agree to The Tarms of Service and Privasy Policy</string>
<string name="create_account">Create Account</string> <string name="create_account">Create Account</string>
<string name="do_you_have_account_sign_in">Do you have account? Sign In</string> <string name="do_you_have_account_sign_in">Do you have account? Sign In</string>
<string name="remove_user">Удалить пользователя</string>
</resources> </resources>