0

我在 2 个布局文件ViewSwitche中有一个 r,FragmentActivity每个方向一个。由ViewSwitcher控制radio group

当我旋转屏幕时,一切都如我所料,正确的布局文件用于渲染屏幕。

但是,当屏幕旋转时会触发广播组的onCheckedChanged事件。监听器看起来像这样(在 中创建):
onCreate

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        //snip
        radios.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int index) {

            viewSwitcher.showNext();
        }
        });
 }

如何在屏幕旋转时调用viewSwitcher.showNext()而不会触发它?listener

4

1 回答 1

0

好吧,我找到了一个 hacky 解决方案,但我希望那里有更好的解决方案。

    private boolean wasRotatedHack;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.savedInstanceState = savedInstanceState;
    if (savedInstanceState == null) {
        wasRotatedHack = false;
    } else {
        wasRotatedHack = true;
    }

    tabRadios.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int index) {

            if (!wasRotatedHack) {
                viewSwitcher.showNext();
            } else {
                wasRotatedHack = false;
            }
        }
    });

    startInspectionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getApplicationContext(), InspectionActivity.class));
        }
    });
  }
于 2012-06-21T07:20:05.523 回答