Android牛轧糖:为什么以编程方式选中Fragment上的复选框时状态不完整(但在Lollipop上看起来不错) [英] Android Nougat: Why do checkboxes on Fragment have incomplete state when selected programmatically (but look fine on Lollipop)

查看:109
本文介绍了Android牛轧糖:为什么以编程方式选中Fragment上的复选框时状态不完整(但在Lollipop上看起来不错)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行任何选择之前,这是我的设置标签(片段)的样子:

用户可以在主Fragment中从Spinner中进行选择-如下所示(在用户做出选择之后):

当用户进行选择时,将加载用户先前选择的,保存在用户首选项中的选项,并选择相应的复选框.现在,第一个快照中显示的复选框如下所示:

看,现在具有关联复选框的项目设置为粉红色?这些项目变成粉红色,但由于某种原因在牛轧糖上未显示支票. 实际上已选中了这些项目,因为如果单击一次,则它们将变为未选中状态.

棒棒糖按预期工作 但是,如果我在Lollipop上运行该程序,则项目将完全按预期显示(选中了复选框).

在牛轧糖上手动设置它们 另外,如果我在Nougat上手动设置它们(在应用程序运行时单击它们),则当应用程序继续运行时,它对于微调器中选择的所有其他项目都可以正常工作.

这是设置这些东西的基础:

在微调框中选择该项目后,我将获得rootview并调用findViewById()

  addCharsTabCheckBox = (CheckBox)rootView.findViewById(R.id.addCharsTabCheckBox);
     maxLengthTabCheckBox = (CheckBox)rootView.findViewById(R.id.maxLengthTabCheckBox);

之后,我只需在每个CheckBox上调用setChecked():

addCharsTabCheckBox.setChecked(currentSiteKey.isHasSpecialChars());

您是否会知道为什么会发生这种情况?

您以前见过这种部分行为吗?

编辑#1 在运行时,我还发现未选中的项目将显示为部分选中状态(复选框为灰色),如下所示:

实际上没有选择那些灰色项目不是. 另外,如果我在查看设置"片段时更改了设备方向,则复选框将被重绘(很明显),并按预期显示未选中.

编辑#2

只需在棉花糖上进行测试,它就可以在该API级别上正常运行(或按预期运行).


编辑3

我已经基于Android Studio 2.3.3提供的TabPage模板创建了一个简化的项目,以便更深入地研究此问题.

公共GitHub项目

我已将代码添加到公共GitHub存储库中,因此您可以尝试以下代码: https://github.com/raddevus/CheckBoxFragTest

该项目与上述项目略有不同,但它们均基于Android Studio 2.3.3提供的模板. 该测试项目包含3个选项卡,因此我可以调查在所有选项卡页面上是否会看到相同的异常行为. SPOILER :我没有.

在第一个标签页中,应用程序的外观如下:

两个最有趣的部分

  1. 单击选项卡页1上的[切换复选框]按钮时,它将在两个复选框(在tabPage2上为1个复选框,在TabPage3上为1个复选框)上调用setChecked.

  2. 当您单击TabPage3上的按钮时,它将设置tabPage1上显示的复选框(在图像中看到).

问题

在牛轧糖上,当您单击TabPage1上的[Toggle Checkbox]按钮时,它会将tabPage2上的复选框设置为奇数状态,这在原始问题报告中已经看到. 但是,tabPage3上的复选框已设置,并按预期完全选中.

编辑4

Oreo Android v8.0 API级别26

我有更多数据.我只是在Oreo(仿真器)上运行了该应用程序,并且在那里看到了相同的问题. 这是我运行的版本:

这是我第一次单击[切换复选框]按钮时的样子.

但是,请注意,即使tabpage3上的复选框是从相同的静态布尔值中设置的,它实际上也已正确选中:

