我该如何刷新片段内容

时间:2016-10-10 13:04:05

标签: java android

创建了一个包含多个片段的应用

  • 每次点击与该片段相对应的按钮时,我都会更改片段。
  • 问题是我有一个功能,当我启用该功能时,我的应用程序必须转到指定的片段并启用所有选项,启用一些额外的选项,如专家模式的所有片段。
  • 问题是应用程序转到该片段但是只有在单击其他按钮并单击该片段的该按钮时才启用该选项。

我该如何解决这个问题?

更改片段的代码:

public void ChangeFragment(View view) {
    Fragment fragment;

    if (view == findViewById(R.id.button_sp)) {
        fragment = new SpoilerFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment2, fragment);
        ft.commit();
    }
    if (view == findViewById(R.id.button_bl)) {

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

    if (view == findViewById(R.id.button_br)) {
        fragment = new BremslichtFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment2, fragment);
        ft.commit();

    }

    if (view == findViewById(R.id.button_ru)) {
        fragment = new RucklichtFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment2, fragment);
        ft.commit();

    }
    if (view == findViewById(R.id.button_ruc)) {

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

    if (view == findViewById(R.id.button_sz)) {
        fragment = new SzenenFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment2, fragment);
        ft.commit();

    }
} 

以下是我在启用专家模式后刷新或如何转到该片段的方法:

   public void refresh() {
    Fragment fragment;
    fragment = new BlinkFragment();
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.fragment2, fragment);
    ft.commit();
}

专家模式功能仅将标志设置为true。 这是binkfragment的一部分:

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) {
   View v = inflater.inflate(R.layout.fragment_blink, container, false);

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

ledControl主类 https://docs.google.com/document/d/12gbxRStzLDmC2Q0kj7EJsx3fnHpeQ5sQNhWbTKTjxx8/edit?usp=sharing

0 个答案:

没有答案