注意新版本as自带 不需要重复导包
implementation 'com.android.support:design:30.0.0'
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mvp.view.activity.HomeActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
app:menu="@menu/menu_bottom"
android:id="@+id/bottom"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.google.android.material.bottomnavigation.BottomNavigationView>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom"
app:layout_constraintTop_toTopOf="parent"></androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:icon="@drawable/ic_home"
android:title="首页"
/>
<item
android:id="@+id/community"
android:icon="@drawable/ic_work"
android:title="社区"
/>
<item
android:id="@+id/live"
android:icon="@drawable/ic_mine_address"
android:title="直播"
/>
<item
android:id="@+id/me"
android:icon="@drawable/ic_mine"
android:title="我"
/>
</menu>
public class FragmentFactory {
//SparseArray 稀疏数组:安卓特有的类,默认键是int类型,节省内存,性能更好
private static SparseArray<Fragment> mFragments = new SparseArray<Fragment>();
/***
* 之前创建过直接获取,没有创建过创建出来并存储到稀疏数组中
* @param position 下标
* @return
*/
public static Fragment createFragment(int position){
Fragment fragment = mFragments.get(position);
if (fragment == null) {
switch (position) {
case 0:
fragment = new OneFragment();
break;
case 1:
fragment = new TwoFragment();
break;
case 2:
fragment = new ThreeFragment();
break;
}
if (fragment != null) {
mFragments.put(position, fragment);
}
}
return fragment;
}
}
ViewPager控件有个特有的预加载机制,即默认情况下当前页面左右两侧的1个页面会被加载,以方便用户滑动切换到相邻的界面时,可以更加顺畅的显示出来.
通过ViewPager的setOffscreenPageLimit(int limit)可以设置预加载页面数量,当前页面相邻的limit个页面会被预加载进内存
缺点:预加载越多就会越卡顿累计到后面就会OOM,懒加载实现就很必要
懒加载:其实也就是延迟加载,就是等到该页面的UI展示给用户时,再加载该页面的数据(从网络、数据库等),而不是依靠ViewPager预加载机制提前加载两三个,甚至更多页面的数据.这样可以提高所属Activity的初始化速度,也可以为用户节省流量.而这种懒加载的方式也已经/正在被诸多APP所采用.
(1)fragment适配器
public class MyFragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mList;
//behavior 重点属性行为实现懒加载
public MyFragmentAdapter(@NonNull FragmentManager fm, int behavior, List<Fragment> list) {
super(fm, behavior);
mList = list;
}
@NonNull
@Override
public Fragment getItem(int position) {
return mList.get(position);
}
@Override
public int getCount() {
return mList.size();
}
}
(2)创建多个fragment:此处省略
(3)activity中实现fragment切换
public class HomeActivity extends BaseActivity{
private BottomNavigationView bottom;
private ViewPager viewPager;
private List<Fragment> mList = new ArrayList<>();
private MyFragmentAdapter mMyFragmentAdapter;
@Override
protected int bindLayout() {
return R.layout.activity_home;
}
@Override
protected void initView(Bundle savedInstanceState) {
bottom = (BottomNavigationView) findViewById(R.id.bottom);
viewPager = (ViewPager) findViewById(R.id.view_pager);
}
@Override
protected void initData() {
mList.add(LiveFragmentFactory.createFragment(0));
mList.add(LiveFragmentFactory.createFragment(1));
mList.add(LiveFragmentFactory.createFragment(2));
mList.add(LiveFragmentFactory.createFragment(3));
//参数二:懒加载实现
mMyFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,mList);
viewPager.setAdapter(mMyFragmentAdapter);
bottom.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.home:
viewPager.setCurrentItem(0);
break;
case R.id.community:
viewPager.setCurrentItem(1);
break;
case R.id.live:
viewPager.setCurrentItem(2);
break;
case R.id.me:
viewPager.setCurrentItem(3);
break;
}
return true;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
bottom.getMenu().getItem(position).setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
(4)fragment在onResume生命周期中加载数据实现懒加载
public class SheQuFragment extends BaseFragment {
@Override
protected int bindLayout() {
return R.layout.fragment_she_qu;
}
//在onResume生命周期中加载数据实现懒加载
@Override
public void onResume() {
super.onResume();
}
@Override
protected void initView(Bundle savedInstanceState) {
}
@Override
protected void initData() {
}
}