片段替换不起作用,直到第二次执行

时间:2014-05-29 07:35:42

标签: java android fragment

嘿,大家,多么美好的一天! 我正在制作一个在屏幕底部有一个RadioGroup(Customed)的应用程序。当第一次进入应用程序时,它会显示如下的欢迎片段:(对不起,我无法上传图片T ^ T,虽然我知道我可以发布图片链接⊙﹏⊙)

------------------------------------ | | | | | | | | | | | | | | | welcome | | | | | | | | | | | | | | | | | | | |---------------------------------- | | | |⊙tab0 ⊙tab1 ⊙tab2 | -------------------------------------

并且未检查RadioButton。

当我触摸tab0时,它将变为另一个片段,如下所示:

------------------------------------ |<back | | | | | | | | | | | | | | tab0 | | content | | | | | | | | | | | | | | | | | |---------------------------------- | | | |*⊙tab0* ⊙tab1 ⊙tab2 | -------------------------------------

它似乎工作正常。但是当按下后退按钮时,它不起作用,当我再次击中时,它起作用。它很奇怪。 这是我的WelcomeFragment代码:

public class WelcomeFragment extends Fragment {

private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.welcome_fragment_layout, null);
    MainActivity.radioGroup.setBackgroundResource(R.drawable.tabbar_bg);
    RadioButton button;
    // Reset the RadioGroup state to not checked。
    for(int i = 0;i<3;i++){
        button = (RadioButton)MainActivity.radioGroup.getChildAt(i);
        //button.setChecked(false);//if I comment this,it work finely!!,but I don't know why                
    }
    return view;
}

它的radiobuttons&#39;活动中的事件:

    private void setupRadioGroup() {
    resetRadioButtonID();
    radioButton = (RadioButton) radioGroup.getChildAt(0);
    radioButton.setChecked(false);
    radioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if (checkedId != 1) {
                        radioButton.setChecked(false);
                    }
                    FragmentTransaction transaction = fragmentManager
                            .beginTransaction();
                    Fragment fragment = FragmentFactory
                            .getInstanceByIndex(checkedId);
                    transaction.replace(R.id.fl_content, fragment);
                    transaction.commitAllowingStateLoss();


                    switch (checkedId) {
                    case 1:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar1);
                        break;
                    case 2:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar2);
                        break;
                    case 3:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar3);
                        break;
                    default:
                        break;
                    }
                }
            });
}

我非常感激,顺便说一句,不要介意我的英语不好。^ - ^

2 个答案:

答案 0 :(得分:0)

你需要将onCheckedChanged中的所有内容都包含在if条件中。 首先检查单选按钮的当前状态。

radioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(!radioButton.checked) return;  // add this
                    if (checkedId != 1) {
                        radioButton.setChecked(false);
                    }
                    FragmentTransaction transaction = fragmentManager
                            .beginTransaction();
                    Fragment fragment = FragmentFactory
                            .getInstanceByIndex(checkedId);
                    transaction.replace(R.id.fl_content, fragment);
                    transaction.commitAllowingStateLoss();


                    switch (checkedId) {
                    case 1:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar1);
                        break;
                    case 2:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar2);
                        break;
                    case 3:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar3);
                        break;
                    default:
                        break;
                    }
                }
            });

答案 1 :(得分:0)

感谢Premsuraj。 没有任何关于Fragment.It由RadioGroup OnCheckedChange事件引起的。 让我们来看看RadioGroup的来源:

/**
 * <p>Changes the checked state of this button.</p>
 *
 * @param checked true to check the button, false to uncheck it
 */
public void setChecked(boolean checked) {
    if (mChecked != checked) {
        mChecked = checked;
        refreshDrawableState();
        notifyViewAccessibilityStateChangedIfNeeded(
                AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);

        // Avoid infinite recursions if setChecked() is called from a listener
        if (mBroadcasting) {
            return;
        }

        mBroadcasting = true;
        if (mOnCheckedChangeListener != null) { 
            mOnCheckedChangeListener.onCheckedChanged(this, mChecked);//This is the root cause
        }
        if (mOnCheckedChangeWidgetListener != null) {
            mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
        }

        mBroadcasting = false;            
    }
}

因为我在Fragment中调用了这个方法而不是在监听器中,所以,当我调用setChecked(boolean checked)时,onCheckedChanged()也被调用。它是问题的根源。