Вы должны обращаться с плавающей кнопкой в MainActiviy, которая обрабатывает все ваши фрагменты.Попробуйте сделать это в MainActity:
myFab = (FloatingActionButton) findViewById(R.id.Main_FAB);
myFab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), iconIntArray[0]));
myFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = tabLayout.getSelectedTabPosition();
switch (position) {
case 0:
myFab.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, iconIntArray[0]));
intent = new Intent(MainActivity.this,
EnterExpensesActivity.class);
intent.putExtra("USER_ID",USER_ID);
intent.putExtra("FRAGMENT",0);
intent.putExtra("TITLE",getResources().getString(R.string.Enter_Expense_title));
intent.putExtra("FILED",getResources().getString(R.string.Enter_Expense_text));
startActivity(intent);
break;
case 1:
myFab.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, iconIntArray[position]));
intent = new Intent(MainActivity.this,
Enter_E_L_Items.class);
intent.putExtra("USER_ID",USER_ID);
intent.putExtra("FRAGMENT",0);
intent.putExtra("TITLE",getResources().getString(R.string.Enter_Expense_title));
intent.putExtra("FILED",getResources().getString(R.string.Enter_Expense_text));
startActivity(intent);
/*
Snackbar.make(view, "Expenses ", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
break;
case 2:
myFab.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, iconIntArray[position]));
intent = new Intent(MainActivity.this,
Enter_E_L_Items.class);
intent.putExtra("USER_ID",USER_ID);
intent.putExtra("FRAGMENT",1);
intent.putExtra("TITLE",getResources().getString(R.string.Enter_Liange_title));
intent.putExtra("FILED",getResources().getString(R.string.Enter_Linage_text));
startActivity(intent);
/*
Snackbar.make(view, "Linage ", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
break;
}
}
});
Я предполагаю, что вы используете вкладку по умолчанию.
Вы можете изменить и установить анимацию для плавающей кнопки следующим образом:
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewpager.setCurrentItem(tab.getPosition());
animateFab(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
setupTabIcons();
}
Я создал метод animateFab следующим образом:
protected void animateFab(final int position) {
final FloatingActionButton fab = findViewById(R.id.Main_FAB);
fab.clearAnimation();
// Scale down animation
ScaleAnimation shrink = new ScaleAnimation(1f, 0.1f, 1f, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
shrink.setDuration(100); // animation duration in milliseconds
shrink.setInterpolator(new AccelerateInterpolator());
shrink.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// Change FAB color and icon
fab.setBackgroundTintList(ContextCompat.getColorStateList(getApplicationContext(), colorIntArray[position]));
fab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), iconIntArray[position]));
// Rotate Animation
Animation rotate = new RotateAnimation(60.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(150);
rotate.setInterpolator(new DecelerateInterpolator());
// Scale up animation
ScaleAnimation expand = new ScaleAnimation(0.1f, 1f, 0.1f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
expand.setDuration(150); // animation duration in milliseconds
expand.setInterpolator(new DecelerateInterpolator());
// Add both animations to animation state
AnimationSet s = new AnimationSet(false); //false means don't share interpolators
s.addAnimation(rotate);
s.addAnimation(expand);
fab.startAnimation(s);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
fab.startAnimation(shrink);
}