반응형
1. 리사이클러 뷰 다시
build.gradle(:app)
viewBinding{
enabled = true
}
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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recyclerview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/recycler_item" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@android:drawable/ic_menu_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_item.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="40dp"
android:paddingVertical="5dp"
>
<ImageView
android:id="@+id/img_rv_photo"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:text="이름"
/>
</LinearLayout>
Member.kt
package com.example.recycler_view_test_2
data class Member (val name:String, val img:Int)
MyAdapter.kt
package com.example.recycler_view_test_2
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.recycler_view_test_2.databinding.RecyclerItemBinding
class MyAdapter : RecyclerView.Adapter<Holder>() {
var listData = mutableListOf<Member>();
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val binding = RecyclerItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return Holder(binding)
}
override fun onBindViewHolder(holder: Holder, position: Int) {
val member = listData[position]
holder.setData(member)
}
override fun getItemCount(): Int {
return listData.size
}
}
class Holder(var binding:RecyclerItemBinding) : RecyclerView.ViewHolder(binding.root){
fun setData(member:Member){
binding.tvItem.text = member.name
binding.imgRvPhoto.setImageResource(member.img)
}
}
MainActivity.kt
package com.example.recycler_view_test_2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.recycler_view_test_2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding:ActivityMainBinding
private val data : MutableList<Member> = mutableListOf()
var i=4
private val thumbs = arrayOf(R.drawable.profile1,R.drawable.profile2);
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
_initialize();
_refreshRecylcerView()
binding.floatingActionButton.setOnClickListener{
val string = "Member$i"
i++
val j = (i%thumbs.size)
Log.i("PAUL", "click click click click j : $j")
data.add(Member(string, thumbs[j]))
_refreshRecylcerView()
}
}
private fun _initialize(){
with(data){
add(Member("Member1", thumbs[0]))
add(Member("Member2", thumbs[1]))
add(Member("Member3", thumbs[0]))
}
}
private fun _refreshRecylcerView(){
val adapter = MyAdapter()
adapter.listData = data
binding.myRecyclerview.adapter = adapter
binding.myRecyclerview.layoutManager = LinearLayoutManager(this)
}
}
그외
fab 에 android:clickable="true" 필요함???
xml의 recyclerview에 item 적용
tools:listitem="@layout/recycler_item"