iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 15
0
自我挑戰組

Android App 初學者從零開始用Kotlin寫APP系列 第 15

<Day15>RecyclerView(上)

  • 分享至 

  • xImage
  •  

RecyclerView 是 ListView 的進階版本。列表中的view 通過擴展 RecyclerView.ViewHolder class 來實作,data 與view 之間通過 RecyclerView.Adapter class 建立與配置,onBindViewHolder() 方法 將view綁定到對應data,此方法使用畫面position來顯示內容。

本篇先以 adapter 和 layout manager (linear / grid / staggered) 與 view holder 說明。

使用 RecyclerView 先import

dependencies {
  implementation "androidx.recyclerview:recyclerview:1.1.0"
  }
  
  • layout
<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.appcompat.widget.AppCompatTextView
        android:id="@+id/textTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:gravity="center"
        android:text="@string/todo_list_title"
        android:textColor="#000"
        android:textSize="26sp"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

  • ViewHolder
    ViewHolder 儲存 View reference 的地方,也可以把它當成儲存 View 的 class。
class TodoViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
    LayoutInflater.from(parent.context).inflate(R.layout.item_todo, parent, false)
) {
    val checkbox: AppCompatCheckBox = itemView.checkbox
}
  • Adapter

Adapter 介接 data 跟 View 的地方

class TodoAdapter : RecyclerView.Adapter<TodoViewHolder>() {

    private var todos = listOf<Todo>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
        return TodoViewHolder(parent)
    }


   // 回傳 Adapter 包含幾筆資料
    override fun getItemCount(): Int {
        return todos.size
    }
    // 依據 position 正確的資料跟 ViewHolder 綁定
    override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
        val todo = todos[position]
        holder.checkbox.text = todo.memo
        holder.checkbox.isChecked = todo.checked
    }

}

manager :linear / grid / staggered

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val adapter = TodoAdapter()
        recyclerView.adapter = adapter
        
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

        adapter.refresh(
            listOf(Todo("hello", false), Todo("world", false))
        )
    }
}

LinearLayoutManager

recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

https://ithelp.ithome.com.tw/upload/images/20200928/20130598mDkw1xSnsP.png

GridLayoutManager

recyclerView.layoutManager = GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false)

https://ithelp.ithome.com.tw/upload/images/20200928/20130598hcdL6kffE2.png

StaggeredGridLayoutManager

recyclerView.layoutManager = StaggeredGridLayoutManager( 3, GridLayoutManager.VERTICAL)

https://ithelp.ithome.com.tw/upload/images/20200928/20130598RaCdwYZ9F3.png

reference: https://www.notion.so/RecyclerView-8-26-Andy-b0d303838fe0425a95d8ba2d4cf89727
reference :https://ithelp.ithome.com.tw/articles/10220196
reference :https://developer.android.com/guide/topics/ui/layout/recyclerview


上一篇
<Day 14 >Fragment 使用
下一篇
<Day16>RecyclerView(中)
系列文
Android App 初學者從零開始用Kotlin寫APP30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言