先建一个带底部菜单的
ViewPager切换 碎片
1、activity_main.xml
<android.support.v4.view.ViewPager android:id="@+id/viewpager_a" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager>
2、根据引导创建两个Fragment
public class MainActivity extends AppCompatActivity implements BlankFragmentM.OnFragmentInteractionListener,PlusOneFragment.OnFragmentInteractionListener { private TextView mTextMessage; private ViewPager viewPager; private List<Fragment> fragmentList = new ArrayList<>(); private BlankFragmentM fragmentM; private PlusOneFragment fragmentN; private MyPagerAdapter adapter; private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.navigation_home: mTextMessage.setText(R.string.title_home); viewPager.setCurrentItem(0); return true; case R.id.navigation_dashboard: mTextMessage.setText(R.string.title_dashboard); viewPager.setCurrentItem(1); return true; case R.id.navigation_notifications: mTextMessage.setText(R.string.title_notifications); viewPager.setCurrentItem(0); return true; } return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextMessage = (TextView) findViewById(R.id.message); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); bindID(); fragmentM = new BlankFragmentM(); fragmentN = new PlusOneFragment(); //填充数据 fragmentList.add(fragmentM); fragmentList.add(fragmentN); adapter = new MyPagerAdapter(getSupportFragmentManager(),fragmentList); viewPager.setAdapter(adapter); viewPager.setCurrentItem(0); } private void bindID() { viewPager = findViewById(R.id.viewpager_a); } @Override public void onFragmentInteraction(Uri uri) { } public class MyPagerAdapter extends FragmentPagerAdapter { private List<Fragment> mfragmentList; public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) { super(fm); this.mfragmentList = fragmentList; } @Override public Fragment getItem(int position) { return mfragmentList.get(position); } @Override public int getCount() { return mfragmentList.size(); } } }