Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ekaterina_sarycheva_shop
- import android.content.Context
- import android.graphics.Color
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.support.v7.widget.LinearLayoutCompat
- import android.support.v7.widget.LinearLayoutManager
- import android.support.v7.widget.RecyclerView
- import android.view.Gravity
- import android.view.ViewGroup
- import android.widget.FrameLayout
- import android.widget.ImageView
- import android.widget.TextView
- import org.jetbrains.anko.*
- import org.jetbrains.anko.recyclerview.v7.recyclerView
- class ProductsActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- val tomato = Product(title = "Помидор", price = 42.99)
- val cucumber = Product(title = "Огурец", price = 56.99)
- val potato = Product(title = "Картошка", price = 21.99)
- val vegetables = listOf(tomato, cucumber, potato)
- recyclerView {
- layoutManager = LinearLayoutManager(this@ProductsActivity)
- adapter = ProductsAdapter(products = vegetables, context = this@ProductsActivity)
- }
- }
- }
- class ProductsAdapter(val products: List<Product>, val context: Context): RecyclerView.Adapter<ProductViewHolder>(){
- override fun getItemCount() = products.size
- override fun onCreateViewHolder(recyclerView: ViewGroup, viewType: Int) = run{
- val itemView = ProductView(context)
- ProductViewHolder(view = itemView)
- }
- override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
- val product = products[position]
- holder.view.imgView.text = null
- holder.view.titleView.text = product.title
- holder.view.priceView.text = product.price.toString()
- }
- }
- class ProductView(context: Context) : FrameLayout(context) {
- lateinit var imgView: TextView
- lateinit var titleView: TextView
- lateinit var priceView: TextView
- init { // код, который выполнится при создании каждого объекта ProductView
- // Задаём параметры макета для ProductView.
- // То же самое, что и lparams
- layoutParams = LayoutParams(matchParent, wrapContent)
- // Описание интерфейса ячейки
- frameLayout {
- linearLayout {
- imgView = textView {
- width = dip(120)
- height = dip(120)
- backgroundColor = Color.BLUE
- }.lparams {
- horizontalMargin = dip(20)
- topMargin = dip(40)
- }
- linearLayout {
- titleView = textView {
- backgroundColor = Color.GRAY
- }.lparams {
- topMargin = dip(90)
- horizontalMargin = dip(20)
- }
- }
- priceView = textView {
- backgroundColor = Color.RED
- }.lparams {
- topMargin = dip(90)
- horizontalMargin = dip(20)
- }
- }
- }
- }
- }
- class Product(val title: String, val price: Double)
- class ProductViewHolder(val view: ProductView) : RecyclerView.ViewHolder(view)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement