This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KotlinViewModelProvider { | |
companion object { | |
inline fun <reified T : ViewModel> of(fragment: Fragment, crossinline factory: () -> T): T { | |
@Suppress("UNCHECKED_CAST") | |
val vmFactory = object : ViewModelProvider.Factory { | |
override fun <U : ViewModel> create(modelClass: Class<U>): U = factory() as U | |
} | |
return ViewModelProviders.of(fragment, vmFactory)[T::class.java] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
var age: Int = 0 | |
} | |
//Usage | |
val person = Person() | |
person.age = 20 | |
println(person.age) //prints 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
private var age: Int = 0 | |
fun getAge(): Int { | |
return age | |
} | |
fun setAge(age: Int) { | |
this.age = age | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val view = View() | |
view.beforeTextChangedListener = { /* do something */ } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class View { | |
var beforeTextChangedListener: (() -> Unit)? = null | |
var afterTextChangedListener: (() -> Unit)? = null | |
var onTextChangedListener: (() -> Unit)? = null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val view = View() | |
view.setBeforeTextChangedListener ({ /* do something */ }) | |
//... | |
// later in code we can set another listener | |
view.setAfterTextChangedListener ({ /* do something */ }) | |
//no need to set 3rd listener |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface TextWatcher { | |
public void beforeTextChanged(CharSequence s, int start, | |
public void onTextChanged(CharSequence s, int start, int before, int count); | |
public void afterTextChanged(Editable s); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val view = View() | |
view.setBeforeTextChangedListener { /* do something */ } | |
//... | |
// later in code we can set another listener | |
view.setAfterTextChangedListener { /* do something */ } | |
//no need to set 3rd listener |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class View { | |
private var beforeTextChangedListener: (() -> Unit)? = null | |
private var afterTextChangedListener: (() -> Unit)? = null | |
private var onTextChangedListener: (() -> Unit)? = null | |
fun setBeforeTextChangedListener(listener: () -> Unit) { | |
beforeTextChangedListener = listener | |
} | |
fun setAfterTextChangedListener(listener: () -> Unit) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
textView.addTextChangedListener(new TextWatcher() { | |
public void afterTextChanged(Editable s) { | |
//do something | |
} | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
//do something | |
} |