Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.58 KB | None | 0 0
  1. package com.example.elite.kate_suvorova_shop
  2.  
  3. import android.content.Context
  4. import android.graphics.Color
  5. import android.os.Bundle
  6. import android.support.v7.app.AppCompatActivity
  7. import android.support.v7.widget.LinearLayoutManager
  8. import android.support.v7.widget.RecyclerView
  9. import android.view.Gravity
  10. import android.view.ViewGroup
  11. import android.widget.FrameLayout
  12. import android.widget.GridLayout
  13. import android.widget.TextView
  14. import org.jetbrains.anko.*
  15. import org.jetbrains.anko.recyclerview.v7.recyclerView
  16.  
  17. class ProductsActivity : AppCompatActivity() {
  18.  
  19.  
  20.     override fun onCreate(savedInstanceState: Bundle?) {
  21.         super.onCreate(savedInstanceState)
  22.         verticalLayout {
  23.             backgroundColor = Color.parseColor("#f7f2ef")
  24.  
  25.             val item1 = Product(title = "Кольцо Рубин", price = 200.55)
  26.             val item2 = Product(title = "Кольцо Beyonce", price = 100.55)
  27.             val item3 = Product(title = "Кольцо Future", price = 300.55)
  28.             val item4 = Product(title = "Кольцо Лето", price = 230.55)
  29.  
  30.             val vegetables = listOf(item1, item2, item3, item4)
  31.  
  32.             recyclerView {
  33.                 layoutManager = LinearLayoutManager(this@ProductsActivity)
  34.                 adapter       = ProductsAdapter(products = vegetables, context = this@ProductsActivity)
  35.             }
  36.         }
  37.     }
  38. }
  39.  
  40. class ProductsAdapter(
  41.         val products: List<Product>,
  42.         val context: Context
  43.  ) :RecyclerView.Adapter<ProductViewHolder>() {
  44.         override fun getItemCount() = products.size
  45.  
  46.         override fun onCreateViewHolder(recyclerView: ViewGroup, viewType: Int) = run {
  47.  
  48.             // Чтобы создать ProductView, нам нужен Context
  49.             val itemView = ProductView(context)
  50.  
  51.             // Теперь в ячейке лежит ProductView
  52.             ProductViewHolder(view = itemView)
  53.      }
  54.  
  55.     override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
  56.         val product = products[position]
  57.  
  58.         // holder.view теперь имеет тип ProductView
  59.         // У ProductView есть 2 константы: titleView и priceView.
  60.         // Они имеют тип TextView. Задаём этим TextView новый text
  61.         holder.view.titleView.text = product.title
  62.  
  63.         // toString() - функция, превращающая дробное число в текст
  64.         holder.view.priceView.text = product.price.toString()+"$"
  65.  
  66.     }
  67. }
  68. // ProductView является FrameLayout. Для его создания необходим
  69. // Context, который для Android
  70. class ProductView(context: Context) : FrameLayout(context) {
  71.  
  72.     lateinit var titleView: TextView // lateinit означает, что переменной
  73.     lateinit var priceView: TextView // не нужно задавать начальное значение
  74.  
  75.     init { // код, который выполнится при создании каждого объекта ProductView
  76.  
  77.         // Задаём параметры макета для ProductView.
  78.         // То же самое, что и lparams
  79.         layoutParams = LayoutParams( matchParent, wrapContent)
  80.         (layoutParams as LayoutParams).setMargins(10, 10, 10, 0);
  81.  
  82.         // Описание интерфейса ячейки
  83.         verticalLayout {
  84.             backgroundColor = Color.parseColor("#fefefe")
  85.             padding = dip(10)
  86.  
  87.             titleView = textView {
  88.                 textSize  = 16f
  89.                 textColor = Color.BLACK
  90.             }
  91.  
  92.             imageView {
  93.                 setImageResource(R.drawable.i0)
  94.             }.lparams{
  95.                     gravity = Gravity.TOP or Gravity.CENTER
  96.                     margin  = dip(30)
  97.                     width   = dip(200)
  98.                     height  = dip(200)
  99.                 }
  100.  
  101.             priceView = textView {
  102.                 textSize  = 16f
  103.             }.lparams {
  104.                     gravity = Gravity.END
  105.               }
  106.         }
  107.     }
  108. }
  109.  
  110. class Product(val title: String, val price: Double)
  111.  
  112. class ProductViewHolder(val view: ProductView) : RecyclerView.ViewHolder(view)
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. /*
  123. import android.content.Context
  124. import android.graphics.Color
  125. import android.support.v7.app.AppCompatActivity
  126. import android.os.Bundle
  127. import android.support.v7.widget.DialogTitle
  128. import android.support.v7.widget.LinearLayoutManager
  129. import android.support.v7.widget.RecyclerView
  130. import android.view.ContextMenu
  131. import android.view.ViewGroup
  132. import android.widget.TextView
  133. import kotlinx.coroutines.experimental.android.UI
  134. import org.jetbrains.anko.UI
  135. import org.jetbrains.anko.recyclerview.v7.recyclerView
  136. import org.jetbrains.anko.textColor
  137. import org.jetbrains.anko.textView
  138.  
  139. class ProductsActivity : AppCompatActivity() {
  140.  
  141.     override fun onCreate(savedInstanceState: Bundle?) {
  142.         super.onCreate(savedInstanceState)
  143.  
  144.         val rings      = Product(title = "Кольца")
  145.         val earrings   = Product(title = "Серьги")
  146.         val pendant    = Product(title = "Кулон")
  147.  
  148.         val jewerly    = listOf(rings, earrings, pendant)
  149.  
  150.  
  151.  
  152.         recyclerView {
  153.             layoutManager = LinearLayoutManager(this@ProductsActivity)
  154.             adapter       = ProductsAdapter(products = jewerly, context = this@ProductsActivity)
  155.         }
  156.     }
  157. }
  158.  
  159. class Product(val title: String)
  160.  
  161. class ProductViewHolder(val view: TextView) : RecyclerView.ViewHolder(view)
  162.  
  163. class ProductsAdapter(
  164.     val products:  List<Product>,
  165.     val context:   Context
  166. ) :
  167.         RecyclerView.Adapter<ProductViewHolder>() {
  168.  
  169.             override fun onCreateViewHolder(recyclerView: ViewGroup, viewtype: Int) = run{
  170.                 val itemUI = context.UI {
  171.                     textView{
  172.                         text = "My text"
  173.                         textColor = Color.parseColor("#4c8e9e")
  174.                     }
  175.                 }
  176.                 ProductViewHolder(view = itemUI.view as TextView)
  177.             }
  178.  
  179.             override fun getItemCount() = products.size
  180.  
  181.             override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
  182.                 val product = products[position]
  183.                 holder.view.text = product.title
  184.             }
  185.         }
  186.  
  187. /*
  188. fun main(){
  189.  
  190.     val peter = Human()
  191.  
  192.     class Panda{
  193.         fun food(){
  194.             peter.feed(panda = this)
  195.         }
  196.     }
  197.  
  198.     class Human{
  199.         fun feed(panda: Panda){
  200.             print("*Panda*: Hrum-hrum")
  201.         }
  202.     }
  203. }
  204.  
  205. */
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement