2016-07-27 5 views
0

アクションバーメニューのオプションをチェックした後、レイアウトから一部のボタンのステータスを更新したいとします。レイアウトのコンテンツをアクションバーから更新するにはどうすればよいですか?

私はアクションバーメニューのため、このコードを持っている:ボタンを制御

@Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 

      int delay = 150; // delay 150 ms. 

    switch (item.getItemId()){ 
     case R.id.action_disconnect: 
      data.saveData(this.getApplicationContext()); 

       Disconnect(); 

      return true; 
     case R.id.action_check: 

      if (item.isChecked()) 
      { 
      // here i want to update button status 
       item.setChecked(false); 
       ledControl.instance.data.expertMode = false; 
       ledControl.instance.data.configExpertMode = 1; 
       ledControl.instance.data.sendConfigTurnLightMessage(); 

      } 
      else { 
       item.setChecked(true); 
       ledControl.instance.data.expertMode = true; 
// here i want to update button status 
      } 
       return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
      } 

そして、ここでは、レイアウトクラスはフラグメント

public class BlinkFragment extends Fragment implements View.OnClickListener { 

     private Switch switch_b1, switch_b2; 
     private DiscreteSeekBar discrete_bl_g, discrete_bl_h; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      // Inflate the layout for this fragment 
      View v = inflater.inflate(R.layout.fragment_blink, container, false); 
        switch_b1 = (Switch) v.findViewById(R.id.switch_b1); 
      switch_b2 = (Switch) v.findViewById(R.id.switch_b2); 
      discrete_bl_g = (DiscreteSeekBar) v.findViewById(R.id.discrete_bl_g); 
      discrete_bl_h = (DiscreteSeekBar) v.findViewById(R.id.discrete_bl_h); 
    discrete_bl_g.setEnabled(ledControl.instance.data.expertMode); 
      discrete_bl_h.setEnabled(ledControl.instance.data.expertMode); 

    } 
} 

としてロードされて、私はこのような方法を試してみます:

 fragment = new BlinkFragment(); 
     FragmentManager fm = getSupportFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 
     ft.replace(R.id.fragment2, fragment); 
     ft.commit(); 

答えて

0

あなたの質問は少し不明です。
BlinkFragmentのビューをActionBarのあるアクティビティから変更したいと思います。

次のコードはお役に立ちますか?

YourActivityから

コールBlinkFragmentの方法:

public class YourActivity extends AppCompatActivity { 

    ... 

    private BlinkFragment getBlinkFragment() { 
     return (BlinkFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 

     int delay = 150; // delay 150 ms. 

     switch (item.getItemId()){ 
      case R.id.action_disconnect: 
       data.saveData(this.getApplicationContext()); 
       Disconnect(); 
       getBlinkFragment.onDisconnected(); 

       return true; 
      case R.id.action_check: 

       if (item.isChecked()) 
       { 
        // here i want to update button status 
        item.setChecked(false); 
        ledControl.instance.data.expertMode = false; 
        ledControl.instance.data.configExpertMode = 1; 
        ledControl.instance.data.sendConfigTurnLightMessage(); 
       } 
       else { 
        item.setChecked(true); 
        ledControl.instance.data.expertMode = true; 
        // here i want to update button status 
       } 
       getBlinkFragment.onLedControlChanged(ledControl); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
    .... 
} 

BlinkFragmentは以下のようになります。

public class BlinkFragment extends Fragment implements View.OnClickListener { 

    ... 

    public void onLedControlChanged(LedControl ledControl) { 
     // change the view 
     discrete_bl_g.setEnabled(ledControl.instance.data.expertMode); 
     discrete_bl_h.setEnabled(ledControl.instance.data.expertMode); 
    } 

    public void onDisconnected() { 
     // change the view 
    } 
} 
+0

私がやりたいことはある: –

+0

私は、フラグメントレイアウト上とでいくつかのボタンがありますアクションバーiのチェックボックスは無効にしてボタンを有効にしますが、レイアウトを再ロードするまで問題はレイアウトには反映されません。 –

+0

[OK]を、あなたの問題を理解したいです。私のコードはあなたのプロジェクトでは機能しませんでしたか? – nshmura

関連する問題