Android RecyclerView使用入門介紹
添加 recycler 依賴
前往 build.gradle 下,添加以下依賴:
implementation 'androidx.recyclerview:recyclerview:1.2.1'
設(shè)置單個(gè)列表項(xiàng)布局
眾所周知,一個(gè)完整的列表是由多個(gè)列表項(xiàng)組成的,而列表項(xiàng)可以使用布局文件進(jìn)行定義;
我們簡單的使用線性布局+一個(gè) tv 組件定義列表項(xiàng);
下方為布局文件 items.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingRight="20dp" android:paddingLeft="20dp"> <TextView android:id="@+id/item_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:padding="15dp" android:text="tools" android:textColor="#353434" android:textSize="20sp"/> </LinearLayout>
主布局中添加 recyclerview
添加方式和 listview 基本一致,很簡單:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
RecyclerAdapter
我們同樣需要編寫一個(gè)適配器來配置其對象關(guān)系;
新建適配器文件 RecyclerAdapter.kt
基本原理以及完善過程:
- 首先讓主類繼承
RecyclerView.Adapter
,泛型使用本類中的我們自建的 viewholder 方法 - 我們自創(chuàng)的 viewholder 方法需要繼承
RecyclerView.ViewHolder
- 我們在
MyViewHolder
中直接獲取列表項(xiàng)中的TextView
組件 - 與此同時(shí),我們還需要實(shí)現(xiàn)三個(gè)方法
onCreateViewHolder
:選擇使用哪一個(gè) layout 作為 viewholder,并放回該 layout 組成的 viewholder
onBindViewHolder
:針對 viewholder 內(nèi)部組件的一些賦值與處理操作
getItemCount
:列表項(xiàng)需要渲染幾個(gè)?
class RecyclerAdapter(val context: Context) : RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>() { inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { val textView: TextView = view.findViewById(R.id.item_text) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { val itemView = LayoutInflater.from(context).inflate(R.layout.items, parent, false) return MyViewHolder(itemView) } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { holder.textView.text = "this is $position" } override fun getItemCount(): Int { return 40 } }
主 activity 注冊
初始化 recyclerview 的方式和 listview 差不多,只不過這里多設(shè)置了一個(gè) layoutManager
依葫蘆畫瓢即可!
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) initRecycler() } fun initRecycler() { val adapter = RecyclerAdapter(this) val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) recyclerView.adapter = adapter val layoutManager = LinearLayoutManager(this) layoutManager.orientation = RecyclerView.VERTICAL recyclerView.layoutManager = layoutManager } }
成果圖
到此這篇關(guān)于Android RecyclerView使用入門介紹的文章就介紹到這了,更多相關(guān)Android RecyclerView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過程
- Android RecyclerView實(shí)現(xiàn)吸頂動態(tài)效果流程分析
- Android RecyclerView四級緩存源碼層詳細(xì)分析
- Android RecyclerView緩存復(fù)用原理解析
- Android開發(fā)RecyclerView單獨(dú)刷新使用技巧
- Android開發(fā)RecyclerView實(shí)現(xiàn)折線圖效果
- Android?手寫RecyclerView實(shí)現(xiàn)列表加載
- Android獲取RecyclerView滑動距離方法詳細(xì)講解
相關(guān)文章
Android實(shí)現(xiàn)收到新短信后自動發(fā)郵件功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)收到新短信后自動發(fā)郵件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05RecyclerView實(shí)現(xiàn)橫向滾動效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)橫向滾動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01Android仿餓了么加入購物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動畫的按鈕效果(實(shí)例詳解)
這篇文章主要介紹了Android仿餓了么加入購物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動畫的按鈕效果(實(shí)例詳解)的相關(guān)資料,需要的朋友可以參考下2017-01-01Android登錄記住多個(gè)密碼的實(shí)現(xiàn)方法
通過在popouWindow里面加上ListView,數(shù)據(jù)是把List以字符串按照J(rèn)SON的樣式存入本地,下面通過實(shí)例代碼給大家介紹android登錄記住多個(gè)密碼的實(shí)現(xiàn)方法,需要的的朋友參考下吧2017-07-07Android 坐標(biāo)系與視圖坐標(biāo)系圖解分析
下面小編就為大家?guī)硪黄狝ndroid 坐標(biāo)系與視圖坐標(biāo)系圖解分析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03解決Android Studio Design界面不顯示layout控件的問題
這篇文章主要介紹了解決Android Studio Design界面不顯示layout控件的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android實(shí)現(xiàn)類似iOS風(fēng)格的對話框?qū)嵗a
通過本文給大家分享一個(gè)簡單的常用的對話框類,關(guān)于Android實(shí)現(xiàn)類似iOS風(fēng)格的對話框?qū)嵗a大家通過本文學(xué)習(xí)下吧2017-09-09