美文网首页
Android - ViewHolder如何捆绑宿主的生命周期

Android - ViewHolder如何捆绑宿主的生命周期

作者: 晖仔Milo | 来源:发表于2023-10-09 10:26 被阅读0次

    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();
            }
        }
    

    相关文章

      网友评论

          本文标题:Android - ViewHolder如何捆绑宿主的生命周期

          本文链接:https://www.haomeiwen.com/subject/sidibdtx.html