Android Tools attributes reference 基本設定 #RecyclerView 顯示 #Sample Data Directory

基本的 RecyclerView 很單調的只會顯示 item0, item1

基本 RecyclerView

設定 1. 可以限制顯示個數

設定 itemCount

xml 檔的畫面會顯示 5 個項目

設定 2. 顯示客製化 item

可以調整 Layout 豐富畫面

新增 item Layout
new Layout XML file

file name : custom_item_Layout

基本的 .xml 檔設定

item 畫面

設定 RecyclerView list item


設定 listitem 後,顯示畫面
會帶入 custom_item_Layout 的設定
所有的 item 的資料都是相同的沒有變化

設定 3. 使用 Tools 的基本設定,Sample data 替代字串



重新設定 text 顯示,用 Sample data,不影響原本的 text 及最後實際顯示的資料

tools:text 設定 us_phone 跟 full_names

設定日期

custom_item_Layout 顯示

RecyclerView 會顯示不同的資料,豐富畫面
資料不再固定

補充額外操作方式:透過 Design 操作

選擇 Design

有兩個 text 欄位,選擇下方 tools:text
會顯示所有支援的 Sample data

支援的 Sample data

設定 4. Sample Data Directory,自定義 text 檔替代字串

新增 Sample Data Directory

顯示方式切換到 Project

顯示 sampledata 資料夾
設定 text 檔案

new file

取名 personalName

選擇 Text

編輯檔案,新增想要顯示的字串

xml 檔設定

custom_item_Layout 顯示

Recycler View 會把 text 檔的資料帶進來

設定 5. Sample Data Directory,自定義 json 檔替代字串

new json file

新增 5 筆資料內容包含 name, country, phone

系統會自動提示

把 json 檔的設定寫到 item

custom_item_Layout 顯示

Recycler View 顯示內容與 json 相同

設定 6. 人物頭像替換

效果

custom_item_Layout 顯示

只需要設定 @tools:sample/avatars 即可

文章最後補充

補充 1.
json 檔及 text 檔皆可在專案資料夾下找到

路徑同樣在 sampledata 下

補充 2.
itemCount 在 RecyclerView 有效 ListView 會失效

ListView 設定 itemCount = 5

超過 5 個項目,itemCount 沒有作用
補充 3.
自定義 text 檔及 json 檔超過顯示數量

設定 12 個項目,資料只有 5 筆

部分資料會重複

Done

留言

熱門文章

TourCard 開通全紀錄 台胞證 支付寶

[ 教學 ] 如何使用 Sourcetree 對 Github 進行版本控制 #Mac OS 版本控制

svn 刪除使者資訊 & 查看使用者帳號密碼