Skip to content

Latest commit

 

History

History
222 lines (134 loc) · 4.38 KB

2주차.md

File metadata and controls

222 lines (134 loc) · 4.38 KB

2주차 과제

필수 과제, 성장 과제 1 성장 과제2
image image

필수 과제

  • RecyclerView 사용
  1. Activity 에 RecyclerView 추가
  2. item xml 추가
  3. RecyclerView.Adapter 생성
  4. RecyclerView에 Adapter 연결

성장 과제 1

  • GridLayout 사용
  1. GridLayout으로 바꿔주는 버튼 클릭 시 recyclerview의 adapter와 layoutmanager 다시 세팅

성장 과제 2

  • RecyclerView Item이동 삭제 구현
  1. ItemTouchHelperListener 생성
  2. ItemTouchHelperCallback 생성
  3. RecyclerView.Adapter에 ItemTouchHelperListener 구현
  4. ItemTouchHelperItemTouchHelperCallback 등록

주요 코드

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) {
        ...
    }
}