猛禽洛的程式筆記庫

[Android] (Kotlin版) 使用RecycleView建立清單

這裡記錄一下如何使用Kotlin語言來製作RecycleView的功能。

list_test.xml (清單的View)

<?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"
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

與Java一樣架構。

 

DeviceUserManagerAdapter.kt (RecycleView的Adapter)

class DeviceUserManagerAdapter(private val context: Context, private val data: IntArray, private val itemClickListener: OnItemClickListener): RecyclerView.Adapter<DeviceUserManagerAdapter.ViewHolder>()  {

    //建立點擊事件傳遞介面
    interface OnItemClickListener {
        fun onItemClick(position: Int)
        fun onItemLongClick(position: Int)
    }

    inner class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
        //把layout檔的元件們拉進來,指派給當地變數
        val textView8: TextView = itemView.findViewById(R.id.textView8)
        init {
            textView8.setOnClickListener {
                Toast.makeText(context, "點擊了$adapterPosition",Toast.LENGTH_SHORT).show()
                val position = getBindingAdapterPosition()//adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    //傳遞點擊事件
                    itemClickListener.onItemClick(position)
                }
            }
            textView8.setOnLongClickListener {
                val position = getBindingAdapterPosition()
                //處理長按事件
                if (position != RecyclerView.NO_POSITION) {
                    itemClickListener.onItemLongClick(position)
                }
                true
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeviceUserManagerAdapter.ViewHolder {
        //載入畫面
        val inflater = LayoutInflater.from(parent.context)
        val view = inflater.inflate(R.layout.list_test, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: DeviceUserManagerAdapter.ViewHolder, position: Int) {
        holder.textView8.text = data[position].toString()
    }

    override fun getItemCount(): Int {
        return data.size
    }

}

與Java有些不一樣,綁定View元件的地方使用inner,並且在init中監聽元件的動作。

在Adapter建立時,順便將初始的清單與必要資訊傳入,同時建立點擊接收介面interface,方便清單的控制

 

主程式部分

private lateinit var srlLayout: RefreshLayout
private lateinit var rvList: RecyclerView

private lateinit var mDeviceUserManagerAdapter: DeviceUserManagerAdapter

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_device_user_manager)
  initView()
  //這邊有使用SmartRefreshLayout套件來實現下拉重整的功能(不一定要)
  srlLayout.setRefreshHeader(MaterialHeader(this))
  srlLayout.setOnRefreshListener { refreshlayout ->
    refreshlayout.finishRefresh(1000 /*,false*/) //傳入false表示刷新失敗
  }
  //作為預設的清單資訊
  var data = intArrayOf(68, 90, 76, 88, 92)
  //將Adapter new出來並初始化
  mDeviceUserManagerAdapter = DeviceUserManagerAdapter(this, data, itemClickListener = object: DeviceUserManagerAdapter.OnItemClickListener{
    override fun onItemClick(position: Int) {
        //清單案一下的事件
    }

    override fun onItemLongClick(position: Int) {
        //清單長按的事件
    }
  })
  //與JAVA一樣,決定清單樣式與方向(這邊預設)
  rvList.layoutManager = LinearLayoutManager(this)
  //將Adapter套用至RecyclerView
  rvList.adapter = mDeviceUserManagerAdapter
}

private fun initView(){
  srlLayout = findViewById(R.id.srl_device_user_manager_layout)
  rvList = findViewById(R.id.rv_device_user_manager_list)
}

 

*更新

使用binding方式綁定Adapter元件的方式

inner class ViewHolder(bindingList: ListProjectBinding): RecyclerView.ViewHolder(bindingList.root) {
  //綁定元件
  var binding: ListProjectBinding = bindingList

  init {
    binding.clListProjectRoot.setOnClickListener {//傳送點擊事件
      val position = getBindingAdapterPosition()//adapterPosition
      if (position != RecyclerView.NO_POSITION) {
        itemClickListener.onItemClick(position)
      }
    }
    binding.clListProjectRoot.setOnLongClickListener {
      val position = getBindingAdapterPosition()
      if (position != RecyclerView.NO_POSITION) {
        itemClickListener.onItemLongClick(position)
      }
      true
    }
  }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  val binding = ListProjectBinding.inflate(LayoutInflater.from(parent.context), parent, false)
  return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  //元件的顯示處理
  with(list[position]) {
    holder.binding.tvListProjectName.text = this.name
  }
}

 

-END-