Application Explanation of ListView and RecyclerView in Kotlin

  • 2021-12-19 06:39:37
  • OfStack

Write it down and read it later:
First, the layout file of item:
There is a picture and a text box inside


<?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:
Layout file:


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

Adapter:


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

The rest is logical processing:


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:
Layout file:


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

Adapter:


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

Logic code:


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

The repeat function here is repeated three times, which means that there will be three ones to fifteen, which means that this recyclerView will have 45 item.
Now it slides vertically. If you want to change it to horizontal, comment out the ones in my code
//layoutManger.orientation = LinearLayoutManager.HORIZONTAL
You can realize horizontal sliding by canceling comments. If you don't dislike ugliness, you don't need to change the layout file.
Finally, there are entity classes:


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

Defines 1 name to display the name and 1 image to display the picture.


Related articles: