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

kotlin season 1 - Part 14 RecyclerView와 Retrofit

TaeHwan
November 13, 2016

kotlin season 1 - Part 14 RecyclerView와 Retrofit

2016년 유데미 Kotlin 강의를 공개합니다.

동영상 : https://www.youtube.com/watch?v=vWUzCi5CHL4

2회 분은 유데미에서 확인 가능합니다.
http://bit.ly/2UpaJUR

TaeHwan

November 13, 2016
Tweet

More Decks by TaeHwan

Other Decks in Education

Transcript

  1. ࢠ೒ ௏٘ח!!! • Sample Code GitHub ࠳۠஖ : 09-Retrofit-RecyclerView •

    https://github.com/taehwandev/Kotlin-Udemy-Sample/tree/ 09-Retrofit-RecyclerView ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 2
  2. য়ט ೡ ࠗ࠙ • Flickr APIীࢲ ੹׳߉਷ ؘ੉ఠܳ RecyclerViewী јन

    • kotlin extension рױೞѱ ࢓ಝࠁӝ ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 3
  3. Retrofit੄ interface ੿੄ interface FlickrServiceInterface { @POST("?method=flickr.interestingness.getList&format=json&nojsoncallback=1&api_key=" + BuildConfig.FLICKR_API_KEY) fun

    getFlickrRecentPhotos( @Query("page") page: Int): Call<PhotoResponse> } ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 5
  4. Retrofitҗ োѾೞӝ object PhotoDataSource { val FLICKR_URL = "https://api.flickr.com/services/rest/" private

    val flickrServiceInterface: FlickrServiceInterface init { flickrServiceInterface = createRetrofit(FlickrServiceInterface::class.java, FLICKR_URL) } fun getRecentPhoto(page: Int) = flickrServiceInterface.getFlickrRecentPhotos(page) } ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 6
  5. Retrofitীࢲ ߉਷ ؘ੉ఠ ୊ܻ photoDataSample?.getRecentPhoto(++page) ?.enqueue(object : Callback<PhotoResponse> { override

    fun onResponse(call: Call<PhotoResponse>?, response: Response<PhotoResponse>?) { // ࢿҕೞ৓ਸ ҃਋ ୊ܻ } override fun onFailure(call: Call<PhotoResponse>?, t: Throwable?) { // पಁೞ৓ਸ ҃਋ ୊ܻ } }) ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 7
  6. RecyclerView.Adapterীࢲ ࢎਊೡ Model/ Viewܳ ੿੄ interface ImageViewAdapterContract { interface View

    { fun reload() } interface Model { fun addItem(item: PhotoItem) } } ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 9
  7. RecyclerView.Adapterী ࢚ࣘ߉ӝ class ImageAdapter(private val context: Context) : RecyclerView.Adapter<ImageViewHolder>(), ImageViewAdapterContract.View,

    ImageViewAdapterContract.Model ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 10
  8. Retrofitਵ۽ ߉਷ ؘ੉ఠܳ Adapterী ࣇ౴ೞҊ, reload val photoResponse = response?.body()

    if (photoResponse?.stat.equals("ok")) { photoResponse?.photos?.photo?.forEach { adapterModel?.addItem(it) } } ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 12
  9. Kotlin extension apply plugin: 'com.android.application' apply plugin: 'kotlin-android' // ੊झబ࣌

    ೒۞Ӓੋ ੸ਊ apply plugin: 'kotlin-android-extensions' ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 14
  10. Viewীࢲ ࢎਊೞӝ itemView?.let { with(it) { tv_title.text = item?.id }

    } ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 16
  11. ଵҊ੗ܐ • HTTP ాनਸ ਤೠ Retrofit ࢎਊೞӝ • RecyclerView ࢎਊೞӝ

    • Kunny - Kotlin Extensions-findViewById()ח ੉ઁ Ӓ݅! • kotlin extension for android • Retrofit API കಕ੉૑ • flickr.interestingness.getList ࢎਊ ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016 17