Kotlin中ListView與RecyclerView的應(yīng)用講解
寫下來自己以后看:
先是item的布局文件:
里邊放了一個(gè)圖片和一個(gè)文本框
<?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:id="@+id/linearLayout"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
ListView:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListViewActivity">
<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
適配器:
class FruitAdapter(private val context: Context, private val list : List<Fruit>) : BaseAdapter() {
override fun getCount(): Int = list.size
override fun getItem(position: Int): Any = list[position]
override fun getItemId(position: Int): Long = position.toLong()
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
var convertView = convertView
var holder : ViewHolder? = null
if (convertView == null){
holder = ViewHolder()
convertView = View.inflate(context,R.layout.item_list_view,null)
holder.textView = convertView.findViewById<View>(R.id.textView) as TextView
holder.imageView = convertView.findViewById<View>(R.id.imageView) as ImageView
holder.linearLayout = convertView.findViewById<View>(R.id.linearLayout) as LinearLayout
convertView.tag = holder
}else{
holder = convertView.tag as ViewHolder
}
holder.textView!!.text = list[position].name
holder.imageView!!.setImageResource(list[position].image)
holder.linearLayout!!.setOnClickListener {
Toast.makeText(context,list[position].name,Toast.LENGTH_SHORT).show()
}
return convertView
}
internal class ViewHolder{
var textView : TextView? = null
var imageView : ImageView? = null
var linearLayout : LinearLayout? = null
}
}
剩下的就是邏輯處理:
class ListViewActivity : AppCompatActivity() {
private val bean = ArrayList<Fruit>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list_view)
for (i in 1..100){
bean.add(Fruit(i.toString(),R.drawable.ic_launcher_foreground))
}
val adapter = FruitAdapter(this,bean)
listView.adapter = adapter
}
}
RecyclerView:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
適配器:
class FruitRecyclerViewAdapter(private val context: Context,private val list: List<Fruit>) : RecyclerView.Adapter<FruitRecyclerViewAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view : View = LayoutInflater.from(context).inflate(R.layout.item_list_view,null)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.textView.text = list[position].name
holder.itemView.imageView.setImageResource(list[position].image)
holder.itemView.linearLayout.setOnClickListener {
Toast.makeText(context,list[position].name,Toast.LENGTH_SHORT).show()
}
}
override fun getItemCount(): Int = list.size
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val textView : TextView = itemView.findViewById(R.id.textView)
private val imageView : ImageView = itemView.findViewById(R.id.imageView)
private val linearLayout : LinearLayout = itemView.findViewById(R.id.linearLayout)
}
}
邏輯代碼:
class RecyclerViewActivity : AppCompatActivity() {
private val bean = ArrayList<Fruit>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycler_view)
repeat(3){
for (i in 1..15){
bean.add(Fruit(i.toString(),R.drawable.ic_launcher_foreground))
}
}
val layoutManger = LinearLayoutManager(this)
//layoutManger.orientation = LinearLayoutManager.HORIZONTAL
recyclerView.layoutManager = layoutManger
val adapter = FruitRecyclerViewAdapter(this,bean)
recyclerView.adapter = adapter
}
}
這里的repeat函數(shù)是重復(fù)三次,意思就是會(huì)有三個(gè)1到15,也就是此recyclerView會(huì)有45個(gè)item.
現(xiàn)在的是縱向滑動(dòng)的,如果要改成橫向的,就把我代碼中的注釋掉的
//layoutManger.orientation = LinearLayoutManager.HORIZONTAL
取消注釋就可以實(shí)現(xiàn)橫向滑動(dòng)了,如果不嫌棄難看,布局文件就不用改。
最后是實(shí)體類:
class Fruit(val name : String,val image : Int) {
}
定義了一個(gè)name用來顯示名字,定義了一個(gè)image,用來顯示圖片。
到此這篇關(guān)于Kotlin中ListView與RecyclerView的應(yīng)用講解的文章就介紹到這了,更多相關(guān)Kotlin中ListView與RecyclerView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)斷點(diǎn)多線程下載
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)斷點(diǎn)多線程下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android?imageVIew實(shí)現(xiàn)鏡像旋轉(zhuǎn)的方法
在Android應(yīng)用開發(fā)中,有時(shí)候我們需要對(duì)ImageView中的圖片進(jìn)行鏡像旋轉(zhuǎn),以展示不同的效果,本文將介紹如何使用代碼實(shí)現(xiàn)ImageView的鏡像旋轉(zhuǎn)效果,這篇文章主要介紹了Android?imageVIew如何做鏡像旋轉(zhuǎn),需要的朋友可以參考下2024-06-06
Flutter Set存儲(chǔ)自定義對(duì)象時(shí)保證唯一的方法詳解
在Flutter中,Set和List是兩種不同的集合類型,List中存儲(chǔ)的元素可以重復(fù),Set中存儲(chǔ)的元素不可重復(fù),如果想在Set中存儲(chǔ)自定義對(duì)象,需要確保對(duì)象的唯一性,那么如何保證唯一,接下來小編就給大家詳細(xì)的介紹一下2023-11-11
Android仿知乎客戶端關(guān)注和取消關(guān)注的按鈕點(diǎn)擊特效實(shí)現(xiàn)思路詳解
這篇文章主要介紹了Android仿知乎客戶端關(guān)注和取消關(guān)注的按鈕點(diǎn)擊特效實(shí)現(xiàn)思路詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android使用Flutter實(shí)現(xiàn)錄音插件
這篇文章主要介紹了基于flutter實(shí)現(xiàn)錄音功能,介紹了如何錄音,如何把文件存放到本地,這些都是我們平常使用這個(gè)功能會(huì)遇到的問題。在使用的過程中遇到的問題也有列出,需要的朋友可以參考下2022-08-08
Android開發(fā)使用UncaughtExceptionHandler捕獲全局異常
本文主要介紹在Android開發(fā)中使用UncaughtExceptionHandler捕獲全局異常,需要的朋友可以參考下。2016-06-06
Android開發(fā)之merge結(jié)合include優(yōu)化布局
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之merge結(jié)合include優(yōu)化布局,感興趣的朋友可以參考一下2016-06-06
Flutter?Android多窗口方案落地實(shí)戰(zhàn)
這篇文章主要為大家介紹了Flutter?Android多窗口方案落地實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

