Kotlin RecyclerView + CardView use viewBinding #android studio #toturial

 Kotlin RecyclerView + CardView use viewBinding 

1. build.gradle (Module) add dependencies


set viewBinding true

add implementation

2. Create RecyclerViewActivity & activity_recycler_view.xml

Code


use tools:listitem check UI Layout

tools attribute reference describe : https://www.eeaseries.com/2021/03/tools-attributes-reference.html

3. check viewBinding file is create

check ActivityRecyclerViewBinding.java file is create

if not find ActivityRecyclerViewBinding.file

Rebulid Project

4. create stock_list_item.xml use cardView

Code


5. Create StockAdapter use ViewBinding + RecyclerView

Code


use ArrayList<Stock> to store information

in onBindViewHolder
use stockList[position] to setText in TextView

need to check stockList.size > 0, otherwise RecyclerVIew will not print data

6. Create Stock.kt to Store information

Code


7. prepare RecyclerViewActivity.kt dataList

Code

prepare data


(1) use viewBing to control RecyclerView

(2) prepare stockList

(3) set stocklist data to adapter

(4) set RecyclerView layoutManager

(5) notify data change

8. How to StartActivity -> RecyclerVIewActivity ?

start another activity


project name : sidekotlin

commit version : 7a52839

more viewbinding describe : DataStore + ViewBinding

後記

如果這篇文章對於你有幫助,可以幫忙分享給更多的人.文章內容如果有誤,可以在下方留言告知.本網站主要提供程式相關資訊,可以訂閱獲得最即時的資訊.

留言

熱門文章

40 個可以改變寫程式技巧的秘訣

如何創造出難以維護的程式碼

雲端硬碟比較,哪種硬碟最推薦? (Google 雲端硬碟, OneDrive, DropBox, iCloud)