import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import com.roughike.bottombar.BottomBar; import com.roughike.bottombar.OnTabReselectListener; import com.roughike.bottombar.OnTabSelectListener; import java.util.ArrayList; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity { @BindView(R.id.bottomBar) BottomBar bottomBar; @BindView(R.id.vp) ViewPager vp; private Recommend fragment2; private Discover fragment1; private ArrayList<Fragment> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); list = new ArrayList<>(); list.add(new Recommend()); list.add(new Anepisode()); list.add(new Discover()); list.add(new Video()); vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }); vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override //滑动时 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } //页面选中时 @Override public void onPageSelected(int position) { switch (position){ case 0: bottomBar.selectTabWithId(R.id.tab_shouye); break; case 1: bottomBar.selectTabWithId(R.id.tab_all); break; case 2: bottomBar.selectTabWithId(R.id.tab_cart); break; case 3: bottomBar.selectTabWithId(R.id.tab_account); break; } } //滑动状态改变时 @Override public void onPageScrollStateChanged(int state) { } }); bottomBar.setOnTabSelectListener(new OnTabSelectListener() { @Override public void onTabSelected(int tabId) { switch (tabId){ case R.id.tab_shouye: vp.setCurrentItem(0,false); break; case R.id.tab_all: vp.setCurrentItem(1,false); break; case R.id.tab_cart: vp.setCurrentItem(2,false); break; case R.id.tab_account: vp.setCurrentItem(3,false); break; } } }); } } //布局
<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"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="9" android:id="@+id/vp" > </android.support.v4.view.ViewPager> <com.roughike.bottombar.BottomBar android:id="@+id/bottomBar" android:layout_width="match_parent" android:layout_height="56dp" android:layout_weight="1" app:bb_tabXmlResource="@xml/xml" android:layout_alignParentBottom="true" /> </LinearLayout>//自建文件夹 在里边创建xml文件如下
<tabs> <tab id="@+id/tab_shouye" title="推荐" icon="@drawable/shouye" /> <tab id="@+id/tab_all" title="段子" icon="@drawable/all" /> <tab id="@+id/tab_cart" title="发现" icon="@drawable/cart" /> <tab id="@+id/tab_account" title="视频" icon="@drawable/account" /> </tabs>//bottombar 依赖
implementation 'com.roughike:bottom-bar:2.0.2' //butterknifr依赖 implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'