带有数据绑定的ViewPager中未正确检查Android复选框 [英] Android checkboxes not checked correctly in ViewPager with data binding

查看:96
本文介绍了带有数据绑定的ViewPager中未正确检查Android复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序中有一个使用ViewPager分布在多个页面上的表单.

I have a form in my Android app that is spread across multiple pages with a ViewPager.

为简单起见,它仅返回视图对象,而不是实例化片段.

To keep it simple, it just returns View-objects instead of instantiating Fragments.

每个视图都由一个布局和一个视图模型(我用于所有视图的同一实例)和数据绑定组成,以自动设置值.

Each view is made of a layout and a viewmodel (The same instance I use for all the views) and data binding to set the values automatically.

问题是,如果在加载活动时复选框未处于正面,则复选框未填充且带有白色选中标记.

The problem is that the checkboxes is not filled and have a white checkmark if they are in a view that is not in the front when loading the activity.

如果更改顺序,则带有复选框的视图是ViewPager加载的第一个视图,我可以看到选中的复选框(带有动画)并正确标记了它们.

If I change the order, so my view with the checkboxes are the first one loaded by the ViewPager, I can see the checkboxes being checked (with an animation) and they are marked correctly.

请参阅所附的屏幕截图:

See the attached screenshots:

视图不是首次加载

视图是第一个加载的

单选按钮也有同样的问题.

I have the same problem with radio buttons.

复选框数据绑定的示例:

An example of a Checkbox's databinding:

<CheckBox
            android:id="@+id/checkbox_calling"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_weight="1"
            android:checked="@={viewModel.observation.calling}"
            android:text="@string/label_calling" />

更新:

我目前已通过在 ViewPager 上设置 OnPageChangeListener 来解决该问题,并且当 ViewPager 滚动到带有复选框的页面时,我取消选中复选框,然后将其设置为原始值.

I have currently solved the problem by setting a OnPageChangeListener on the ViewPager and when the ViewPager scrolls to the page with the checkboxes, I uncheck the checkboxes and then set them to the original value.

然后正确显示动画,并且该复选框以正确的复选标记结束.

The animation is then shown correctly and the checkboxes ends up with the correct checkmarks.

但是,我仍然希望找出我做错了什么,或者Android数据绑定本身是否有问题.

I would however still appreciate finding out what I've done wrong or if there is a problem with Android databinding itself.

推荐答案

我认为这与复选框动画的方式有关.因此,我查看了如何跳过动画,并在检查完成后对复选框 调用了 jumpDrawablesToCurrentState .

I think that it has to do with the way checkbox are animated. So I looked up how to skip the animation, and calling jumpDrawablesToCurrentState on the checkbox after checking it does the trick for me.

这篇关于带有数据绑定的ViewPager中未正确检查Android复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