如何在onCreate中打开一个随机片段进行活动?

最后发布: 2014-02-14 19:23:50


问题

当我启动MainAcitivity时,我想在framelayout中打开一个随机片段。

问题:

  1. 如何将random1值转换为片段?
  2. 当按下屏幕上的按钮时,难以“刷新”视图以获得另一个randow片段吗?

     public class Beginner extends ActionBarActivity { private SoundPool spool; public int soundID_R1; public int soundID_R5; public int soundID_R8; private Fragment_gehorINTbeginnerR1 intBegFragment_R1; private Fragment_gehorINTbeginnerR5 intBegFragment_R5; private Fragment_gehorINTbeginnerR8 intBegFragment_R8; private int randomFragmentNum; private FrameLayout container; private FragmentTransaction fragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_beginner); initUI(); } private void initUI() { container = (FrameLayout) findViewById(R.id.beginner_frame); fragmentTransaction = getSupportFragmentManager().beginTransaction(); setRandomFragment(); } private void setRandomFragment() { Random random = new Random(); randomFragmentNum = random.nextInt(2); //random choose fragment switch (randomFragmentNum) { case 0: { intBegFragment_R1 = new Fragment_gehorINTbeginnerR1(); fragmentTransaction.replace(R.id.beginner_frame, intBegFragment_R1); fragmentTransaction.commit(); break; } case 1: { intBegFragment_R5 = new Fragment_gehorINTbeginnerR5(); fragmentTransaction.replace(R.id.beginner_frame, intBegFragment_R5); fragmentTransaction.commit(); break; } case 2: { intBegFragment_R8 = new Fragment_gehorINTbeginnerR8(); fragmentTransaction.replace(R.id.beginner_frame, intBegFragment_R8); fragmentTransaction.commit(); break; } } } 
android random android-fragments android-activity
回答

简单的解决方案(更新2)。 如果使用2个随机片段,则必须设置random.getInt()值100,如果三个片段 - 150,四个 - 200 ......并添加块if-else为逻辑选择片段:

 /**
     * Created by scijoker on 2/14/14.
     */
    public class TestRandomFragmentActivity extends FragmentActivity {
        private LoginFragment loginFragment;
        private SignUpFragment signUpFragment;
        private FragmentTransaction fragmentTransaction;

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

            initUI();
        }

        private void initUI() {
            fragmentTransaction = getSupportFragmentManager().openTransaction();
            setRandomFragment();
        }

        private void setRandomFragment() {
            Random random = new Random();
           int randomFragmentNum = random.nextInt(100);
        if (randomFragmentNum < 50) {
            randomFragmentNum = 0;
        } else{
            randomFragmentNum = 1;
        }
            //random choose fragment
            switch (randomFragmentNum) {
                case 0: {
                    loginFragment = new LoginFragment();
                    fragmentTransaction.add(R.id.fl_container, loginFragment);
                    fragmentTransaction.commit()
                    break;
                }
                case 1: {
                    signUpFragment = new SignUpFragment();
                    fragmentTransaction.add(R.id.fl_container, signUpFragment);
                    fragmentTransaction.commit()
                    break;
                }
            }
        }


回答

问题是随机的:随机选择三个片段之间的非常小的参数。 这段代码可以帮到你。 请插入setRandomFragment():

randomFragmentNum = random.nextInt(150);
        if (randomFragmentNum < 50) {
            randomFragmentNum = 0;
        } else if (randomFragmentNum < 100) {
            randomFragmentNum = 1;
        } else randomFragmentNum = 2;

更新2:五个片段

randomFragmentNum = random.nextInt(250);
            if (randomFragmentNum < 50) {
                randomFragmentNum = 0;
            } else if (randomFragmentNum < 100) {
                randomFragmentNum = 1;
            } else if (randomFragmentNum < 150) {
                randomFragmentNum = 2;
            } else if (randomFragmentNum < 200) {
                randomFragmentNum = 3;
            } else 
                randomFragmentNum = 4;
            } 


回答

这是一个例子:

   switch(random1.nextInt(3)){
        case 0:  setMyFragment(new Fragment_gehorINTbeginnerR1());
                 break;
        case 1: setMyFragment(new Fragment_gehorINTbeginnerR5());
                break;
        case 2: setMyFragment(new Fragment_gehorINTbeginnerR8());
                break;
    }

我使用一种方法来加载我的片段:

 public void setMyFragment(Fragment fr){
    try{
        FragmentManager fm = getSupportFragmentManager();
        if(fr != null){
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.Mainfragment, fr, null);
                            ft.commitAllowingStateLoss();
        }
    }catch(IllegalStateException e) {           
        Log.e(TAG, "ISE setting main view "+e.getMessage());
    }       
}