让你的代码减少三倍!使用kotlin开发Android(五) 监听器

在前面的博客中,有一个栗子,是点击按钮转跳的监听器。

让你的代码量减少3倍!使用kotlin开发Android(四) kotlin bean背后的秘密

可以看到,不用再new一个OnClickListener了。实际上这种写法是lambda的一种简略模式,通常情况下匿名内部类可以以如下表现形式展示:

简单来说就是->左边为参数,右面为结果。其过程相当于

从以上栗子可以看出,lambda可以极大简化我们的代码。当左面参数没有使用到的时候,可以省略掉整个左半部分”view->”,就如同第一个代码片一样。

给RecyclerView加监听

在MainActivity里添加一个RecyclerView。并且新建一个适配器起名MainAdapter

像往常一样写一个RecyclerView的布局

### 注意这个适配器里使用的一些小技巧

1.with改变作用域,如果你有一大堆需要重复写的前缀,则可以使用with改变作用域。如:

可以改为


2.在头部导入

这样就可以免去万恶的findviewbyid


3.View.infalte()代替LayoutInfalter.form().infalte()
这个没啥好说的,省点代码而已~~


4.String模板输出,类似于C语言的print

添加监听器

大家都知道,有个简便的方法就是直接给itemView添加个OnClickListener()~

所以在onBindViewHolder()开刀,注意写法,这里与java有不同

其中给了一个函数变量mListener, 他的输入为int,返回值为Unit(kotlin的null,不过是个具体的对象)

接下来给个set函数

这样就完成了item监听器的编写,所以我们的Activity代码如下:

本文这就结束了(嫌文字太短?不要急啦,先去动手实践下咯。。系列文章,既然挖了坑,就不会拖更的(吧..))

欢迎加入Kotlin小乐园群(这是一个高质量、优秀的圈子):479109923

如果你是Android开发者,那么你还可以来 wing的酒馆:425983695来分享你的开发经验哦

 

本项目使用的代码地址

24 thoughts on “让你的代码减少三倍!使用kotlin开发Android(五) 监听器

  1. var mListener: ((pos: Int) -> Unit)? = null

    fun setOnItemClickListener(listener: ((pos: Int) -> Unit)){
    mListener = listener
    } 请分析一下 ((pos: Int) -> Unit) 这是个什么类型

  2. adapter.setOnItemClickListener { pos->toast(“$pos”) }
    activity里面的这个代码 pos->toast(“$pos”) } 这个参数 pos怎么来的啊

  3. However, you can find significant risks involved wheen you’re dealing with private funding sources mainly because that they’ll dictate your business very quickly.
    And ssuch documentation will allow that yoou gauge your
    organization’s standing around all times and help out with
    reporting and paying of the appropriate levels of vadious taxes.
    We bargained from time to time; we gave in maybe; and
    we cheated sometimes.

  4. This relates to aany business if they are global, national or community based.
    Yes, right now your orgfanization carries a little of your personality within it,
    which is ok. The skill of joining a team and together to realize goals is quite
    important.

  5. Whatever one does, don’t spend high costs on name brand baby clothes unless funds
    are not an object for you. The shoe seller can assess the local
    terrain and may then discuss special activities that produce the
    highlighted footwear so very beneficial. Durting those Urgent Shopping Trips:So you have to go
    from sttation for some time, or you require some urgent
    clothes for some reason, and apso you don’t have the budget that you’d lime to get,
    it’s perfectly fine tto acquire clothes which are
    inexpensive and affordable.

  6. The biggest concern of people today is not only feelinng buut alo
    looking very healthy. If you need to generate any adjustments, you will possess the required time to ake any ecessary changes.
    Keywords inmclude the first thing to do in brosing the world wide web
    as this is wuat mot online website does to make their business noticeable in public.

  7. How can you prottect neglect the as well as your child’s if somewhere down the road, tthe adult
    children separate or divorce. This is very true if you are looking in an item tbat is similar to the
    one you originally chose. Despite its nearness
    to bigger, noisier, and much more packed Patong, Karon has a lengthy stretch of quiet beachfront, and presents a lovely vjewpoint in the sunset.

  8. The traditional stilettos was suddenly passed over to the hot new and much more trendy
    kitten heels. It would seem that a little bit of UK fashion will make your wardrobe interesting and
    add an international flare. Once you have reliable information of shoes you
    prefer, you’ll be able to find your perfect pair.

  9. If you might be pear-shaped (with larger hips and
    legs) avoid any clothing thjat would emphasize those
    areas. Another crucial consideration will be tthe capital because without one there is no wayy that one could start your business.
    This will help you to acquire what you cann only afford
    so that you are left with the running capital.

  10. CostHelper – Realise what cost homeowners аге paying. You’ll diuscover otһer beneficial info.
    CostHelper ρlaces tһᥱsᥱ labbour pices in perspective.
    Ꮢesearch а large numƅer oof matters oon CostHelper.

  11. I have to thank you for the efforts you’ve put in writing this website.
    I’m hoping to see the same high-grade blog posts by you in the future as well.
    In truth, your creative writing abilities has inspired me
    to get my very own website now 😉

发表评论

电子邮件地址不会被公开。