1.我们先导入底部导航栏的依赖
//三方TabLayout
implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
//底部菜单
implementation 'com.github.chaychan:BottomBarLayout:2.0.2'
2.实现mainActivity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"></androidx.viewpager.widget.ViewPager>
<com.flyco.tablayout.CommonTabLayout
app:tl_textSelectColor="#03A9F4"
app:tl_textUnselectColor="#cccccc"
android:id="@+id/comm"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></com.flyco.tablayout.CommonTabLayout>
</LinearLayout>
3.定义导航栏的实例化对象TabEntity 实现CustomTabEntity
package com.bw.news_lx.entity;
import com.flyco.tablayout.listener.CustomTabEntity;
// 导航栏实例
public class TabEntity implements CustomTabEntity {
private String getTabTitle;
private int getTabSelectedIcon;
private int getTabUnselectedIcon;
public TabEntity(String getTabTitle, int getTabSelectedIcon, int getTabUnselectedIcon) {
this.getTabTitle = getTabTitle;
this.getTabSelectedIcon = getTabSelectedIcon;
this.getTabUnselectedIcon = getTabUnselectedIcon;
}
@Override
public String getTabTitle() {
return getTabTitle;
}
@Override
public int getTabSelectedIcon() {
return getTabSelectedIcon;
}
@Override
public int getTabUnselectedIcon() {
return getTabUnselectedIcon;
}
}
4.使用FragmentStatePagerAdapter
package com.bw.news_lx.adapter;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import java.util.ArrayList;
public class FragmentAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> list;
public FragmentAdapter(@NonNull FragmentManager fm, ArrayList<Fragment> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
}
@Override
public void destroyItem(@NonNull View container, int position, @NonNull Object object) {
}
}
5.创建几个Fragment
6.回到主页面初始化控件
7.准备数据源
8.定义适配器
9.设置适配器
10.建立联系
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private CommonTabLayout comm;
ArrayList<CustomTabEntity> mTabEntitys = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
ArrayList<Fragment> list = new ArrayList<>();
list.add(new HomeFragment());
list.add(new VideoFragment());
list.add(new HeadFragment());
list.add(new MyFragment());
FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),list);
vp.setAdapter(fragmentAdapter);
mTabEntitys.add(new TabEntity("首页",R.drawable.home_c,R.drawable.home_f));
mTabEntitys.add(new TabEntity("视频",R.drawable.video_c,R.drawable.video_f));
mTabEntitys.add(new TabEntity("头条",R.drawable.headnews,R.drawable.headnews_f));
mTabEntitys.add(new TabEntity("我的",R.drawable.my_c,R.drawable.my_f));
comm.setTabData(mTabEntitys);
comm.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int position) {
vp.setCurrentItem(position);
}
@Override
public void onTabReselect(int position) {
}
});
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
comm.setCurrentTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initView() {
vp = (ViewPager) findViewById(R.id.vp);
comm = (CommonTabLayout) findViewById(R.id.comm);
}
}