Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ListView and RecyclerView in Kotlin

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use ListView and RecyclerView in Kotlin. I hope you will get something after reading this article. Let's discuss it together.

First, the layout file of item:

There is a picture and a text box inside.

ListView:

Layout file:

Adapter:

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.textViewroom.text = list [position] .name Holder.imageViewroom.setImageResource (listposition] .image) holder.roomLayoutLayoutListener .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}}

The rest is logical processing:

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:

Layout file:

Adapter:

Class FruitRecyclerViewAdapter (privateval context: Context,privateval list: List): RecyclerView.Adapter () {override fun onCreateViewHolder (parent: ViewGroup, viewType: Int): ViewHolder {val view: View = LayoutInflater.from (context) .inherate (R.layout.itemological listings: 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)}}

Logic code:

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}}

The repeat function here is repeated three times, which means there will be three 1s to 15, that is, the recyclerView will have 45 item.

The current one is sliding vertically. If you want to change it to horizontal, please comment out the comments in my code.

/ / layoutManger.orientation = LinearLayoutManager.HORIZONTAL

Uncomment and slide horizontally, and if you don't hate ugliness, you don't have to change the layout file.

Finally, there are entity classes:

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

Define a name to display the name, and define an image to display the picture.

After reading this article, I believe you have a certain understanding of "how to use ListView and RecyclerView in Kotlin". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report