溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Kotlin中ListView與RecyclerView怎么用

發(fā)布時(shí)間:2021-09-13 11:06:19 來(lái)源:億速云 閱讀:257 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下Kotlin中ListView與RecyclerView怎么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!


先是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用來(lái)顯示名字,定義了一個(gè)image,用來(lái)顯示圖片。

看完了這篇文章,相信你對(duì)“Kotlin中ListView與RecyclerView怎么用”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI