Android 開發之BottomBar ViewPager Fragment實現炫酷的底部導航效果

Android 開發之BottomBar ViewPager Fragment實現炫酷的底部導航效果
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

BottomBar

這裡寫圖片描述

  BottomBar是Github上的一個開源框架,因為從1.3.3開始不支援fragments了,要自己配置,弄了很久,不管是app的fragment還是V4 的程式總是總是閃退。於是就用這種方式實現了,效果還不錯。github有詳細說明,多餘的就不說了。

這裡寫圖片描述

  這個roughike是這個專案的所有者(大神致敬)。

  我用的是Android studio開發,fragment全部導的V4的包(以為最開始就支援的是v4的,後面也支援了app.fragment).

首先是dependencies

compile ‘com.jakewharton:butterknife:7.0.0’
compile ‘com.roughike:bottom-bar:1.3.3’

新增第二個就行了,我這用到了butterknife(不知道的可以百度,出自jakewharton大神的一款View注入框架)。

從menu新增items

res/menu/bottombar_menu.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@ id/bb_menu_recents"
android:icon="@drawable/ic_recents"
android:title="Recents" />
<item
android:id="@ id/bb_menu_favorites"
android:icon="@drawable/ic_favorites"
android:title="Favorites" />
<item
android:id="@ id/bb_menu_nearby"
android:icon="@drawable/ic_nearby"
android:title="Nearby" />
<item
android:id="@ id/bb_menu_friends"
android:icon="@drawable/ic_friends"
android:title="Friends" />
<item
android:id="@ id/bb_menu_food"
android:icon="@drawable/ic_restaurants"
android:title="Food" />
</menu>

在activity中初始化BottomBar和ViewPager


public class MainActivity extends FragmentActivity {
@Bind(R.id.viewPager)
ViewPager viewPager;
@Bind(R.id.myCoordinator)
CoordinatorLayout myCoordinator;
private BottomBar mBottomBar;
private List<Fragment> fragmentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initViewPager();
createBottomBar(savedInstanceState);
}
private void createBottomBar(Bundle savedInstanceState) {
mBottomBar = BottomBar.attachShy(myCoordinator,viewPager, savedInstanceState);
mBottomBar.setItemsFromMenu(R.menu.bottombar_menu, new OnMenuTabClickListener() {
@Override
public void onMenuTabSelected(@IdRes int menuItemId) {
switch (menuItemId) {
case R.id.bb_menu_recents:
viewPager.setCurrentItem(0);
break;
case R.id.bb_menu_favorites:
viewPager.setCurrentItem(1);
break;
case R.id.bb_menu_nearby:
break;
case R.id.bb_menu_friends:
break;
case R.id.bb_menu_food:
break;
}
}
@Override
public void onMenuTabReSelected(@IdRes int menuItemId) {
}
});
// Setting colors for different tabs when there's more than three of them.
// You can set colors for tabs in three different ways as shown below.
mBottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorAccent));
mBottomBar.mapColorForTab(1, 0xFF5D4037);
mBottomBar.mapColorForTab(2, "#7B1FA2");
mBottomBar.mapColorForTab(3, "#FF5252");
mBottomBar.mapColorForTab(4, "#FF9800");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Necessary to restore the BottomBar's state, otherwise we would
// lose the current tab on orientation change.
mBottomBar.onSaveInstanceState(outState);
}
private void initViewPager() {
fragmentList = new ArrayList<>();
fragmentList.add(new FragmentOne());
fragmentList.add(new FragmentTwo());
viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mBottomBar.selectTabAtPosition(position, true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}

  BottomBar的github上提供了兩種初始化方式,這裡是第二種實現下滑隱藏,因為是fragment滾動所以fragment的佈局要被NestedScrollView包裹(下面貼程式碼,很簡單的),同時注意viewPager.setOnPageChangeListener已經過時了。

layout/activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/myCoordinator"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.bottombar.bottombar.MainActivity">
<android.support.v4.view.ViewPager
android:id="@ id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>

FragmentOne.Java


public class FragmentOne extends Fragment {
View v;
Context context;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_one, container,false);
context = getActivity();
return v;
}
}

layout/fragment_one.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:id="@ id/myScrollView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/baiduInfo"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

以上所述是小編給大家介紹的Android 開發之BottomBar ViewPager Fragment實現炫酷的底部導航效果的相關知識,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對指令碼之家網站的支援!

您可能感興趣的文章:

Android使用ViewPager實現導航Android中TabLayout ViewPager 簡單實現app底部Tab導航欄Android自定義ViewPagerIndicator實現炫酷導航欄指示器(ViewPager Fragment)Android動態給ViewPager新增Indicator導航Android 利用ViewPager GridView實現首頁導航欄佈局分頁效果Android 中 TabHost與ViewPager結合實現首頁導航效果ViewPager頂部導航欄聯動效果(標題欄條目多)Android ViewPager製作新手導航頁(動態載入)Android ViewPager導航小圓點實現無限迴圈效果

相關文章

Android 開發 最新文章