編輯:關於Android編程
BottomBar是Github上的一個開源框架,因為從1.3.3開始不支持fragments了,要自己配置,弄了很久,不管是app的fragment還是V4 的程序總是總是閃退。於是就用這種方式實現了,效果還不錯。github有詳細說明,多余的就不說了。
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPtXiuPZyb3VnaGlrZcrH1eK49s/uxL+1xMv509DV36OotPPJ8dbCvrSjqaGjPC9wPg0KPHA+ztLTw7XEysdhbmRyb2lkIHN0dWRpb7+qt6KjrGZyYWdtZW50yKuyv7W8tcRWNLXEsPyjqNLUzqrX7r+qyry+zdans9a1xMrHdjS1xKOsuvPD5tKy1qez1sHLYXBwLmZyYWdtZW50o6kuPC9wPg0KPGgxIGlkPQ=="首先是dependencies">首先是dependencies
compile 'com.jakewharton:butterknife:7.0.0'
compile 'com.roughike:bottom-bar:1.3.3'
添加第二個就行了,我這用到了butterknife(不知道的可以百度,出自jakewharton大神的一款View注入框架)。
res/menu/bottombar_menu.xml
public class MainActivity extends FragmentActivity {
@Bind(R.id.viewPager)
ViewPager viewPager;
@Bind(R.id.myCoordinator)
CoordinatorLayout myCoordinator;
private BottomBar mBottomBar;
private List 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
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
微信支付是騰訊公司最新推出的一種支付方式,是集成在微信客戶端的支付功能,用戶可以通過手機完成快速的支付流程。還有一些朋友不知道微信支付怎麼用的,下面小編通過
使用數據線調試應用難免不方便,本篇博客介紹使用ADB Wireless工具,當手機和電腦處在同一網絡下,實現無線調試應用。ADB Wireless能夠讓手機用無線來取代U
Android L: Google已經確認Android L就是Android Lollipop(5
手機上輸入中文相當容易,但是在靠譜助手這個電腦上的安卓模擬器要怎麼輸入中文呢?電腦上直接輸入中文根本沒反應啊?靠譜助手模擬器輸入中文需要安裝拼音輸入法。經小