Skip to content

Instantly share code, notes, and snippets.

@SurajBahadur
Created August 14, 2019 07:32
Show Gist options
  • Save SurajBahadur/c4e4df4783837247e46a0b0bea37bd21 to your computer and use it in GitHub Desktop.
Save SurajBahadur/c4e4df4783837247e46a0b0bea37bd21 to your computer and use it in GitHub Desktop.
Set item click in adapter with the use of function parameter
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*
* A LinearLayoutManager is responsible for measuring and positioning item views within a
* RecyclerView into a linear list. This means that it can produce either a horizontal or
* vertical list depending on which parameter you pass in to the LinearLayoutManager
* constructor. By default, if you don't specify an orientation, you get a vertical list.
* In our case, we want a vertical list, so we don't need to pass in an orientation flag to
* the LinearLayoutManager constructor.
*
* There are other LayoutManagers available to display your data in uniform grids,
* staggered grids, and more! See the developer documentation for more details.
*/
rv_parts.layoutManager = LinearLayoutManager(this)
/*
* Use this setting to improve performance if you know that changes in content do not
* change the child layout size in the RecyclerView
*/
rv_parts.setHasFixedSize(true)
val testData = createTestData()
//rv_parts.adapter = PartAdapter(testData)
// Create the PartAdapter
// 1st parameter: our generated testData
// 2nd parameter: item click handler function (implemented below) as function parameter
rv_parts.adapter = PartAdapter(testData, { partItem : PartData -> partItemClicked(partItem) })
// ---------------------------------------------------------
// Kotlin Language Features
// Create new class instance
val calcTest = ClassWithConstructorProperties(10, 20)
// Print calculation results
Log.d("Tests", "Calculation result: " + calcTest.calculate())
// Call a function, supplying a lambda to the function parameter
testFunctionParameters( {a : Int, b : Int -> a + b } )
}
private fun partItemClicked(partItem : PartData) {
Toast.makeText(this, "Clicked: ${partItem.itemName}", Toast.LENGTH_LONG).show()
// Launch second activity, pass part ID as string parameter
val showDetailActivityIntent = Intent(this, PartDetailActivity::class.java)
showDetailActivityIntent.putExtra(Intent.EXTRA_TEXT, partItem.id.toString())
startActivity(showDetailActivityIntent)
}
private fun createTestData() : List<PartData> {
val partList = ArrayList<PartData>()
partList.add(PartData(100411, "LED Green 568 nm, 5mm"))
partList.add(PartData(101119, "Aluminium Capacitor 4.7μF"))
partList.add(PartData(101624, "Potentiometer 500kΩ"))
return partList
}
/**
* Defines a class with a constructor. Its parameters are automatically available
* as properties in the class. Note that the keyword "constructor" is optional
* and could be stripped.
*/
class ClassWithConstructorProperties constructor (var a: Int, var b: Int) {
fun calculate() : Int {
return a + b;
}
}
private fun testFunctionParameters(performCalculation: (Int, Int) -> Int) {
Log.d("Tests", "Calculation result: " + performCalculation(1, 2))
}
}
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.part_list_item.view.*
class PartAdapter (val partItemList: List<PartData>, val clickListener: (PartData) -> Unit) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
// LayoutInflater: takes ID from layout defined in XML.
// Instantiates the layout XML into corresponding View objects.
// Use context from main app -> also supplies theme layout values!
val inflater = LayoutInflater.from(parent.context)
// Inflate XML. Last parameter: don't immediately attach new view to the parent view group
val view = inflater.inflate(R.layout.part_list_item, parent, false)
return PartViewHolder(view)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
// Populate ViewHolder with data that corresponds to the position in the list
// which we are told to load
(holder as PartViewHolder).bind(partItemList[position], clickListener)
}
override fun getItemCount() = partItemList.size
class PartViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(part: PartData, clickListener: (PartData) -> Unit) {
itemView.tv_part_item_name.text = part.itemName
itemView.tv_part_id.text = part.id.toString()
itemView.setOnClickListener { clickListener(part)}
}
}
}
data class PartData ( val id: Long, val itemName: String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment