Android - ViewHolder如何捆绑宿主的生命周期
2023-10-09 本文已影响0人
晖仔Milo
ViewHolder实现接口 androidx.lifecycle.LifecycleObserver;
image.png宿主中添加观察
ChatBotVideoVH chatBotVideoVH = new ChatBotVideoVH(new ChatBotVideoVH.Listener() {
@Override
public void onViewBookClick(String bookText) {
viewModel.sendBotBookTextMsg(bookText);
}
});
getLifecycle().addObserver(chatBotVideoVH);//重点
return chatBotVideoVH;
在ViderHolder注册事件
image.png
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
//关联宿主onDestory
if (mSimpleVideoView != null) {
mSimpleVideoView.release();
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
//关联宿主onPause
if (mSimpleVideoView != null) {
mSimpleVideoView.stop();
}
}