第一次用此控件,所以在用的过程中出现了一些问题,现将使用方法和问题及解决方案做个记录。
使用步骤:
一、在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并实现点击监听;
//初始化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
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/bottombar_menu_home"
android:title="@string/bottombar_menu_home_title"
android:icon="@drawable/bottom_home_icon"/>
<item
android:id="@+id/bottombar_menu_mv"
android:title="@string/bottombar_menu_mv_title"
android:icon="@drawable/bottom_mv_unselect"/>
<item
android:id="@+id/bottombar_menu_vbang"
android:title="@string/bottombar_menu_vbang_title"
android:icon="@drawable/bottom_vbang_unselect"/>
<item
android:id="@+id/bottombar_menu_yuedan"
android:title="@string/bottombar_menu_yuedan_title"
android:icon="@drawable/bottom_yuedan_unselect"/>
</menu>
至此,BottomBar的简单功能实现就完成了,做到最后效果实现了,但我发现在布局文件中并没有添加BottomBar控件,看了下源码,原来诀窍在attatch方法~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.aaaaa.myvmplayer.activity.MainActivity">
<!-- AppBarLayout 可以使内部的子控件具备标题栏的显示风格(背景色)
用之前需添加依赖包-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 状态栏 -->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<!-- 正文容器 -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
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;
}