As an Android developer, Kotlin, which Google has been pushing so hard, must be able to use. Recently, I used part of douban film interface for a small Demo to learn. (Because the computer is relatively bad, opened the simulator recording good card. It’s actually pretty smooth on the phone. Ha, ha, ha)

Introduction to the

Md-style, MVP architecture, using Kotlin+Retrofit+RxKotlin development. Now there are two main functions:

  • Douban movie release list and details (due to API restrictions, some functions are not implemented)
  • Use the direction sensor and matrix to move the picture position with the mobile phone swing (need a real phone)

About the first feature

Kotlin+Retrofit+RxKotlin
  • Define request methods in ApiServers
@GET(BaseURL.HOTSCREEN)
    fun getHotScreenList(@QueryMap par: HashMap<String, String>): Observable<HotScreenResult>
Copy the code
  • Encapsulate in the Module layer
override fun loadHotScreenData(page: Int, next: (result: HotScreenResult) -> Unit
                                   , error: (th: Throwable) -> Unit) {
        val parm = HashMap<String, String>()
        parm.put("apikey", getApiKey())
        parm.put("city"."Shenzhen")
        parm.put("start", page.toString())
        parm.put("count"."10")
        parm.put("client"."")
        parm.put("udid", getToken())
        BuildApi.buildApiServers()!!
                .getHotScreenList(parm)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeBy(
                        onNext = next,
                        onError = error)
    }
Copy the code

Presenter layer calls

 if (view == null || module == null)
            returnview? .showLoading() module? .loadHotScreenData(page, { view!! .notifyList(it) view!! .dismissLoading() }, { it.printStackTrace() view!! .dismissLoading() view!! .showTipMessage(1,"Network exception")})
Copy the code
Shared element transition animation
  • Define the same in the XML file to be transitioned separatelytransitionName
<! --> <ImageView Android :id="@+id/item_hot_screen_image"
            android:layout_width="@dimen/dimen_100"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:transitionName="@string/t_movie_list_to_detail"/ > <! --> <ImageView Android :id="@+id/movie_detail_coll_head_photo"
                android:layout_width="@dimen/dimen_150"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="@dimen/dimen_15"
                android:layout_marginTop="@dimen/dimen_55"
                android:scaleType="centerCrop"
                android:transitionName="@string/t_movie_list_to_detail"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.6" />

Copy the code
  • Defined in code
/ / the first page val intent = intent (activity, MovieDetailAct: : class. Java) intent. PutExtra ("id", mData[position].id) // View third argument to shared elements: transitionName val optionsCompat = defined in the XML file ActivityOptionsCompat.makeSceneTransitionAnimation(activity, shareView, getString(R.string.t_movie_list_to_detail)) startActivity(intent, Optionscompat.tobundle ()) // Second page postponeEnterTransition() // Delay animation... . StartPostponedEnterTransition () / / animationCopy the code
Picture the color

The tool Palette in V7 package is used. It should be noted that due to the complexity of pictures, not every picture can be selected with the corresponding color, so it is necessary to make a non-empty judgment.

                Palette.from(resource).generate {
                    if(it ! = NULL) {val DVS = it.darkVibrantSwatch // lightVibrantSwatch val DMS = it.darkMutedSwatch Var color = when {DVSS? Var color = when {DVSS? .rgb ! = null -> dvs.rgb lvs? .rgb ! = null -> lvs.rgb dms? .rgb ! = null -> dms.rgb lms? .rgb ! = null -> lms.rgbelse-> ContextCompat.getColor(getContext(), R.color.themcolor)} window.statusbarcolor = color // Status bar movie_detail_COLL_head_bg.setbackgroundColor (color) movie_detail_coll_layout.setContentScrimColor(color) } }Copy the code
Define higher-order functions
fun setOnItemClickListener(listener: (itemView: View, position: Int, shareView: View) -> Unit) { itemClick = listener } .... . holder? .btn? .setOnClickListener {if(itemBtnClick ! = null) itemBtnClick.invoke(it, position) }Copy the code

About the second little feature

The effect of the second function is to see the advertising effect of toutiao, feel very interesting, so I want to implement it myself. They do down the code is not a lot, but it is worth understanding the principle involved. I am going to write another article to record it. I have a feeling that mathematics is really a good thing.

The last

In general, I think Kotlin is still very easy to use and worth further study. Code amount is very small, suitable for practice, code above to GitHub.