如何从操作栏更新布局的内容?

时间:2016-07-27 09:58:12

标签: java android android-fragments

我想在检查操作栏菜单中的选项后,从布局更新某个按钮的状态。

我有动作栏菜单的代码:

    @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();

1 个答案:

答案 0 :(得分:0)

你的问题有点不清楚 我想你想要从有ActionBar的Activity中改变BlinkFragment的视图。

以下代码会对您有所帮助吗?

BlinkFragment

调用YourActivity的方法
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
    }
}