如何随机显示片段?

时间:2015-04-17 12:02:07

标签: java android android-fragments

我正在使用Android Studio创建一个测验应用。我有一个活动和PagerAdapter。 PagerAdapter中有三个片段。当我启动应用程序时,片段按顺序出现:fragment1,fragment2,fragment3。

我希望每次启动应用程序时它们都会随机出现在不同的位置。例如:fragment3,fragment1,fragment2 ......

其中包含PagerAdapter的活动:

public class testActivity extends FragmentActivity {

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

    private  void initialisePaging() {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this,fragment1.class.getName()));
        fragments.add(Fragment.instantiate(this,fragment2.class.getName()));
        fragments.add(Fragment.instantiate(this,fragment3.class.getName()));
        PagerAdapter mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);

        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(mPagerAdapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

PagerAdapter.java

public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int arg0) {
        return this.fragments.get(arg0);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }

    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
    }
}

3 个答案:

答案 0 :(得分:3)

在将片段传递给适配器之前,只需将片段列表随机播放。

private  void initialisePaging() {
    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this,fragment1.class.getName()));
    fragments.add(Fragment.instantiate(this,fragment2.class.getName()));
    fragments.add(Fragment.instantiate(this,fragment3.class.getName()));

    //add this line
    Collections.shuffle(fragments , new Random(System.nanoTime()));

    PagerAdapter mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
    pager.setAdapter(mPagerAdapter);

}

答案 1 :(得分:0)

public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm); 
        this.fragments = fragments;
        long seed = System.nanoTime();
        //shuffle the fragment list
        Collections.shuffle(fragments , new Random(seed));
} 

答案 2 :(得分:-1)

不确定这是否有所帮助,但尝试这样的事情

Random rand = new Random();

int  n = rand.nextInt(50) + 1;
\\50 is the maximum and the 1 is our minimum 

然后,您可以使用int来获取/生成您想要访问的名称,甚至可以将您的片段放入数组中并使用int(n)作为索引来访问数组