【问题标题】:How to choose which Fragment opens first?如何选择先打开哪个 Fragment?
【发布时间】:2014-11-27 07:49:06
【问题描述】:

我正在处理一个带有标签的 ActionBar 活动,该活动托管三个片段(片段 A、B、C)。问题是当您打开活动时显示片段 A,因为它是第一个,所以我试图制作一个直接打开片段 C 的按钮和另一个直接打开片段 B 的按钮。关于如何制作的任何想法,谢谢:D

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    带有标签的活动:

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    
        Bundle extras = getIntent().getExtras();
        int position = extras.getInt("position");
    
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
        mViewPager.setAdapter(mViewPagerAdapter);
    
        // etc etc etc
    
        mViewPager.setCurrentItem(position);
    }
    

    并在您的第一个 Activity 上使用 Bundle 传递所需的位置:

     Intent intent = new Intent(this, SecondActivity.class);
     i.putExtra("position", positionTabDesired);
     startActivity(intent);
    

    【讨论】:

    • 对不起,我不明白我应该做什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    相关资源
    最近更新 更多