内容简介:MainActivityactivity_main.xml
- 新建android工程,选Bottom Navigation Actvity项
- 使用向导新建三个Fragment(Blank)
-
编辑activity_main.xml
- 增加ViewPager,并设置id,(Design>Containers>ViewPager)
-
编辑navigation.xml,给每个菜单增加
android:orderInCategory="0"
序号依次增加 -
编辑MainActivity
ViewPager viewpager ViewPager.OnPageChangeListener navigation.getMenu().getItem(position).setChecked(true); viewpager.setCurrentItem(item.getOrder()); List<Fragment> fragmentList =new ArrayList<Fragment>();
- 分编辑 BlankFragment中的文字内容
代码
MainActivity
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener ,BlankFragment.OnFragmentInteractionListener,BlankFragment2.OnFragmentInteractionListener,BlankFragment3.OnFragmentInteractionListener{ private BottomNavigationView navigation; private ViewPager viewpager; private List<Fragment> fragmentList =new ArrayList<Fragment>(); private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { viewpager.setCurrentItem(item.getOrder()); return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); viewpager =(ViewPager)findViewById(R.id.viewpager); viewpager.addOnPageChangeListener(this); Fragment fragment1 = new BlankFragment(); Fragment fragment2 = new BlankFragment2(); Fragment fragment3 = new BlankFragment3(); fragmentList.add(fragment1); fragmentList.add(fragment2); fragmentList.add(fragment3); viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } }); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { navigation.getMenu().getItem(position).setChecked(true); } @Override public void onPageScrollStateChanged(int state) { } @Override public void onFragmentInteraction(Uri uri) { } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="0dp" android:layout_marginStart="0dp" android:background="?android:attr/windowBackground" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/navigation" /> </android.support.constraint.ConstraintLayout>
以上所述就是小编给大家介绍的《Android使用BottomNavigationView及ViewPager布局》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 使用 Flex 布局与其他普通布局的简单对比
- Flutter布局中Expanded使用
- iOS使用代码进行AutoLayout自动布局
- Android开发 - 使用ConstraintLayout(一)传统布局的问题
- iOS 中使用 FlexBox 布局实现图片九宫格
- (译)使用渲染函数构建一个设计系统的排版布局
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。