
RecyclerView is used to load views to the activity when you don’t know how many views we need, or have a large number of individual item views to display in one activity. It saves memory by reusing views when you scroll in an activity instead of creating them all at the beginning when the activity first loads.
Loading all views at the beginning wastes memory since there will be many views loaded that cannot be seen on the screen. When developing an app, you always want to be aware of the memory cost of the features in your app. Android devices have limited memory, and your app will not be the only one running on a user’s device
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".MainActivity" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rec"/> </LinearLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.androindian.rec import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { //internal var recyclerView: RecyclerView internal var Names = arrayOf("AAAA", "BBB") internal var Number = arrayOf("9988", "9999") internal var Images = intArrayOf(R.mipmap.ic_launcher, R.mipmap.ic_launcher_round) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //recyclerView = findViewById(R.id.rec) val layoutManager = LinearLayoutManager( this@MainActivity, LinearLayoutManager.HORIZONTAL, false) rec.layoutManager = layoutManager rec.adapter = RecAdp( this@MainActivity, Names, Number, Images) } } |
RecAdp.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.androindian.rec import android.content.Context import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import kotlinx.android.synthetic.main.custom.view.* class RecAdp(mainActivity: MainActivity, internal var Cnames: Array<String>, internal var Cnumber: Array<String>, internal var Cimages: IntArray) : RecyclerView.Adapter<RecAdp.MyViewHolder>() { internal var context: Context init { context = mainActivity } override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): RecAdp.MyViewHolder { val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val view = inflater.inflate(R.layout.custom, null) return MyViewHolder(view) } override fun onBindViewHolder(myViewHolder: RecAdp.MyViewHolder, i: Int) { myViewHolder.tv1.text = Cnames[i] myViewHolder.tv2.text = Cnumber[i] myViewHolder.iv.setImageResource(Cimages[i]) } override fun getItemCount(): Int { return Cimages.size } inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { var tv1 = itemView.tv1 var tv2=itemView.tv2 var iv=itemView.iv } } |
custom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv2"/> </LinearLayout> </LinearLayout> |
Output