Permalink
Browse files

Merge branch 'feature/lambdas' into develop

  • Loading branch information...
gonzapico committed Apr 22, 2017
2 parents e61f6cf + 0cd8d07 commit 28ffd938b74022aeafea15de011e603b6d701afb
@@ -53,4 +53,29 @@ inline fun <reified V : View> View.find(@IdRes resId: Int): V = findViewById(res
inline fun <reified V : View> Activity.find(@IdRes resId: Int): V = findViewById(resId) as V
inline fun <reified V : View> RecyclerView.ViewHolder.find(@IdRes resId: Int): V = itemView.find<V>(
resId)
resId)
/*** A function that passes by parameter that is an extension function ***/
fun <T : Any> T.applyTwo(f: T.() -> Unit): T {
this.f()
// return itself because that's the way apply works
return this
}
fun <T : Any> T.withTwo(obj: T, f: T.() -> Unit): T {
obj.f()
return obj
}
fun ViewGroup.filterViews(condition: (View) -> Boolean): List<View> {
// Default list are inmutable
// Inmutable list empty just created
val list = mutableListOf<View>()
for (i in 0 until childCount) {
val view = getChildAt(i)
if (condition(view)) {
list.add(view)
}
}
return list
}
@@ -2,17 +2,29 @@ package xyz.gonzapico.myfirstkotlinapp
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.activity_home.*
class HomeActivity : AppCompatActivity() {
val f: (Int) -> Int = { x ->
x * x
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
// id = recycler
recycler.adapter = MediaAdapter(fetchMedia())
recycler.adapter = MediaAdapter(fetchMedia()) {
// This is valid just if there is one input parameter
showToast(it.title)
}
val viewGroup: ViewGroup = FrameLayout(this)
val list: List<View> = viewGroup.filterViews(View::visible)
}
}
}
@@ -3,23 +3,26 @@ package xyz.gonzapico.myfirstkotlinapp
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.view_media_item.view.*
import kotlinx.android.synthetic.main.view_media_item.view.media_title as mediaTitle
/**
* Created by gfernandez on 22/04/17.
*/
class MediaAdapter(
val mediaItemList: List<MediaItem>) : RecyclerView.Adapter<MediaAdapter.MediaViewHolder>() {
val mediaItemList: List<MediaItem>,
val listener: (MediaItem) -> Unit) : RecyclerView.Adapter<MediaAdapter.MediaViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MediaViewHolder {
return MediaViewHolder(parent.inflate(R.layout.view_media_item))
}
override fun onBindViewHolder(holder: MediaViewHolder, position: Int) {
holder.bind(mediaItemList[position])
holder.itemView.setOnClickListener {
listener(mediaItemList[position])
// listener.invoke(mediaItemList[position])
}
}
override fun getItemCount() =
@@ -31,19 +34,6 @@ class MediaAdapter(
mediaTitle.text = item.title
media_thumb.loadUrl(item.thumbUrl)
media_video_indicator.visible = item.type == MediaItem.TYPE.VIDEO
setOnClickListener { showToast(item.title, Toast.LENGTH_LONG) }
// Java
val tv = TextView(itemView.context)
tv.text = "a comer"
tv.textSize = 20f
// Kotlin
val tv2 = TextView(itemView.context).apply {
text = "a comer"
textSize = 20f
}
// it returns itself
}
}
}

0 comments on commit 28ffd93

Please sign in to comment.