Open AliRezaBeitari opened 6 years ago
This is my code:
import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.view.ViewPager; import android.support.v4.view.animation.LinearOutSlowInInterpolator; import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatDelegate; import android.view.View; import android.view.animation.OvershootInterpolator; import android.widget.Toast; import com.aurelhubert.ahbottomnavigation.AHBottomNavigation; import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem; import com.aurelhubert.ahbottomnavigation.AHBottomNavigationViewPager; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements PageFragment.OnFragmentInteractionListener { private MainViewPagerAdapter adapter; private ArrayList<AHBottomNavigationItem> bottomNavigationItems = new ArrayList<>(); // UI private AHBottomNavigationViewPager viewPager; private AHBottomNavigation bottomNavigation; private FloatingActionButton floatingActionButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); initUI(); } /** * Init UI */ private void initUI() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } bottomNavigation = findViewById(R.id.bottom_navigation); viewPager = findViewById(R.id.view_pager); viewPager.setPagingEnabled(true); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { bottomNavigation.setCurrentItem(position); animateFloatingButton(position); } @Override public void onPageScrollStateChanged(int state) { } }); floatingActionButton = findViewById(R.id.floating_action_button); AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.tab_1, R.drawable.ic_apps_black_24dp, R.color.color_tab_1); AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.tab_2, R.drawable.ic_maps_local_bar, R.color.color_tab_2); AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.tab_3, R.drawable.ic_maps_local_restaurant, R.color.color_tab_3); bottomNavigationItems.add(item1); bottomNavigationItems.add(item2); bottomNavigationItems.add(item3); bottomNavigation.addItems(bottomNavigationItems); bottomNavigation.setColored(true); bottomNavigation.setSelectedBackgroundVisible(true); bottomNavigation.manageFloatingActionButtonBehavior(floatingActionButton); bottomNavigation.setTranslucentNavigationEnabled(true); bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() { @Override public boolean onTabSelected(int position, boolean wasSelected) { if (wasSelected) { return true; } viewPager.setCurrentItem(position); animateFloatingButton(position); return true; } }); viewPager.setOffscreenPageLimit(3); adapter = new MainViewPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); bottomNavigation.setCurrentItem(1); } @Override public void onFragmentInteraction(Uri uri) { } void animateFloatingButton(int position) { if (position == 1) { floatingActionButton.setVisibility(View.VISIBLE); floatingActionButton.setAlpha(0f); floatingActionButton.setScaleX(0f); floatingActionButton.setScaleY(0f); floatingActionButton.animate() .alpha(1) .scaleX(1) .scaleY(1) .setDuration(300) .setInterpolator(new OvershootInterpolator()) .setListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { floatingActionButton.animate() .setInterpolator(new LinearOutSlowInInterpolator()) .start(); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }) .start(); } else { if (floatingActionButton.getVisibility() == View.VISIBLE) { floatingActionButton.animate() .alpha(0) .scaleX(0) .scaleY(0) .setDuration(300) .setInterpolator(new LinearOutSlowInInterpolator()) .setListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { floatingActionButton.setVisibility(View.GONE); } @Override public void onAnimationCancel(Animator animation) { floatingActionButton.setVisibility(View.GONE); } @Override public void onAnimationRepeat(Animator animation) { } }) .start(); } } } }
When I slide the view pager, background color animation will be triggered; but when I click on each tab it won't!! Where is the problem?!
Thanks!
This is my code:
When I slide the view pager, background color animation will be triggered; but when I click on each tab it won't!! Where is the problem?!
Thanks!