前言
最后在Activity中实现通信:
public class MainActivity extends AppCompatActivity{
private ArrayList fragmentArrayList = new ArrayList<>();
private Fragment mCurrentFragment;
private BottomNavigationBar mBottomNavigationBar;
BadgeItem badgeItem;
FragmentManager mFragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
mFragmentManager = getSupportFragmentManager();
initFragemnt();
showFragment(0);
mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
mBottomNavigationBar.setBarBackgroundColor(android.R.color.white);
badgeItem = new BadgeItem()
.setBackgroundColor(Color.RED).setText("99")
.setHideOnSelect(true); //设置被选中时隐藏角标
mBottomNavigationBar
.setActiveColor(R.color.colorAccent) //设置选中的颜色
.setInActiveColor(R.color.colorPrimary);//未选中
mBottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "首页"))
.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "店铺"))
.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "购物车"))
.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "我的").setBadgeItem(badgeItem))
.initialise();
mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
@Override
public void onTabSelected(int position) {
showFragment(position);
}
@Override
public void onTabUnselected(int position) {
}
@Override
public void onTabReselected(int position) {
}
});
}
private void showFragment(int page) {
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
if (mCurrentFragment != null) {
mFragmentTransaction.hide(mCurrentFragment);
}
mCurrentFragment = fragmentArrayList.get(page);
if (mCurrentFragment.isAdded())
{
mFragmentTransaction.show(mCurrentFragment);
}else {
mFragmentTransaction.add(R.id.fragmenta, mCurrentFragment,mCurrentFragment.getClass().getName());
}
mFragmentTransaction.commitAllowingStateLoss();
}
private void initFragemnt(){
fragmentArrayList.add(new A());
fragmentArrayList.add(new B());
fragmentArrayList.add(new C());
fragmentArrayList.add(new D());
}
public void setFunctionForFragment(final String tag){
BaseFragment fragment=(BaseFragment)mFragmentManager.findFragmentByTag(tag);
FunctionManager functionManager=FunctionManager.getInstance();
fragment.setmFunctionManager(functionManager.addFunction(new FunctionNoParamNotResult(A.INTERFCE) {
@Override
public void function() {
Toast.makeText(MainActivity.this,"无参无返回值"+tag,Toast.LENGTH_LONG).show();
}
}).addFunction(new FunctionNoParamWithResult(B.INTERFCE) {
@Override
public String function() {
Toast.makeText(MainActivity.this,"无参有返回值",Toast.LENGTH_LONG).show();
return "张三";
}
}).addFunction(new FunctionWithParamNoResult(C.INTERFCE) {
@Override
public void function(Integer o) {
Toast.makeText(MainActivity.this,"有参无返回值"+o,Toast.LENGTH_LONG).show();
}
}).addFunction(new FunctionWithParamResultn(D.INTERFCE) {
@Override
public String function(String o) {
Toast.makeText(MainActivity.this,"有参有返回值"+o,Toast.LENGTH_LONG).show();
return "zhangqie";
}
}));
}
}
效果图:
源码下载Github:https://github.com/DickyQie/android-fragment-interface