依賴//底部菜單
implementation'com.github.chaychan:BottomBarLayout:2.0.2'
這里記得添加整個(gè)項(xiàng)目的build
repositories{
? ? google()
jcenter()
maven{ url'https://jitpack.io' }
}
//布局文件
? ? android:layout_width="match_parent"
? ? android:layout_height="1px"
? ? android:background="#F1F1F1" />
? ? android:id="@+id/act_main_bottom_bar"
? ? android:layout_width="match_parent"
? ? android:layout_height="@dimen/dp_50"
? ? android:background="@color/white"
? ? android:orientation="horizontal">
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:layout_weight="1"
? ? ? ? app:iconHeight="@dimen/dp_28"
? ? ? ? app:iconNormal="@mipmap/ic_tab_news_nor"
? ? ? ? app:iconSelected="@mipmap/ic_tab_news_select"
? ? ? ? app:iconWidth="@dimen/dp_28"
? ? ? ? app:itemText="新聞"
? ? ? ? app:itemTextSize="@dimen/sp_10"
? ? ? ? app:textColorNormal="@color/text_99"
? ? ? ? app:textColorSelected="@color/main_color" />
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:layout_weight="1"
? ? ? ? app:iconHeight="@dimen/dp_28"
? ? ? ? app:iconNormal="@mipmap/ic_tab_video_nor"
? ? ? ? app:iconSelected="@mipmap/ic_tab_video_select"
? ? ? ? app:iconWidth="@dimen/dp_28"
? ? ? ? app:itemText="視頻"
? ? ? ? app:itemTextSize="@dimen/sp_10"
? ? ? ? app:textColorNormal="@color/text_99"
? ? ? ? app:textColorSelected="@color/main_color" />
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:layout_weight="1"
? ? ? ? app:iconHeight="@dimen/dp_28"
? ? ? ? app:iconNormal="@mipmap/ic_tab_live_nor"
? ? ? ? app:iconSelected="@mipmap/ic_tab_live_select"
? ? ? ? app:iconWidth="@dimen/dp_28"
? ? ? ? app:itemText="新聞"
? ? ? ? app:itemTextSize="@dimen/sp_10"
? ? ? ? app:textColorNormal="@color/text_99"
? ? ? ? app:textColorSelected="@color/main_color" />
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:layout_weight="1"
? ? ? ? app:iconHeight="@dimen/dp_28"
? ? ? ? app:iconNormal="@mipmap/ic_tab_mine_nor"
? ? ? ? app:iconSelected="@mipmap/ic_tab_mine_select"
? ? ? ? app:iconWidth="@dimen/dp_28"
? ? ? ? app:itemText="新聞"
? ? ? ? app:itemTextSize="@dimen/sp_10"
? ? ? ? app:textColorNormal="@color/text_99"
? ? ? ? app:textColorSelected="@color/main_color" />
</com.chaychan.library.BottomBarLayout>
MainActivity
/**
* 頁(yè)面控制
*/
public class MainActivityextends BaseActivity {
private BottomBarLayoutbottomBarLayout;
? ? private Listfragments;
? ? private androidx.viewpager.widget.ViewPageractMainLayout;
? ? private Listtittles;
? ? @Override
? ? public void initBar() {
BarUtils.setStatusBarColor(this, Color.WHITE);
? ? ? ? BarUtils.setStatusBarLightMode(this, true);
? ? }
@Override
? ? public int bandLayout() {
return R.layout.activity_main;
? ? }
@Override
? ? public void initView() {
bottomBarLayout = findViewById(R.id.act_main_bottom_bar);
? ? ? ? actMainLayout = findViewById(R.id.act_main_layout);
? ? ? ? tittles=new ArrayList<>();
? ? }
@Override
? ? public void initData() {
initFragment();
? ? ? ? initBottomSelect();
? ? ? ? bottomBarLayout.setCurrentItem(0);
? ? }
/**
? ? * 初始化Fragment
*/
? ? private void initFragment() {
fragments =new ArrayList<>();
? ? ? ? fragments.add(new NewsFragment());
? ? ? ? fragments.add(new VideoFragment());
? ? ? ? fragments.add(new LiveFragment());
? ? ? ? fragments.add(new MineFragment());
//重要部分添加聯(lián)動(dòng)
? ? ? ? bottomBarLayout.setSmoothScroll(true);
? ? ? ? TestFragmentAdapter adapter =new TestFragmentAdapter(getSupportFragmentManager(), fragments,tittles);
? ? ? ? actMainLayout.setAdapter(adapter);
? ? ? ? bottomBarLayout.setViewPager(actMainLayout);
? ? }
/**
? ? * 點(diǎn)擊控制頁(yè)面顯示
? ? * add hide show rp rm
*/
? ? private void initBottomSelect() {
//? ? ? ? bottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
//? ? ? ? ? ? @Override
//? ? ? ? ? ? public void onItemSelected(BottomBarItem bottomBarItem, int previousPosition, int currentPosition) {
//? ? ? ? ? ? ? ? if (previousPosition == currentPosition && currentPosition == 0 && !fragments.get(0).isAdded()) {
//? ? ? ? ? ? ? ? ? ? //第一次點(diǎn)擊0位置
//? ? ? ? ? ? ? ? ? ? getSupportFragmentManager().beginTransaction().add(R.id.act_main_layout, fragments.get(currentPosition)).commit();
//? ? ? ? ? ? ? ? } else if (previousPosition != currentPosition && fragments.get(currentPosition).isAdded()) {
//? ? ? ? ? ? ? ? ? ? //之前有頁(yè)面,新頁(yè)面 展示
//? ? ? ? ? ? ? ? ? ? getSupportFragmentManager().beginTransaction().hide(fragments.get(previousPosition)).show(fragments.get(currentPosition)).commit();
//? ? ? ? ? ? ? ? } else if (previousPosition != currentPosition) {
//? ? ? ? ? ? ? ? ? ? //之前有頁(yè)面,新頁(yè)面 添加
//? ? ? ? ? ? ? ? ? ? getSupportFragmentManager().beginTransaction().hide(fragments.get(previousPosition)).add(R.id.act_main_layout, fragments.get(currentPosition)).commit();
//? ? ? ? ? ? ? ? }
//? ? ? ? ? ? }
//? ? ? ? });
? ? }
}