简体   繁体   English

复选框值为true,但未显示刻度线-Android / Java

[英]Checkbox value is true but tick is not showing - Android / Java

Basically here I have three(3) checkbox list in three(3) different fragment. 基本上,我在三(3)个不同的片段中有三(3)个复选框列表。 When I open the activity, my checkbox in the default fragment display like normal, but the one in different fragment display differently. 当我打开活动时,默认片段中的复选框显示为正常,但不同片段中的复选框显示为不同。 The value of the checkbox is still true. 复选框的值仍然为true。 Below is the image for the checkbox to make things clearer and also my code. 下面是复选框的图像,它使内容和我的代码更加清晰。

First fragment page: 第一个片段页面:

在此处输入图片说明

Second fragment page: 第二个片段页面:

在此处输入图片说明

Code for setting up the checkbox and its if condition 设置复选框及其if condition

private void setCheckBox(ArrayList<DataPrefType> arrayList){
    for(int i = 0; i < arrayList.size(); i++){
        id = i + 1;
        checkBox = new CheckBox(getActivity());
        checkBox.setId(i + 1);
        checkBox.setText(arrayList.get(i).getName());
        checkBox.setPadding(10, 10, 10, 10);
        checkBox.setLayoutParams(params);
        checkBox.setGravity(Gravity.CENTER);
        checkBoxLayout.addView(checkBox);

        if(!userPreference.isEmpty() || userPreference != null){
            for(int j = 0; j < userPreference.get(0).getDataPrefTypeArrayList().size(); j++){
                int retrieveId = Integer.parseInt(userPreference.get(0).getDataPrefTypeArrayList().get(j).getId());
                if(checkBox.getId() == retrieveId)
                    checkBox.setChecked(true);
            }
        }

The bug with the checkboxes not showing as checked even though isChecked is true is within the animation of the draw. 即使isChecked为true,带有复选框也未显示为已选中的bug处于绘制动画中。

I found this solution here . 我在这里找到了这个解决方案。 I am posting it here as an answer to make it easier to find and including some extra information about how to call it. 我将其发布在此处作为答案,以使其更易于查找,并包括有关如何调用它的一些其他信息。

Try calling jumpDrawablesToCurrentState on the root view (or parent view of your check boxes). 尝试在根视图(或复选框的父视图)上调用jumpDrawablesToCurrentState If specifically calling to checkBox.setChecked(true); 如果专门调用checkBox.setChecked(true); as OP is, call this after. 与OP一样,请在此后调用。 In this question, it would be important to call this after all checkboxes have been added to the parent view. 在这个问题中,在将所有复选框都添加到父视图之后调用此函数很重要。 This essentially will draw all drawables in the current state, skipping the animation process. 本质上,这将在当前状态下绘制所有可绘制对象,从而跳过动画过程。

Example: 例:

@Override
public void onStart() {
    View v = getView();
    if(v != null) {
        CheckBox chkYourCheckBox = (CheckBox)v.findViewById(R.id.chkYourCheckBox);
        chkYourCheckBox.setChecked(true); //sometimes this may draw fine, and other times it may not
        //***The important line:
        v.jumpDrawablesToCurrentState();
    }
}

You can override the Fragment's setUserVisibleHint() method and load in the checkboxes there. 您可以覆盖Fragment的setUserVisibleHint()方法并在其中的复选框中加载。

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser)setCheckBox();
}

This is just a workaround, and I don't think its the best way to solve it, but I haven't found anything better yet. 这只是一个解决方法,我认为这不是解决问题的最佳方法,但是我还没有发现更好的方法。 This can lead to Null pointer exceptions. 这可能导致Null指针异常。

I created this Kotlin extension property to deal with the problem (obviously only useful if you are writing Kotlin code, not Java) 我创建了此Kotlin扩展属性来解决该问题(显然,仅在编写Kotlin代码而不是Java时有用)

/**
 * Set the state of a checkbox skipping animations.
 */

var CheckBox.checked: Boolean
    get() = isChecked
    set(value) {
        if(isChecked != value) {
            isChecked = value
            jumpDrawablesToCurrentState()
        }
    }

Now in my code I write checkbox_view.checked = true instead of checkbox_view.isChecked = true 现在在我的代码中,我写的是checkbox_view.checked = true而不是checkbox_view.isChecked = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM