Upgrade to Pro — share decks privately, control downloads, hide ads and more …

10 Extension Functions of Kotlin in Android Dev...

magiepooh
November 06, 2015

10 Extension Functions of Kotlin in Android Development

10 Extension Functions of Kotlin in Android Development

magiepooh

November 06, 2015
Tweet

More Decks by magiepooh

Other Decks in Technology

Transcript

  1. Kotlin • By JetBrains • Lambdas -> Retrolambda • Nullable(NonNull)

    • Higher-Order Functions (Stream) • Extension Functions
  2. Kotlin • By JetBrains • Lambdas -> Retrolambda • Nullable(NonNull)

    -> Optional • Higher-Order Functions (Stream) • Extension Functions
  3. Kotlin • By JetBrains • Lambdas -> Retrolambda • Nullable(NonNull)

    -> Optional • Higher-Order Functions (Stream) -> RxJava • Extension Functions
  4. Kotlin • By JetBrains • Lambdas -> Retrolambda • Nullable(NonNull)

    -> Optional • Higher-Order Functions (Stream) -> RxJava • Extension Functions
  5. fun Activity.toast(text: String) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show() } override fun

    onCreate(savedInstanceState : Bundle?) { toast(“Hello, Toast!”) } Extension Functions
  6. setFragment fun FragmentActivity.setFragment(fragment: Fragment, containerId: Int = android.R.id.content) = supportFragmentManager

    .beginTransaction() .add(containerId, fragment) .commit() setFragment(MainFragment.newInstance(), android.R.id.content)
  7. setFragment fun FragmentActivity.setFragment(fragment: Fragment, containerId: Int = android.R.id.content) = supportFragmentManager

    .beginTransaction() .add(containerId, fragment) .commit() setFragment(MainFragment.newInstance())
  8. setFragment fun FragmentActivity.setFragment(fragment: Fragment, containerId: Int = android.R.id.content) = supportFragmentManager

    .beginTransaction() .add(containerId, fragment) .commit() setFragment(MainFragment.newInstance()) setFragment(MainFragment.newInstance(), R.id.my_content)
  9. let

  10. isAttachedToWindow fun View.isAttachedToWindowCompat () = if (Build.VERSION.SDK_INT < 19) {

    ViewCompat.isAttachedToWindow(this) } else { isAttachedToWindow() }
  11. WindowManager fun Context.getWindowManager() = getSystemService(Context.WINDOW_SERVICE) as WindowManager val windowManager =

    getWindowManager() val display = windowManager.getDefaultDisplay() val width = display.width val height = display.height
  12. WindowManager fun Context.getWindowManager() = getSystemService(Context.WINDOW_SERVICE) as WindowManager val windowManager =

    getWindowManager() val display = windowManager.getDefaultDisplay() val width = display.width val height = display.height
  13. WindowManager val Context.windowManager = get() = getSystemService(Context.WINDOW_SERVICE) as WindowManager val

    windowManager = getWindowManager() val display = windowManager.getDefaultDisplay() val width = display.width val height = display.height
  14. WindowManager val Context.windowManager = get() = getSystemService(Context.WINDOW_SERVICE) as WindowManager val

    display = windowManager.getDefaultDisplay() val width = display.width val height = display.height
  15. Glide Extension fun RequestManager.loadOptimize(url: String?, width: Int, height: Int): DrawableTypeRequest<String>

    = load(url?.let { "$it?width=$width&height=$height" }) Glide.with(context) .loadOptimize(url, 300, 300) .into(imageView)
  16. ViewPager# OnPageChangeListener(Java) pager.addOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position)

    { // no op } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // no op } @Override public void onPageScrollStateChanged(int state) { onStateChange(state); } });
  17. ViewPager# OnPageChangeListener(Java) pager.addOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position)

    { // no op } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // no op } @Override public void onPageScrollStateChanged(int state) { onStateChange(state); } });
  18. ViewPager# OnPageChangeListener(Java) pager.addOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position)

    { // no op } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // no op } @Override public void onPageScrollStateChanged(int state) { onStateChange(state); } });
  19. fun ViewPager.addOnPageChangeListener( onPageScrolled: (position: Int, positionOffset: Float, positionOffsetPixels: Int) ->

    Unit = { position, positionOffset, positionOffsetPixels -> }, onPageScrollStateChanged: (state: Int) -> Unit = {}, onPageSelected: (position: Int) -> Unit = {} ) = addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { onPageScrolled(position, positionOffset, positionOffsetPixels) } override fun onPageScrollStateChanged(state: Int) { onPageScrollStateChanged(state) } override fun onPageSelected(position: Int) { onPageSelected(position) } })
  20. OnPageChangeListener(Java) pager.addOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) {

    // no op } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // no op } @Override public void onPageScrollStateChanged(int state) { onStateChange(state); } });
  21. Animator fun Animator.addListener( onAnimationStart: (Animator) -> Unit = {}, onAnimationEnd:

    (Animator) -> Unit = {}, onAnimationCancel: (Animator) -> Unit = {}, onAnimationRepeat: (Animator) -> Unit = {}): Animator { addListener(object : Animator.AnimatorListener { override fun onAnimationStart(animation: Animator) { onAnimationStart(animation) } // ….. }) return this }
  22. Animator fun Animator.addListener( onAnimationStart: (Animator) -> Unit = {}, onAnimationEnd:

    (Animator) -> Unit = {}, onAnimationCancel: (Animator) -> Unit = {}, onAnimationRepeat: (Animator) -> Unit = {}): Animator { addListener(object : Animator.AnimatorListener { override fun onAnimationStart(animation: Animator) { onAnimationStart(animation) } // ….. }) return this }
  23. Animator fun Animator.addListener( onAnimationStart: (Animator) -> Unit = {}, onAnimationEnd:

    (Animator) -> Unit = {}, onAnimationCancel: (Animator) -> Unit = {}, onAnimationRepeat: (Animator) -> Unit = {}): Animator { addListener(object : Animator.AnimatorListener { override fun onAnimationStart(animation: Animator) { onAnimationStart(animation) } // ….. }) return this }