千家信息网

Kotlin中ListView与RecyclerView怎么用

发表于:2025-01-17 作者:千家信息网编辑
千家信息网最后更新 2025年01月17日,小编给大家分享一下Kotlin中ListView与RecyclerView怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!先是item的布局文件:里边放了一个图片和一个文本框
千家信息网最后更新 2025年01月17日Kotlin中ListView与RecyclerView怎么用

小编给大家分享一下Kotlin中ListView与RecyclerView怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!


先是item的布局文件:
里边放了一个图片和一个文本框

        

ListView:
布局文件:

    

适配器:

class FruitAdapter(privateval context: Context, privateval list : List) : 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(R.id.textView) as TextView            holder.imageView = convertView.findViewById(R.id.imageView) as ImageView            holder.linearLayout = convertView.findViewById(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() {    privateval bean = ArrayList()    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:
布局文件:

    

适配器:

class FruitRecyclerViewAdapter(privateval context: Context,privateval list: List) : RecyclerView.Adapter() {    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) {        privateval textView : TextView = itemView.findViewById(R.id.textView)        privateval imageView : ImageView = itemView.findViewById(R.id.imageView)        privateval linearLayout : LinearLayout = itemView.findViewById(R.id.linearLayout)    }}

逻辑代码:

class RecyclerViewActivity : AppCompatActivity() {    privateval bean = ArrayList()    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函数是重复三次,意思就是会有三个1到15,也就是此recyclerView会有45个item.
现在的是纵向滑动的,如果要改成横向的,就把我代码中的注释掉的
//layoutManger.orientation = LinearLayoutManager.HORIZONTAL
取消注释就可以实现横向滑动了,如果不嫌弃难看,布局文件就不用改。
最后是实体类:

class Fruit(val name : String,val image : Int) {}

定义了一个name用来显示名字,定义了一个image,用来显示图片。

看完了这篇文章,相信你对"Kotlin中ListView与RecyclerView怎么用"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

0