2016-12-19 7 views
2

CheckBoxクリックアクションでレイアウトを変更しようとしています。1つのレイアウトからビューを削除して別のレイアウトに追加しようとしました

アクションをチェックする正常に動作しますが、チェックを外すと、エラーを与える:

java.lang.IllegalStateException: The specified child already has a 
parent. You must call removeView() on the child's parent first. 

ここに私のコードです:

@Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     presenter.onCukRulesClicked(); 

     final LinearLayout checkboxesContainer = (LinearLayout) findViewById(R.id.checkboxes_container); 

     final LinearLayout rootView = (LinearLayout) findViewById(R.id.linearLayout2345); 

     final LinearLayout leftContainer = (LinearLayout) findViewById(R.id.left_container); 

     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) 
      if (isChecked) { 
       leftContainer.removeView(checkboxesContainer); 
       leftContainer.invalidate(); 
       rootView.addView(checkboxesContainer); 
      } else { 
       rootView.removeView(checkboxesContainer); 
       rootView.invalidate(); 
       leftContainer.addView(checkboxesContainer); 
      } 
    } 

私が間違っているのか?

+0

プットレイアウトXMLSは、 –

答えて

1

Androidトランジションで行われたアクションの遅延がremoveView(view)の問題でした。これは、より新しいAndroidシステムバージョンのデバイスでのみ発生します。

解決策は非常に簡単です。 invalidate()ビューの代わりに、rootView.setLayoutTransition(null)を使用してトランジションを削除しました。

ここでは、コードです:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) 
      if (isChecked) { 
       leftContainer.removeView(checkboxesContainer); 
       rootView.addView(checkboxesContainer); 
      } else { 
       rootView.setLayoutTransition(null); 
       rootView.removeView(checkboxesContainer); 
       leftContainer.addView(checkboxesContainer); 
      } 
関連する問題