Kotlin?RecyclerView滾動(dòng)控件詳解
Android 中的控件非常的豐富,我們會(huì)陸陸續(xù)續(xù)的進(jìn)行介紹,從第九節(jié)開(kāi)始,關(guān)于Kotlin 的語(yǔ)法特性就差不多結(jié)束,后面如果有發(fā)現(xiàn)需要說(shuō)明的語(yǔ)法,再進(jìn)行相關(guān)的補(bǔ)充。
在Android的控件中,RecyclerView算是一個(gè)大控件,基本上所有的大型項(xiàng)目都會(huì)使用到。因?yàn)樗淖饔檬怯昧斜淼姆绞絹?lái)展現(xiàn)相關(guān)的信息,比如我們是做新聞?lì)惖?,我們可以用它?lái)展示一條一條的圖文信息,我們做商品類的,那么我們可以用來(lái)展現(xiàn)商品的重要信息,可以說(shuō)RecyclerView 就是一個(gè)信息展示器。
這里我們也用 原生的RecyclerView 做了一個(gè)簡(jiǎn)單的,動(dòng)漫人物展示,這是只是展示RecyclerView的用法,如果列表需要更多的內(nèi)容,可以自己進(jìn)行相應(yīng)的添加設(shè)計(jì)。

RecyclerView.Adapter適配器
對(duì)于列表的展示,我們需要靈活的操作,我們需要自己訂閱每一類的信息展示形式,這樣一來(lái)在做設(shè)計(jì)的時(shí)候,就可以根據(jù)產(chǎn)品的形式進(jìn)行自己的設(shè)計(jì)。
Adapter 適配器 就是為了滿足我們這樣的設(shè)計(jì),基本上會(huì)配合xml界面進(jìn)行設(shè)計(jì)。
我們先來(lái)看看Adapter 適配器的自定義。
class NarutoListAdapter(var narutoList:ArrayList<Naruto>):RecyclerView.Adapter<NarutoListAdapter.ViewHolder>() {
inner class ViewHolder(view:View):RecyclerView.ViewHolder(view){
var narutoPic:ImageView = view.findViewById(R.id.naruto_pic)
var narutoName:TextView = view.findViewById(R.id.naruto_name)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.naruto_item_layout,parent,false)
var viewHolder = ViewHolder(view as View)
viewHolder.itemView.setOnClickListener{
var position = viewHolder.adapterPosition
var naruto = narutoList[position]
Toast.makeText(parent.context,"你點(diǎn)擊的是${naruto.name}",Toast.LENGTH_SHORT).show()
}
return viewHolder
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val naruto = narutoList[position]
holder.narutoPic.setImageResource(naruto.imageId)
holder.narutoName.setText(naruto.name)
}
override fun getItemCount(): Int {
return narutoList.size
}
}適配器 NarutoListAdapter 要重載3個(gè)方法,onCreateViewHolder 就是你要怎么設(shè)計(jì)這個(gè)界面,onBindViewHolder 每個(gè)界面的數(shù)據(jù)是什么,getItemCount 總共有多少條數(shù)據(jù)。
有了這三個(gè)重載方法,我們基本就搞定了列表的顯示。
在onCreateViewHolder 加載界面的時(shí)候,會(huì)跟xml的界面進(jì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="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
>
<ImageView
android:id="@+id/naruto_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/naruto_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/black"
android:textSize="16sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
界面這里也非常的簡(jiǎn)單,只有兩個(gè)控件ImageView 和TextView,這個(gè)界面在現(xiàn)實(shí)的項(xiàng)目中,需要根據(jù)自己的需求設(shè)計(jì)的飽滿點(diǎn),這里只是一個(gè)簡(jiǎn)單的展示而已。
主界面顯示RecyclerView
有了適配器,我們?cè)谥鹘缑嬷校€需要用一個(gè)RecyclerView 用來(lái)顯示相關(guā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"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="使用RecycleView的實(shí)例"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleViewItem"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>界面中的控件 RecyclerView 也有了,我們需要把數(shù)據(jù)給適配器,并綁定適配器。
initData()
recycleViewItem.layoutManager = LinearLayoutManager(this)
var adapter = NarutoListAdapter(narutoList)
recycleViewItem.adapter = adapter
數(shù)據(jù)初始化如下:
fun initData(){
narutoList.add(Naruto(" 漩渦鳴人 ",R.drawable.image_1))
narutoList.add(Naruto(" 宇智波佐助 ",R.drawable.image_2))
narutoList.add(Naruto(" 小櫻 ",R.drawable.image_3))
narutoList.add(Naruto(" 旗木卡卡西 ",R.drawable.image_4))
narutoList.add(Naruto(" 奈良鹿丸 ",R.drawable.image_5))
narutoList.add(Naruto(" 日向雛田 ",R.drawable.image_6))
narutoList.add(Naruto(" 山中井野 ",R.drawable.image_7))
narutoList.add(Naruto(" 秋道丁次 ",R.drawable.image_8))
narutoList.add(Naruto(" 猿飛阿斯瑪 ",R.drawable.image_9))
narutoList.add(Naruto(" 犬冢牙 ",R.drawable.image_10))
narutoList.add(Naruto(" 油女志乃 ",R.drawable.image_11))
narutoList.add(Naruto(" 夕日紅 ",R.drawable.image_12))
narutoList.add(Naruto(" 李洛克 ",R.drawable.image_13))
narutoList.add(Naruto(" 日向?qū)幋?",R.drawable.image_14))
narutoList.add(Naruto(" 天天 ",R.drawable.image_15))
narutoList.add(Naruto(" 邁特凱 ",R.drawable.image_16))
narutoList.add(Naruto(" 自來(lái)也 ",R.drawable.image_17))
narutoList.add(Naruto(" 大蛇丸 ",R.drawable.image_18))
narutoList.add(Naruto(" 綱手 ",R.drawable.image_19))
narutoList.add(Naruto(" 赤丸 ",R.drawable.image_20))
narutoList.add(Naruto(" 大和 ",R.drawable.image_21))
narutoList.add(Naruto(" 志村團(tuán)藏 ",R.drawable.image_22))
narutoList.add(Naruto(" 佐井 ",R.drawable.image_23))
narutoList.add(Naruto(" 千手柱間 ",R.drawable.image_24))
narutoList.add(Naruto(" 宇智波斑 ",R.drawable.image_25))
narutoList.add(Naruto(" 千手扉間 ",R.drawable.image_26))
narutoList.add(Naruto(" 波風(fēng)水門 ",R.drawable.image_27))
narutoList.add(Naruto(" 伊魯卡 ",R.drawable.image_28))
narutoList.add(Naruto(" 猿飛日斬 ",R.drawable.image_29))
narutoList.add(Naruto(" 玖辛奈 ",R.drawable.image_30))
}第三方RecyclerView之SmartRefreshLayout
在大型的項(xiàng)目中,我們通常使用的是第三方的RecyclerView ,因?yàn)樗墓δ鼙仍亩嗟枚啵矣闷饋?lái)一點(diǎn)也不費(fèi)勁,RecyclerView我們經(jīng)常用到的第三方控件 是SmartRefreshLayout,github的地址在這里。可以先看看里面的說(shuō)明在動(dòng)手吧。

