BottomBar

第一次用此控件,所以在用的过程中出现了一些问题,现将使用方法和问题及解决方案做个记录。
使用步骤:
一、在build.gradle中添加依赖;

dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.1' testCompile 'junit:junit:4.12' compile 'com.android.support:design:25.1.1' compile 'com.jakewharton:butterknife:5.1.1' //添加bottombar的依赖 compile 'com.roughike:bottom-bar:1.3.3' }

问题一、在创建调用BottomBar的方法后出现以下问题:
error resolve:com.roughhike:bottom-bar:1.3.3。
怎么解决呢?
在网上找了一些资料,链接如下:
https://stackoverflow.com/questions/39435772/eror-resolve-com-roughikebottom-bar1-4-0-1
https://stackoverflow.com/questions/46023971/failed-to-resolve-com-android-supportsupport-annotations-26-0-1
综合资料在build.gradle中添加以下代码后问题解决(具体是什么原因本人也不是很清楚~~)
allprojects { repositories { jcenter() maven { url 'https://maven.google.com' } } }

【BottomBar】二、代码中初始化BottomBar并实现点击监听;
//初始化Fragment mSparseArray = new SparseArray<>(); mSparseArray.append(R.id.bottombar_menu_home, new HomeFragment()); mSparseArray.append(R.id.bottombar_menu_mv, new MVFragment()); mSparseArray.append(R.id.bottombar_menu_vbang, new VBangFragment()); mSparseArray.append(R.id.bottombar_menu_yuedan, new YueDanFragment()); //底部栏 BottomBar bottomBar = BottomBar.attach(this, savedInstanceState); bottomBar.setItemsFromMenu(R.menu.bottombar_menu, new OnMenuTabClickListener() { @Override public void onMenuTabSelected(@IdRes int menuItemId) { Fragment fragment = mSparseArray.get(menuItemId); switchFragment(fragment); }@Override public void onMenuTabReSelected(@IdRes int menuItemId) { Toast.makeText(MainActivity.this, "MenuTabReSelected", Toast.LENGTH_SHORT).show(); } }); public void switchFragment(Fragment fragment){ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.container, fragment); transaction.commit(); }

三、创建底部Tab的配置文件bottombar_menu.xml

至此,BottomBar的简单功能实现就完成了,做到最后效果实现了,但我发现在布局文件中并没有添加BottomBar控件,看了下源码,原来诀窍在attatch方法~~

public static BottomBar attach(Activity activity, Bundle savedInstanceState) { BottomBar bottomBar = new BottomBar(activity); bottomBar.onRestoreInstanceState(savedInstanceState); ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); View oldLayout = contentView.getChildAt(0); contentView.removeView(oldLayout); bottomBar.setPendingUserContentView(oldLayout); contentView.addView(bottomBar, 0); return bottomBar; }

    推荐阅读