Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. import android.content.Intent
  2. import android.os.Bundle
  3. import androidx.appcompat.app.AppCompatActivity
  4. import androidx.recyclerview.widget.LinearLayoutManager
  5. import android.util.Log
  6. import android.widget.Toast
  7. import kotlinx.android.synthetic.main.activity_main.*
  8.  
  9. class MainActivity : AppCompatActivity() {
  10.  
  11. override fun onCreate(savedInstanceState: Bundle?) {
  12. super.onCreate(savedInstanceState)
  13. setContentView(R.layout.activity_main)
  14.  
  15. /*
  16. * A LinearLayoutManager is responsible for measuring and positioning item views within a
  17. * RecyclerView into a linear list. This means that it can produce either a horizontal or
  18. * vertical list depending on which parameter you pass in to the LinearLayoutManager
  19. * constructor. By default, if you don't specify an orientation, you get a vertical list.
  20. * In our case, we want a vertical list, so we don't need to pass in an orientation flag to
  21. * the LinearLayoutManager constructor.
  22. *
  23. * There are other LayoutManagers available to display your data in uniform grids,
  24. * staggered grids, and more! See the developer documentation for more details.
  25. */
  26. rv_parts.layoutManager = LinearLayoutManager(this)
  27. /*
  28. * Use this setting to improve performance if you know that changes in content do not
  29. * change the child layout size in the RecyclerView
  30. */
  31. rv_parts.setHasFixedSize(true)
  32. val testData = createTestData()
  33. //rv_parts.adapter = PartAdapter(testData)
  34.  
  35. // Create the PartAdapter
  36. // 1st parameter: our generated testData
  37. // 2nd parameter: item click handler function (implemented below) as function parameter
  38. rv_parts.adapter = PartAdapter(testData, { partItem : PartData -> partItemClicked(partItem) })
  39.  
  40.  
  41. // ---------------------------------------------------------
  42. // Kotlin Language Features
  43.  
  44. // Create new class instance
  45. val calcTest = ClassWithConstructorProperties(10, 20)
  46. // Print calculation results
  47. Log.d("Tests", "Calculation result: " + calcTest.calculate())
  48.  
  49. // Call a function, supplying a lambda to the function parameter
  50. testFunctionParameters( {a : Int, b : Int -> a + b } )
  51. }
  52.  
  53. private fun partItemClicked(partItem : PartData) {
  54. Toast.makeText(this, "Clicked: ${partItem.itemName}", Toast.LENGTH_LONG).show()
  55.  
  56. // Launch second activity, pass part ID as string parameter
  57. val showDetailActivityIntent = Intent(this, PartDetailActivity::class.java)
  58. showDetailActivityIntent.putExtra(Intent.EXTRA_TEXT, partItem.id.toString())
  59. startActivity(showDetailActivityIntent)
  60. }
  61.  
  62.  
  63. private fun createTestData() : List<PartData> {
  64. val partList = ArrayList<PartData>()
  65. partList.add(PartData(100411, "LED Green 568 nm, 5mm"))
  66. partList.add(PartData(101119, "Aluminium Capacitor 4.7μF"))
  67. partList.add(PartData(101624, "Potentiometer 500kΩ"))
  68. return partList
  69. }
  70.  
  71. /**
  72. * Defines a class with a constructor. Its parameters are automatically available
  73. * as properties in the class. Note that the keyword "constructor" is optional
  74. * and could be stripped.
  75. */
  76. class ClassWithConstructorProperties constructor (var a: Int, var b: Int) {
  77. fun calculate() : Int {
  78. return a + b;
  79. }
  80. }
  81.  
  82. private fun testFunctionParameters(performCalculation: (Int, Int) -> Int) {
  83. Log.d("Tests", "Calculation result: " + performCalculation(1, 2))
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement