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

明日からちょっと KotlinでAndroidが書きたくなる(かもしれない?)SAM変換と...

明日からちょっと KotlinでAndroidが書きたくなる(かもしれない?)SAM変換と拡張関数

2014/07/04 かわいいKotlin勉強会で行った発表の資料です

※ 元々はSlideShareで公開していた資料です。

RyotaMurohoshi

July 14, 2014
Tweet

More Decks by RyotaMurohoshi

Other Decks in Technology

Transcript

  1. ͍Βͬͱ͢Δ৑௕ͳίʔυ̍ button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

    Log.v(TAG, "clicked"); } }); ͲΜͳAndroidೖ໳ॻʹ΋ࡌ͍ͬͯΔɺΞΫςΟϏςΟͰΑ͘ΈΔίʔυ KotlinͰॻ͍ͨ΍ͭͱൺ΂Δͱɺ৑௕ͳͷ͕Ұ໨ྎવʂ
  2. JavaͷίʔυΛ΋͏Ұ౓ݟͯΈ·͠ΐ͏ button.setOnClickListener(new View.OnClickListener() { // newҎ߱ຊ࣭͡Όͳ͍ @Override // <- ຊ࣭͡Όͳ͍

    public void onClick(View v) { // <- ຊ࣭͡Όͳ͍ Log.v(TAG, "clicked!"); } // <- ຊ࣭͡Όͳ͍ }); // <- ຊ࣭͡Όͳ͍ Java͸ຊ࣭͡Όͳ͍෦෼͕ଟ͍ɻKotlin͸ຊ࣭ͷΈΛهड़͢Ε͹͍͍ Kotlin͸ͳͥ͋Μͳهड़͕Ͱ͖Δͷ͔ʁ
  3. SAMม׵Ͱ͖Δྫ button.setOnClickListener( { (v : View): Unit -> Log.v(TAG, "clicked")

    }) button.setOnClickListener( { v -> Log.v(TAG, "clicked")}) button.setOnClickListener{ v -> Log.v(TAG, "clicked") } button.setOnClickListener{ Log.v(TAG, "clicked") } val listener : (View) -> Unit = {v -> Log.v(TAG, "clicked") } //or val listener : (View) -> Unit = { Log.v(TAG, "click") } //or val listener = { (v : View) : Unit -> Log.v(TAG, "clicked") } button.setOnClickListener(listener)
  4. SAMม׵Ͱ͖ͳ͍ྫ // Լه͸ίϯύΠϧΤϥʔ // Type mismatch val listener : View.OnClickListener

    = { (v : View) : Unit -> Log.v(TAG, "clicked") } // Լه͸࣮ߦ࣌Τϥʔ // java.lang.ClassCastException val listener = { (v : View) : Unit -> Log.v(TAG, "clicked") } as View.OnClickListener Ҿ਺ʹؔ਺ϦςϥϧΛ౉͞ͳ͍ͱ͍͚ͳ͍
  5. VolleyͷStringRequest0Java൛ StringRequest request = new StringRequest( "https://www.google.co.jp/", new Response.Listener<String>() {

    @Override public void onResponse(String response) { Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(getApplicationContext(), "onErrorResponse", Toast.LENGTH_LONG).show(); } } ); ແବͳ෦෼͕ଟ͘ɺॎʹ௕͍ɽɽɽ
  6. VolleyͷStringRequest0Kotlin൛ val request = StringRequest( url = "https://www.google.co.jp/", listener =

    { response -> Toast.makeText(this, response, Toast.LENGTH_LONG).show() }, errorListener = { volleyError -> Toast.makeText(this, "onErrorResponse", Toast.LENGTH_LONG).show() } ); ܕύϥʔϝʔλ͕͋ΔΠϯλʔϑΣʔε΋SAMม׵Մೳ ໊લ෇͖Ҿ਺ͰՄಡੑΞοϓ
  7. ͪΐͬͱGroovy΋ΈͯΈ·͠ΐ͏ Groovy΋SAMม׵͕࢖͑ΔΑ͏Ͱ͢ɻ button.setOnClickListener { v -> Toas.makeText(this, "click", Toast.LENGTH_LONG).show() }

    Groovy&2.2͔Βasԋࢉࢠ͕͍Βͳ͘ͳͬͨͦ͏Ͱ͢ɻ h"p:/ /groovy.codehaus.org/Groovy+2.2+release+notes
  8. ͪΐͬͱGroovy΋ΈͯΈ·͠ΐ͏ SAMΠϯλʔϑΣʔε͚ͩͰͳ͘ɺෳ਺ϝιουΛ࣋ͭΠϯλʔϑΣʔε΋ Map+Ϋϩʔδϟʔ+asԋࢉࢠͰ͜Μͳײ͡ʹʂ viewPager.setOnPageChangeListener ([ onPageScrollStateChanged: { state -> Log.v(TAG,

    state) }, onPageScrolled : { position, positionOffset, positionOffsetPixels -> /*ུ*/ }, onPageSelected : { position -> Log.v(TAG, position) } ] as ViewPager.OnPageChangeListener) h"p:/ /groovy.codehaus.org/Groovy+way+to+implement+interfaces
  9. ͱΓ͋͑ͣܧঝ͠·͢ʁ Ac#vityΛܧঝͨ͠ΫϥεBaseAc#vityʹͯɺ͜Μͳϝιουఆٛ͢Ε͹ɺ public <T extends View> T findById(int viewId) {

    return (T)findViewById(viewId); } BaseAc'vityΛߋʹܧঝͨ͠ΫϥεͰ͸ɺ͜Μͳײ͡ͰΩϟετෆཁʹͳΓ·͢Ͷ ImageView imageView = findById(R.id.image_view);
  10. Extension)Func-onsͷྫ)ར༻ଆ package com.mrstar.android_with_kotlin // ུ import com.mrstar.extensions.findById // <- ஫໨

    public class MainActivity() : FragmentActivity() { // <- ஫໨ protected override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val textView: ImageView = findById (R.id.image_view) // <- ஫໨ } }
  11. ͪΐͬͱC#΋ΈͯΈ·͠ΐ͏ C#ʹ΋֦ுؔ਺ͱಉ͡Α͏ͳ֦ுϝιου͕͋Γ·͢ɻ // ఆٛଆ public static class StringExtensions { public

    static string Decorate(this string str, string symbol) { return string.Format("{0}{1}{2}", symbol, str, symbol); } } //ར༻ଆ string decoratedMessage = "Hello!".Decorate("===") ͜Μͳײ͡Ͱɺsta$cͳΫϥεʹsta$cͳϝιουͱ֦ͯ͠ுϝιουΛఆٛ͠·͢ɻ