android 点击后颜色无变化原因分析
在android开发中,使用viewmodel来管理ui数据时,经常会遇到点击后颜色无变化的情况,这可能是由于以下原因造成的:
<textview ... android:textcolor='@{vm.getsorttextcolor(vm.sort_column_auto)}' ... />
生命周期所有者未设置
引用viewmodel时,需要设置它的生命周期所有者,以确保在生命周期改变时更新绑定数据。在上述代码中,生命周期所有者未设置,导致视图无法收到viewmodel数据的更新。
解决方案
在fragment的oncreateview()方法中,设置生命周期所有者:
@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false); // 设置生命周期所有者 this.binding.setLifecycleOwner(this); return this.binding.getRoot(); }
以上就是Android点击无颜色变化?可能是ViewModel生命周期惹的祸的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号