In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about the specific use of RecyclerView in Android, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The basic usage of RecyclerView
Unlike the control we learned before, RecyclerView is a new control, so we need to add the dependency of the RecyclerView library to the project's build.gradle before we can use it.
Dependencies {implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.2.0' implementation' androidx.appcompat:appcompat:1.1.0' implementation 'com.google.android.material:material:1.1.0' implementation' androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.recyclerview:recyclerview:1.1.0' testImplementation' junit:junit:4.+' AndroidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation' androidx.test.espresso:espresso-core:3.2.0'}
Modify the code in activity_main.xml as follows
Here we want to use RecyclerView to achieve the same effect as ListView, prepare an adapter, create a new FruitAdapter class, let the adapter inherit RecyclerView.Adapter, and specify the generics as FruitAdapter.ViewHolder, where ViewHolder is an inner class we defined in FruitAdapter
Class FruitAdapter (val fruitList: List): RecyclerView.Adapter () {inner class ViewHolder (view: View): RecyclerView.ViewHolder (view) {val fruitImage: ImageView = view.findViewById (R.id.fruitImage) val fruitName: TextView = view.findViewById (R.id.fruitName)} override fun onCreateViewHolder (parent: ViewGroup, viewType: Int): FruitAdapter.ViewHolder {val view = LayoutInflater.from (parent.context) .expiate (R.layout.fruit_item, parent) False) return ViewHolder (view)} override fun getItemCount () = fruitList.size override fun onBindViewHolder (holder: FruitAdapter.ViewHolder, position: Int) {val fruit = fruitList [position] holder.fruitImage.setImageResource (fruit.imageId) holder.fruitName.text = fruit.name}}
First, we define an inner class ViewHolder, which inherits from RecyclerView.ViewHolder, and then pass in a View parameter in the main constructor of ViewHolder, which is usually the outermost layout of the RecyclerView subitem. Then we can get the ImageView and TextView instances in the layout through the findViewById () method.
FruitAdapter inherits from RecyclerView.Adapter, so you must override the three methods onCreateViewHolder (), onBindViewHolder (), and getItemCount ():
The onCreateViewHolder () method is used to create a ViewHolder instance
The onBindViewHolder () method is used to assign values to the data of RecyclerView subitems.
The getItemCount () method tells RecyclerView how many children there are, and returns the length of the data source directly.
Modify the code in MainActivity to start using RecyclerView
Class MainActivity: AppCompatActivity () {privateval fruitList = ArrayList () override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) initFruits () val layoutManager = LinearLayoutManager (this) recyclerView.layoutManager = layoutManager val adapter = FruitAdapter (fruitList) recyclerView.adapter = adapter} private fun initFruits () {repeat (2) {fruitList.add (Fruit ("Apple") R.drawable.apple_pic)) fruitList.add (Fruit ("Banana", R.drawable.banana_pic)) fruitList.add (Fruit ("Orange", R.drawable.orange_pic)) fruitList.add (Fruit ("Watermelon", R.drawable.watermelon_pic)) fruitList.add (Fruit ("Pear", R.drawable.pear_pic)) fruitList.add (Fruit ("Grape") R.drawable.grape_pic)) fruitList.add (Fruit ("Pineapple", R.drawable.pineapple_pic)) fruitList.add (Fruit ("Strawberry", R.drawable.strawberry_pic)) fruitList.add (Fruit ("Cherry", R.drawable.cherry_pic)) fruitList.add (Fruit ("Mango", R.drawable.mango_pic))}
Create a LinearLayoutManager object in the onCreate () method and set it to RecyclerView to specify how the RecyclerView is laid out. Then create an instance of FruitAdapter and call the setAdapter () method of RecyclerView to complete the adapter setting.
Horizontal scrolling
If we want to scroll horizontally, we must first modify the layout of the fruit_item and change the elements to be arranged vertically.
Next, modify the code in MainActivity
Class MainActivity: AppCompatActivity () {privateval fruitList = ArrayList () override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) initFruits () val layoutManager = LinearLayoutManager (this) layoutManager.orientation = LinearLayoutManager.HORIZONTAL recyclerView.layoutManager = layoutManager val adapter = FruitAdapter (fruitList) recyclerView.adapter = adapter}.}
In addition to scrolling horizontally, RecyclerView can also implement waterfall flow and grid layout
RecyclerView Click event
Unlike ListView,RecyclerView, which does not provide a method of registering listeners like setOnItemClickListener (), we need to register click events for the specific View of the subkey ourselves.
Modify the code in FruitAdapter
Class FruitAdapter (val fruitList: List): RecyclerView.Adapter () {inner class ViewHolder (view: View): RecyclerView.ViewHolder (view) {val fruitImage: ImageView = view.findViewById (R.id.fruitImage) val fruitName: TextView = view.findViewById (R.id.fruitName)} override fun onCreateViewHolder (parent: ViewGroup, viewType: Int): FruitAdapter.ViewHolder {val view = LayoutInflater.from (parent.context) .expiate (R.layout.fruit_item, parent) False) val viewHolder = ViewHolder (view) viewHolder.itemView.setOnClickListener {val position = viewHolder.adapterPosition Log.d ("FruitAdapter", position.toString ()) val fruit = fruitList [position] Toast.makeText (parent.context, "you clicked view ${fruit.name}" Toast.LENGTH_SHORT) .show ()} viewHolder.fruitImage.setOnClickListener {val position = viewHolder.adapterPosition Log.d ("FruitAdapter", position.toString ()) val fruit = fruitList [position] Toast.makeText (parent.context, "you clicked image ${fruit.name}" Toast.LENGTH_SHORT) .show ()} return viewHolder} override fun getItemCount () = fruitList.size override fun onBindViewHolder (holder: FruitAdapter.ViewHolder, position: Int) {val fruit = fruitList [position] holder.fruitImage.setImageResource (fruit.imageId) holder.fruitName.text = fruit.name}} above are the specific uses of RecyclerView in Android The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.