惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
V
Visual Studio Blog
H
Help Net Security
Engineering at Meta
Engineering at Meta
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
博客园 - 【当耐特】
B
Blog
Stack Overflow Blog
Stack Overflow Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园 - 司徒正美
博客园 - 叶小钗
Y
Y Combinator Blog
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - y丶innocence

临时111 RecyclerView 数据多时无法滑动:ConstraintLayout 约束高度修复笔记 Android Kotlin OkHttp3 WebSocket 长连接与 Gson 数据解析系统笔记 Android + Kotlin + OkHttp WebSocket 相关概念与使用流程笔记(TLS/证书 + 鉴权/会话) AI对话导出markdown格式流程 代理转发 分享文件 面向全球的app的excel导出和kotlin IO原理 安卓导出笔记(未整理) 0.7 动画 0.4 View 工作流程 0.3 view 滑动冲突 13. Jetpack 0. 安卓开发艺术探索参考资料 12. Material Design 7. 持久化技术 5. Fragment java 基础 4. UI 开发 3. Activity 2.2 Kotlin 面向对象 2.3 Kotlin高级 2.1 Kotlin基础 1. Android简介 [OpenJudge] 反正切函数的应用 (枚举)(数学) [OpenJudge] 摘花生 (模拟)
java&kotlin listener
y丶innocence · 2026-03-18 · via 博客园 - y丶innocence
private MyListener myListener;
public void setMyListener(MyListener myListener) {
    this.myListener = myListener;
}
public interface MyListener {
    void myFun(long timeInMillis);
}
fragment.setMyListener{
            mModel.myAnotherFun(it)
        }
        


private var myListener: MyListener? = null
fun setMyListener(myListener: MyListener) {
    this.myListener = myListener
}
fun interface MyListener {
    fun myFun(timeInMillis: Long)
}
fragment.setMyListener{
    mModel.myAnotherFun(it)
}



private var myListener: MyListener? = null
fun setMyListener(myListener: MyListener) {
    this.myListener = myListener
}
interface MyListener {
    fun myFun(timeInMillis: Long)
}
fragment.setMyListener( object: MyListener {
    override fun myFun(timeInMillis: Long) {       // 必须实现接口
        mModel.myAnotherFun(timeInMillis) 
    }
}
                       
                       
// 使用函数类型替代接口
private var myListener: ((Long) -> Unit)? = null
// 使用函数类型参数
fun setMyListener(listener: (Long) -> Unit) {
    this.myListener = listener
}
// 使用(可以直接使用Lambda):
fragment.setMyListener { time ->
    mModel.myAnotherFun(time)
}
// 或使用it简写:
fragment.setMyListener {
    mModel.myAnotherFun(it)
}