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

推荐订阅源

月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
T
Tailwind CSS Blog
博客园_首页
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
J
Java Code Geeks
量子位
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
Check Point Blog
V
Visual Studio Blog
H
Help Net Security
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta

博客园 - 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)
}