Skip to content

Instantly share code, notes, and snippets.

@marconvcm
Last active November 30, 2018 23:17
Show Gist options
  • Save marconvcm/7c439bdc6ab6504a32393403dcf42c33 to your computer and use it in GitHub Desktop.
Save marconvcm/7c439bdc6ab6504a32393403dcf42c33 to your computer and use it in GitHub Desktop.
package com.example.marconvcm.mylist
import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
class MainActivity : AppCompatActivity() {
val listaDeDesejos by lazy {
findViewById<RecyclerView>(R.id.listaDeDesejos)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val produtos = listOf(
Produto(id = 1, title = "Produto 1", qty = 1f, price = 10f),
Produto(id = 2, title = "Produto 2", qty = 1f, price = 10f),
Produto(id = 3, title = "Produto 3", qty = 1f, price = 10f),
Produto(id = 4, title = "Produto 4", qty = 1f, price = 10f), Produto(id = 1, title = "Produto 1", qty = 1f, price = 10f),
Produto(id = 2, title = "Produto 2", qty = 1f, price = 10f),
Produto(id = 3, title = "Produto 3", qty = 1f, price = 10f),
Produto(id = 4, title = "Produto 4", qty = 1f, price = 10f), Produto(id = 1, title = "Produto 1", qty = 1f, price = 10f),
Produto(id = 2, title = "Produto 2", qty = 1f, price = 10f),
Produto(id = 3, title = "Produto 3", qty = 1f, price = 10f),
Produto(id = 4, title = "Produto 4", qty = 1f, price = 10f), Produto(id = 1, title = "Produto 1", qty = 1f, price = 10f),
Produto(id = 2, title = "Produto 2", qty = 1f, price = 10f),
Produto(id = 3, title = "Produto 3", qty = 1f, price = 10f),
Produto(id = 4, title = "Produto 4", qty = 1f, price = 10f)
)
val adapter = ListaDesejoAdapter(
this, produtos
)
adapter.onItemClick = View.OnClickListener { view ->
val position = listaDeDesejos.getChildLayoutPosition(view)
val produto = produtos[position];
// TODO: enviar para tela de produto
}
listaDeDesejos.layoutManager = LinearLayoutManager(this)
listaDeDesejos.adapter = adapter
adapter.notifyDataSetChanged()
}
}
class ListaDesejoAdapter(
val context: Context,
val produtos: List<Produto>): RecyclerView.Adapter<ItemDesejoViewHolder>() {
var onItemClick: (View.OnClickListener)? = null
override fun onCreateViewHolder(group: ViewGroup, position: Int): ItemDesejoViewHolder {
val layoutInflater = this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
val view = layoutInflater.inflate(
R.layout.view_item_desejo, group,
false)
view.setOnClickListener(this.onItemClick)
val viewHolder = ItemDesejoViewHolder(view)
return viewHolder
}
override fun getItemCount(): Int {
return produtos.count()
}
override fun onBindViewHolder(holder: ItemDesejoViewHolder, position: Int) {
val produto = this.produtos.get(position)
holder.priceTextView.text = produto.price.toString()
holder.titleTextView.text = produto.title.toString()
holder.quantityTextView.text = produto.qty.toString()
}
}
data class Produto (
val id: Int,
val title: String,
val qty: Float,
val price: Float
)
class ItemDesejoViewHolder(view: View):
RecyclerView.ViewHolder(view) {
val titleTextView by lazy {
view.findViewById<TextView>(R.id.titleTextView)
}
val quantityTextView by lazy {
view.findViewById<TextView>(R.id.quantityTextView)
}
val priceTextView by lazy {
view.findViewById<TextView>(R.id.priceTextView)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment