Skip to content

Instantly share code, notes, and snippets.

@SergeyBurlaka
Created November 20, 2018 09:31
Show Gist options
  • Save SergeyBurlaka/5a14573cca50e2ec704bf327142088bc to your computer and use it in GitHub Desktop.
Save SergeyBurlaka/5a14573cca50e2ec704bf327142088bc to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_image" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_animal_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
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.setText(items[position])
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
// Holds the TextView that will add each animal to
val tvAnimalType = view.tv_animal_type
}
package com.b5eg.sergburlaka.keyboard
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
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
private 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
private 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")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment