如何使用switch小部件在框架中设置初始片段

时间:2018-04-20 08:54:46

标签: android switch-statement fragment

我正在使用switch更改android中的片段,并且它的成功,但我没有找到如何在帧中设置和显示初始片段。

public class SwitchActivity extends AppCompatActivity {

Switch swCheck;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_switch);

    swCheck = findViewById(R.id.switch_check);

    swCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();

            if (isChecked) {
                transaction.replace(R.id.content, new NewsFragment()).commit();
            } else {
                transaction.replace(R.id.content, new BoardFragment()).commit();
            }
        }
    });



}

}

1 个答案:

答案 0 :(得分:0)

要替换片段我使用这样的辅助方法:(我保留backstack但每个片段类只有1个)

@Override
public void replaceCurrentFragment(Fragment fragment, String newTag) {

    invalidateOptionsMenu();
    String backStateName = fragment.getClass().getName();

    FragmentManager manager = getSupportFragmentManager();
    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);

    if (!fragmentPopped && manager.findFragmentByTag(newTag) == null) {
        //fragment not in back stack, create it.
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.flContainer, fragment, newTag);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(backStateName);
        ft.commit();
    }
}

我称之为

    replaceCurrentFragment(new MenuFragment(), "yourTag");

您可以在交换机和初始设备中使用此方法,您的代码将如下所示:

public class SwitchActivity extends AppCompatActivity {

Switch swCheck;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_switch);

    swCheck = findViewById(R.id.switch_check);

    swCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();

            if (isChecked) {
                replaceCurrentFragment(new NewsFragment(), "yourTag");
            } else {
                replaceCurrentFragment(new BoardFragment(), "yourTag2");
            }
        }
    });

    //to set the initial fragment, just use the replace again here in the oncreate:
    replaceCurrentFragment(new NewsFragment(), "yourTag");
  }

@Override
public void replaceCurrentFragment(Fragment fragment, String newTag) {

    invalidateOptionsMenu();
    String backStateName = fragment.getClass().getName();

    FragmentManager manager = getSupportFragmentManager();
    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);

    if (!fragmentPopped && manager.findFragmentByTag(newTag) == null) {
        //fragment not in back stack, create it.
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.content, fragment, newTag);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(backStateName);
        ft.commit();
    }
}
}

如果您不想使用该方法

public class SwitchActivity extends AppCompatActivity {

Switch swCheck;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_switch);

    swCheck = findViewById(R.id.switch_check);

    swCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();

            if (isChecked) {
                transaction.replace(R.id.content, new NewsFragment()).commit();
            } else {
                transaction.replace(R.id.content, new BoardFragment()).commit();
            }
        }
    });
  transaction.replace(R.id.content, new BoardFragment()).commit();

}
}

希望这有帮助