Android RecyclerView使用入門(mén)介紹
添加 recycler 依賴
前往 build.gradle 下,添加以下依賴:
implementation 'androidx.recyclerview:recyclerview:1.2.1'
設(shè)置單個(gè)列表項(xiàng)布局
眾所周知,一個(gè)完整的列表是由多個(gè)列表項(xiàng)組成的,而列表項(xiàng)可以使用布局文件進(jìn)行定義;
我們簡(jiǎ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 基本一致,很簡(jiǎn)單:
<?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
我們同樣需要編寫(xiě)一個(gè)適配器來(lái)配置其對(duì)象關(guān)系;
新建適配器文件 RecyclerAdapter.kt
基本原理以及完善過(guò)程:
- 首先讓主類(lèi)繼承
RecyclerView.Adapter,泛型使用本類(lèi)中的我們自建的 viewholder 方法 - 我們自創(chuàng)的 viewholder 方法需要繼承
RecyclerView.ViewHolder - 我們?cè)?
MyViewHolder中直接獲取列表項(xiàng)中的TextView組件 - 與此同時(shí),我們還需要實(shí)現(xiàn)三個(gè)方法
onCreateViewHolder:選擇使用哪一個(gè) layout 作為 viewholder,并放回該 layout 組成的 viewholder
onBindViewHolder:針對(duì) 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 注冊(cè)
初始化 recyclerview 的方式和 listview 差不多,只不過(guò)這里多設(shè)置了一個(gè) layoutManager
依葫蘆畫(huà)瓢即可!
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使用入門(mén)介紹的文章就介紹到這了,更多相關(guān)Android RecyclerView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過(guò)程
- Android RecyclerView實(shí)現(xiàn)吸頂動(dòng)態(tài)效果流程分析
- Android RecyclerView四級(jí)緩存源碼層詳細(xì)分析
- Android RecyclerView緩存復(fù)用原理解析
- Android開(kāi)發(fā)RecyclerView單獨(dú)刷新使用技巧
- Android開(kāi)發(fā)RecyclerView實(shí)現(xiàn)折線圖效果
- Android?手寫(xiě)RecyclerView實(shí)現(xiàn)列表加載
- Android獲取RecyclerView滑動(dòng)距離方法詳細(xì)講解
相關(guān)文章
Android實(shí)現(xiàn)收到新短信后自動(dòng)發(fā)郵件功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)收到新短信后自動(dòng)發(fā)郵件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Android仿餓了么加入購(gòu)物車(chē)旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫(huà)的按鈕效果(實(shí)例詳解)
這篇文章主要介紹了Android仿餓了么加入購(gòu)物車(chē)旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫(huà)的按鈕效果(實(shí)例詳解)的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android登錄記住多個(gè)密碼的實(shí)現(xiàn)方法
通過(guò)在popouWindow里面加上ListView,數(shù)據(jù)是把List以字符串按照J(rèn)SON的樣式存入本地,下面通過(guò)實(shí)例代碼給大家介紹android登錄記住多個(gè)密碼的實(shí)現(xiàn)方法,需要的的朋友參考下吧2017-07-07
Android 坐標(biāo)系與視圖坐標(biāo)系圖解分析
下面小編就為大家?guī)?lái)一篇Android 坐標(biāo)系與視圖坐標(biāo)系圖解分析。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
解決Android Studio Design界面不顯示layout控件的問(wèn)題
這篇文章主要介紹了解決Android Studio Design界面不顯示layout控件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android實(shí)現(xiàn)類(lèi)似iOS風(fēng)格的對(duì)話框?qū)嵗a
通過(guò)本文給大家分享一個(gè)簡(jiǎn)單的常用的對(duì)話框類(lèi),關(guān)于Android實(shí)現(xiàn)類(lèi)似iOS風(fēng)格的對(duì)話框?qū)嵗a大家通過(guò)本文學(xué)習(xí)下吧2017-09-09

