필수 과제, 성장 과제 1 | 성장 과제2 |
---|---|
- RecyclerView 사용
- Activity 에 RecyclerView 추가
- item xml 추가
- RecyclerView.Adapter 생성
- RecyclerView에 Adapter 연결
- GridLayout 사용
- GridLayout으로 바꿔주는 버튼 클릭 시 recyclerview의 adapter와 layoutmanager 다시 세팅
- RecyclerView Item이동 삭제 구현
ItemTouchHelperListener
생성ItemTouchHelperCallback
생성- RecyclerView.Adapter에
ItemTouchHelperListener
구현 ItemTouchHelper
에ItemTouchHelperCallback
등록
ProfileActivity.kt
private fun initView() {
//item touch helper 등록
val itemTouchHelperCallback = ItemTouchHelperCallback(profileAdapter)
val itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
viewDataBinding.recyclerView.apply {
layoutManager = linearLayoutManager
adapter = profileAdapter
setHasFixedSize(true)
}
itemTouchHelper.attachToRecyclerView(viewDataBinding.recyclerView)
}
private fun initEvent() {
viewDataBinding.btnLinear.setOnClickListener {
setRecyclerViewLinear()
}
viewDataBinding.btnGrid.setOnClickListener {
setRecyclerViewGrid()
}
}
private fun initData() {
profileList = mutableListOf(...)
profileAdapter.setData(profileList)
}
ProfileAdapter.kt
class ProfileAdapter(private val context : Context) : RecyclerView.Adapter<ProfileAdapter.ViewHolder>(), ItemTouchHelperListener {
private val data = mutableListOf<Profile>()
private val linearItemView = R.layout.item_profile
private val gridItemView = R.layout.item_profile_grid
private var itemViewType : Int = linearItemView
private var onItemClickListener : ((Profile) -> Unit) ?= null
fun setData(data : MutableList<Profile>) {
this.data.clear()
this.data.addAll(data)
notifyDataSetChanged()
}
fun setOnItemClickListener(listener : (Profile) -> Unit) {
this.onItemClickListener = listener
}
fun setGridItemViewType(){
this.itemViewType = gridItemView
}
fun setLinearItemViewType(){
this.itemViewType = linearItemView
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val title : TextView = itemView.findViewById(R.id.title)
private val subtitle : TextView = itemView.findViewById(R.id.subtitle)
private val image : ImageView = itemView.findViewById(R.id.image)
fun onBind(data : Profile) {
title.text = data.title
subtitle.text = data.subtitle
image.setImageResource(data.resourceId)
itemView.setOnClickListener {
onItemClickListener?.invoke(data)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(itemViewType, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.onBind(data[position])
}
override fun getItemCount(): Int = data.size
override fun onItemMoved(from: Int, to: Int){
Collections.swap(data, from, to)
notifyItemMoved(from, to)
}
override fun onItemSwiped(position: Int) {
data.removeAt(position)
notifyItemRemoved(position)
}
}
ItemTouchHelperListener.kt
interface ItemTouchHelperListener {
fun onItemMoved(from : Int, to : Int)
fun onItemSwiped(position : Int)
}
ItemTouchHelperCallback.kt
class ItemTouchHelperCallback(private val listener: ItemTouchHelperListener) : ItemTouchHelper.Callback(){
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
): Int {
...
}
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
...
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
...
}
}