Blog purpose for android basic example for android app developer. any query please my contact

Thursday, 24 December 2020

kotlin recyclerview in android example

 kotlin recyclerview in android example


In this Article today lern kotlin recyclerview in android. follow the full source code kotlin recyclerview in android


1.build.gradle:

 implementation 'androidx.recyclerview:recyclerview:1.1.0'


2.MainActivity.kt:

import android.support.v7.app.AppCompatActivity

import android.os.Bundle

import android.support.v7.widget.GridLayoutManager

import android.support.v7.widget.LinearLayoutManager

import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {


    // Initializing an empty ArrayList to be filled with animals

    val animals: ArrayList<String> = ArrayList()


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        // Loads animals into the ArrayList

        addAnimals()


        // Creates a vertical Layout Manager

        rv_animal_list.layoutManager = LinearLayoutManager(this)


        // You can use GridLayoutManager if you want multiple columns. Enter the number of columns as a parameter.

//        rv_animal_list.layoutManager = GridLayoutManager(this, 2)


        // Access the RecyclerView Adapter and load the data into it

        rv_animal_list.adapter = AnimalAdapter(animals, this)


    }


    // Adds animals to the empty animals ArrayList

    fun addAnimals() {

        animals.add("dog")

        animals.add("cat")

        animals.add("owl")

        animals.add("cheetah")

        animals.add("raccoon")

        animals.add("bird")

        animals.add("snake")

        animals.add("lizard")

        animals.add("hamster")

        animals.add("bear")

        animals.add("lion")

        animals.add("tiger")

        animals.add("horse")

        animals.add("frog")

        animals.add("fish")

        animals.add("shark")

        animals.add("turtle")

        animals.add("elephant")

        animals.add("cow")

        animals.add("beaver")

        animals.add("bison")

        animals.add("porcupine")

        animals.add("rat")

        animals.add("mouse")

        animals.add("goose")

        animals.add("deer")

        animals.add("fox")

        animals.add("moose")

        animals.add("buffalo")

        animals.add("monkey")

        animals.add("penguin")

        animals.add("parrot")


    }


}


3.AnimalAdapter.kt:

import android.content.Context

import android.support.v7.widget.RecyclerView

import android.view.LayoutInflater

import android.view.View

import android.view.ViewGroup

import kotlinx.android.synthetic.main.animal_list_item.view.*


class AnimalAdapter(val items : ArrayList<String>, val context: Context) : RecyclerView.Adapter<ViewHolder>() {


    // Gets the number of animals in the list

    override fun getItemCount(): Int {

        return items.size

    }


    // Inflates the item views

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {

        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.animal_list_item, parent, false))

    }


    // Binds each animal in the ArrayList to a view

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {

        holder?.tvAnimalType?.text = items.get(position)

    }

}


class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {

        // Holds the TextView that will add each animal to

        val tvAnimalType = view.tv_animal_type

}



4.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.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="com.example.recylerview.tutorial.MainActivity">


    <androidx.support.v7.widget.RecyclerView


        android:id="@+id/rv_animal_list"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />


</androidx.support.constraint.ConstraintLayout>



5.animal_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="wrap_content">


    <TextView

        android:id="@+id/tv_animal_type"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="20sp"

        android:padding="20dp"/>


</LinearLayout>


6.AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.recylerview.tutorial">


    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>


</manifest>


No comments:

Post a Comment