如果第二次单击[切换复选框]按钮,则会显示灰色复选框状态(实际上未选中.

但是,实际上未选中tabpage3上的复选框.

现在,如果再次单击[Toggle Checkbox],则它将正确选择tabPage2上的复选框,并且在此之后运行该应用程序即可正常工作.

解决方案

只需在调用radioButton.setChecked(true)checkBox.setChecked(true);

后调用此jumpDrawablesToCurrentState()

适用于我的案例的代码


CheckBox checkBox   = (CheckBox) clView;   
checkBox.setChecked(true);
checkBox.jumpDrawablesToCurrentState();

RadioButton radioButton = (RadioButton)  clView; 
radioButton.setChecked(true);
radioButton.jumpDrawablesToCurrentState();

Here is what my settings tab (Fragment) looks like before any selections are made:

From the main Fragment the user can make a selection from the Spinner -- looks like the following (after the user has made a choice):

When the user makes that selection, choices the user has previously chosen which are saved in the User Prefs are loaded and the appropriate checkboxes are selected. Checkboxes which were shown in first snapshot now look like the following:

See, the items which now have associated checkboxes set to pink? Those items turn pink but the check is not shown for some reason on Nougat. Those items are actually selected, because if I click them once then they then become unselected.

Lollipop Works As Expected However, if I run the program on Lollipop, the items show up completely selected (with checkboxes checked) as expected.

Manually Setting Them On Nougat Works Also, if I manually set them (click them while the app is running) on Nougat then it works as expected for all other items chosen in the spinner while the app continues to run.

Here's the basics of what is going on for those to get set:

When the item is selected in the Spinner, I get the rootview and call findViewById()

  addCharsTabCheckBox = (CheckBox)rootView.findViewById(R.id.addCharsTabCheckBox);
     maxLengthTabCheckBox = (CheckBox)rootView.findViewById(R.id.maxLengthTabCheckBox);

After that I just call the setChecked() on each CheckBox:

addCharsTabCheckBox.setChecked(currentSiteKey.isHasSpecialChars());

Would you happen to know why this might be happening?

Have you seen this partial behavior before?

Edit #1 While running I also found that unselected items will show up as if they are partially selected (checkboxes but gray) and look like the following:

Those gray items are not actually selected. Also, if I change the device orientation while viewing that Settings fragment then the checkboxes get redrawn (obviously) and show up unchecked as expected.

Edit #2

Just tested on Marshmallow and it does work properly (or as expected) on that API level also.


EDIT 3

I have created a simplified project based upon the TabPage template provided by Android Studio 2.3.3 in order to study this problem more closely.

Public GitHub Project

I've added the code to a public GitHub repo so you can get it and try it: https://github.com/raddevus/CheckBoxFragTest

The project is a bit different than the one above but they are both based upon that template provided by Android Studio 2.3.3. The test project includes 3 tabs so I could investigate whether or not I'd see the same odd behavior on all tab pages. SPOILER : I did not.

Here's what the app looks like from he first tab page:

The Two Most Interesting Parts

  1. When you click the [Toggle Checkbox] button on Tab Page 1, it will call setChecked on two checkboxes (1 checkbox on tabPage2 and and 1 checkbox on tabPage3)

  2. When you click the button on TabPage3, it will set the checkbox shown on tabPage1 (seen above in the image).

The Problem

On Nougat, when you click the [Toggle Checkbox] button on TabPage1, it sets the checkbox on tabPage2 to the odd state, we saw in the original problem report. However, the checkbox on tabPage3 is set and fully checked as you expect.

EDIT 4

Oreo Android v8.0 API Level 26

I have more data. I just ran the app on Oreo (emulator) and I see the same issue there. Here's the version I ran on:

Here's what it looks like the first time I click the [Toggle Checkbox] button.

But, notice that the checkbox on tabpage3 is actually selected properly even though it is set from the same static boolean:

If you click the [Toggle Checkbox] button a second time then you get the odd grey checkbox state (which is actually unselected.

However, the checkbox on tabpage3 is actually unselected as it should be.

And now, if you click the [Toggle Checkbox] again, then it will select the checkbox on tabPage2 properly and will work properly as long as the app runs after this.

解决方案

just Call this jumpDrawablesToCurrentState() after calling radioButton.setChecked(true) or checkBox.setChecked(true);

Code which worked for me in my Case


CheckBox checkBox   = (CheckBox) clView;   
checkBox.setChecked(true);
checkBox.jumpDrawablesToCurrentState();

RadioButton radioButton = (RadioButton)  clView; 
radioButton.setChecked(true);
radioButton.jumpDrawablesToCurrentState();

这篇关于Android牛轧糖:为什么以编程方式选中Fragment上的复选框时状态不完整(但在Lollipop上看起来不错)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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