Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.41 KB | None | 0 0
  1. package com.example.ekaterina_sarycheva_shop
  2.  
  3. import android.content.Context
  4. import android.graphics.Color
  5. import android.support.v7.app.AppCompatActivity
  6. import android.os.Bundle
  7. import android.support.v7.widget.LinearLayoutCompat
  8. import android.support.v7.widget.LinearLayoutManager
  9. import android.support.v7.widget.RecyclerView
  10. import android.view.Gravity
  11. import android.view.ViewGroup
  12. import android.widget.FrameLayout
  13. import android.widget.ImageView
  14. import android.widget.TextView
  15. import org.jetbrains.anko.*
  16. import org.jetbrains.anko.recyclerview.v7.recyclerView
  17.  
  18. class ProductsActivity : AppCompatActivity() {
  19.  
  20.     override fun onCreate(savedInstanceState: Bundle?) {
  21.         super.onCreate(savedInstanceState)
  22.  
  23.         val tomato = Product(title = "Помидор", price = 42.99)
  24.         val cucumber = Product(title = "Огурец", price = 56.99)
  25.         val potato = Product(title = "Картошка", price = 21.99)
  26.  
  27.         val vegetables = listOf(tomato, cucumber, potato)
  28.  
  29.         recyclerView {
  30.             layoutManager = LinearLayoutManager(this@ProductsActivity)
  31.             adapter = ProductsAdapter(products = vegetables, context = this@ProductsActivity)
  32.         }
  33.     }
  34. }
  35.  
  36.  
  37. class ProductsAdapter(val products: List<Product>, val context: Context): RecyclerView.Adapter<ProductViewHolder>(){
  38.  
  39.     override fun getItemCount() = products.size
  40.  
  41.     override fun onCreateViewHolder(recyclerView: ViewGroup, viewType: Int) = run{
  42.         val itemView = ProductView(context)
  43.         ProductViewHolder(view = itemView)
  44.     }
  45.  
  46.     override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
  47.         val product = products[position]
  48.         holder.view.imgView.text = null
  49.         holder.view.titleView.text = product.title
  50.         holder.view.priceView.text = product.price.toString()
  51.     }
  52. }
  53. class ProductView(context: Context) : FrameLayout(context) {
  54.     lateinit var imgView: TextView
  55.     lateinit var titleView: TextView
  56.     lateinit var priceView: TextView
  57.  
  58.     init { // код, который выполнится при создании каждого объекта ProductView
  59.  
  60.         // Задаём параметры макета для ProductView.
  61.         // То же самое, что и lparams
  62.         layoutParams = LayoutParams(matchParent, wrapContent)
  63.  
  64.         // Описание интерфейса ячейки
  65.         frameLayout {
  66.             linearLayout {
  67.                 imgView = textView {
  68.                     width = dip(120)
  69.                     height = dip(120)
  70.                     backgroundColor = Color.BLUE
  71.                 }.lparams {
  72.                     horizontalMargin = dip(20)
  73.                     topMargin = dip(40)
  74.                 }
  75.  
  76.                 linearLayout {
  77.                     titleView = textView {
  78.                         backgroundColor = Color.GRAY
  79.                     }.lparams {
  80.                         topMargin = dip(90)
  81.                         horizontalMargin = dip(20)
  82.                     }
  83.                 }
  84.  
  85.                 priceView = textView {
  86.                     backgroundColor = Color.RED
  87.                 }.lparams {
  88.                     topMargin = dip(90)
  89.                     horizontalMargin = dip(20)
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }
  95.  
  96. class Product(val title: String, val price: Double)
  97.  
  98. class ProductViewHolder(val view: ProductView) : RecyclerView.ViewHolder(view)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement