開發(fā)Android已經(jīng)有些時日了,接觸了很多APP結(jié)構(gòu),大部分的APP結(jié)構(gòu)都是如此!!!
這樣的APP模板已經(jīng)在當(dāng)下很成熟了,開發(fā)中,實現(xiàn)這種底部導(dǎo)航欄的方式也有很多,如:
RadioGroup+RadioButton,LinearLayout線性布局,TabLayout+ViewPager等,然而我們都需要設(shè)置一系列的監(jiān)聽,來實現(xiàn)切換fragment,開發(fā)自然會麻煩許多,今天給大家推薦一個使用的View(GYBottomBar),使用起來,非常的方便,省去了很多的麻煩,附上地址(https://github.com/gyadministrator/GYBottomBar)
實現(xiàn)效果如下:
怎樣使用?
Gradle引入方式:
allprojects {
repositories {
maven {
url 'https://jitpack.io'
}
}
}
dependencies {
implementation 'com.github.gyadministrator:GYBottomBar:1.3'
}
Maven引入方式:
第一步,添加到build文件中
jitpack.io
https://jitpack.io
第二步,添加依賴
com.github.gyadministrator
GYBottomBar
1.3
在activity使用,非常簡單。
在xml中添加這個view。
activity中使用
package com.android.gybottombar;
import android.widget.Toast;
import com.android.bottombar.activity.GYBottomActivity;
import com.android.bottombar.model.GYBarItem;
import com.android.bottombar.view.GYBottomBarView;
import com.android.gybottombar.fragment.InfoFragment;
import com.android.gybottombar.fragment.ContactFragment;
import com.android.gybottombar.fragment.FindFragment;
import com.android.gybottombar.fragment.MyFragment;
public class MainActivity extends GYBottomActivity implements GYBottomBarView.IGYBottomBarChangeListener {
private GYBottomBarView bottomView;
@Override
public void onSelected(int position) {
Toast.makeText(this, "點擊了" + position, Toast.LENGTH_SHORT).show();
}
@Override
protected void initBarItems() {
barItems.add(new GYBarItem("微信", R.mipmap.home_normal));
barItems.add(new GYBarItem("通信錄", R.mipmap.category_normal));
barItems.add(new GYBarItem("發(fā)現(xiàn)", R.mipmap.service_normal));
barItems.add(new GYBarItem("我", R.mipmap.mine_normal));
}
@Override
protected void initFragment() {
fragments.add(InfoFragment.newInstance());
fragments.add(ContactFragment.newInstance());
fragments.add(FindFragment.newInstance());
fragments.add(MyFragment.newInstance());
}
@Override
protected void initSelectIcons() {
icons.add(R.mipmap.home_selected);
icons.add(R.mipmap.category_selected);
icons.add(R.mipmap.service_selected);
icons.add(R.mipmap.mine_selected);
}
@Override
protected int initContentView() {
return R.layout.activity_main;
}
@Override
protected GYBottomBarView getBottomBarView() {
return bottomView;
}
@Override
protected int initContainerId() {
return R.id.fl_container;
}
@Override
protected GYBottomBarView.IGYBottomBarChangeListener initChangeListener() {
return this;
}
@Override
protected void initView() {
bottomView = findViewById(R.id.bottomView);
}
@Override
protected void initPositionBadge() {
super.initPositionBadge();
bottomView.setPositionBadge(0, 6);
bottomView.setPositionBadge(1, 0);
bottomView.setPositionBadge(2, 100);
}
}
bottomView.setPositionBadge(3, -1);
num設(shè)置為小于0的時候,顯示小圓點
這樣你就可以省去很多麻煩了!!!!