小結(jié)
RecyclerView 是很常用的一個(gè)android 控件,我們可以通過(guò)對(duì)原生控件的使用來(lái)看第三個(gè)控件的用法,這樣用起來(lái)心里就有數(shù)多了。
到此這篇關(guān)于Kotlin RecyclerView滾動(dòng)控件詳解的文章就介紹到這了,更多相關(guān)Kotlin RecyclerView 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android廣播接實(shí)現(xiàn)監(jiān)聽(tīng)電話狀態(tài)(電話的狀態(tài),攔截)
這篇文章主要介紹了Android廣播接實(shí)現(xiàn)監(jiān)聽(tīng)電話狀態(tài)(電話的狀態(tài),攔截) 的相關(guān)資料,需要的朋友可以參考下2016-03-03
官網(wǎng)項(xiàng)目Jetpack?Startup庫(kù)學(xué)習(xí)
這篇文章主要為大家介紹了官網(wǎng)項(xiàng)目Jetpack?Startup庫(kù)學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
詳解Android原生json和fastjson的簡(jiǎn)單使用
本文主要介紹了Android原生json和fastjson的簡(jiǎn)單使用,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
Android App多個(gè)入口的實(shí)現(xiàn)方法
這篇文章主要介紹了Android App多個(gè)入口的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
去掉RecycleView或者ListView上下滑動(dòng)陰影的方法
下面小編就為大家分享一篇去掉RecycleView或者ListView上下滑動(dòng)陰影的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android編程開(kāi)發(fā)音樂(lè)播放器實(shí)例
這篇文章主要介紹了Android編程開(kāi)發(fā)音樂(lè)播放器,結(jié)合實(shí)例形式分析了Android音樂(lè)播放器開(kāi)發(fā)所涉及的SeekBar,ListView,廣播接收者(以代碼的形式注冊(cè)Receiver),系統(tǒng)服務(wù),MediaPlayer等技巧,需要的朋友可以參考下2016-01-01
Android實(shí)時(shí)獲取攝像頭畫面?zhèn)鬏斨罰C端思路詳解
這篇文章主要介紹了Android實(shí)時(shí)獲取攝像頭畫面?zhèn)鬏斨罰C端思路詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
分享一個(gè)輕量級(jí)圖片加載類 ImageLoader
這篇文章給大家分享一個(gè)輕量級(jí)圖片加載類 ImageLoader,需要的朋友可以參考下2016-08-08

