如何从“活动”向Viewpager中启动片段?

时间:2019-03-16 07:10:19

标签: android android-activity android-viewpager fragment

我有一个活动和一个包含两个片段的ViewPager。我想通过活动开始片段。我该怎么办?

1 个答案:

答案 0 :(得分:0)

  

在“活动”布局内创建 ViewPager 选项卡布局(如果需要标签)

    <android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_below="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="10dp"
    android:minHeight="?attr/actionBarSize"
    app:tabGravity="fill"
    app:tabMode="fixed"
    app:tabIndicatorColor="@color/green2"
    app:tabIndicatorHeight="4dp"
    app:tabIndicatorGravity="top"
    app:tabSelectedTextColor="#FFFFFF"
    app:tabTextColor="#555050" />

    <android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/tabLayout" />
  

创建一个ViewpageAdapter

    public class ViewPagerAdapter extends FragmentPagerAdapter {

    String[] names = new String[2];
    List<Fragment> fragmentList = new ArrayList<>();
    List<String> fragmentTitles = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
        names[0] = "Fragment One";
        names[1] = "Fragment Two";
    }

    @Override
    public Fragment getItem(int i) {

        return fragmentList.get(i);
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {

        return fragmentTitles.get(position);
    }

    public void addFragments(Fragment fragment, String title) {

        fragmentList.add(fragment);
        fragmentTitles.add(title);
    }
}
  

在您的活动类中,位于onCreate()中
     初始化此视图分页器

viewPager = findViewById(R.id.viewPager);
            viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
     //adding fragments to page
                viewPagerAdapter.addFragments(new Fragment1Class(), getResources().getString(R.string.tab1Name));
                viewPagerAdapter.addFragments(new Fragment2Class(), getResources().getString(R.string.tab2Name));

                //setUp
                viewPager.setAdapter(viewPagerAdapter);
                tabLayout = findViewById(R.id.tabLayout);
            tabLayout.setupWithViewPager(viewPager);
  

如果您使用的是标签页,也要添加这些标签页。

            tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                selectedTabPosition = getTabSelected();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

                selectedTabPosition = getTabSelected();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

                selectedTabPosition = getTabSelected();
            }
        });
  

不要忘记在片段类中添加一个空的构造函数


  

要在活动中开始特定片段

将此内容放入活动中,该活动以片段开始

    int fragOne=1;
int fragTwo=2;

Intent intent = new Intent(getBaseContext(), MainActivity.class);

//sending data to MainActivity
Bundle extras=new Bundle();
extras.putInt("fragmentId",fragOne);
intent.putExtras(extras);
startActivity(intent);

在活动中包含片段

//getting data
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle!=null) {
 int fragId=bundle.getInt("fragmentId");
}
if (fragmentId==1) {
    viewPagerAdapter.addFragments(new fragment1Class(), getResources().getString(R.string.tab1Name));
}else if (fragmentId==2) {
    viewPagerAdapter.addFragments(new fragment2Class(), getResources().getString(R.string.tab1Name));

}