Created
April 4, 2018 01:20
-
-
Save Jabriko/466a995b37dcc60a3b855689b952a564 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package id.indrasudirman.tablayoutwithfab; | |
| import android.os.Build; | |
| import android.os.Bundle; | |
| import android.support.annotation.RequiresApi; | |
| import android.support.design.widget.FloatingActionButton; | |
| import android.support.design.widget.Snackbar; | |
| import android.support.design.widget.TabLayout; | |
| import android.support.v4.app.Fragment; | |
| import android.support.v4.app.FragmentManager; | |
| import android.support.v4.app.FragmentPagerAdapter; | |
| import android.support.v4.content.ContextCompat; | |
| import android.support.v4.view.ViewPager; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.support.v7.widget.Toolbar; | |
| import android.view.View; | |
| import android.view.animation.AccelerateInterpolator; | |
| import android.view.animation.Animation; | |
| import android.view.animation.AnimationSet; | |
| import android.view.animation.DecelerateInterpolator; | |
| import android.view.animation.RotateAnimation; | |
| import android.view.animation.ScaleAnimation; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Objects; | |
| public class MainActivity extends AppCompatActivity { | |
| public TabLayout tabLayout; | |
| private int[] tabIcons = { | |
| R.drawable.ic_tab_favourite, | |
| R.drawable.ic_tab_call, | |
| R.drawable.ic_tab_contacts | |
| }; | |
| int[] colorIntArray = {R.color.red,R.color.yellow,R.color.green}; | |
| int[] iconIntArray = {R.drawable.ic_tab_favourite, R.drawable.ic_tab_call, R.drawable.ic_tab_contacts}; | |
| @RequiresApi(api = Build.VERSION_CODES.KITKAT) | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| Toolbar toolbar = findViewById(R.id.action_bar); | |
| setSupportActionBar(toolbar); | |
| Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); | |
| FloatingActionButton fab = findViewById(R.id.fab); | |
| fab.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
| .setAction("Action", null).show(); | |
| } | |
| }); | |
| final ViewPager viewPager = findViewById(R.id.viewpager); | |
| setupViewPager(viewPager); | |
| 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) { | |
| } | |
| }); | |
| tabLayout = findViewById(R.id.tabs); | |
| tabLayout.setupWithViewPager(viewPager); | |
| setupTabIcons(); | |
| } | |
| private void setupTabIcons() { | |
| tabLayout.getTabAt(0).setIcon(tabIcons[0]); | |
| tabLayout.getTabAt(1).setIcon(tabIcons[1]); | |
| tabLayout.getTabAt(2).setIcon(tabIcons[2]); | |
| } | |
| private void setupViewPager(ViewPager viewPager) { | |
| ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); | |
| adapter.addFrag(new OneFragment(), "ONE"); | |
| adapter.addFrag(new TwoFragment(), "TWO"); | |
| adapter.addFrag(new ThreeFragment(), "THREE"); | |
| viewPager.setAdapter(adapter); | |
| } | |
| class ViewPagerAdapter extends FragmentPagerAdapter { | |
| private final List<Fragment> mFragmentList = new ArrayList<>(); | |
| private final List<String> mFragmentTitleList = new ArrayList<>(); | |
| public ViewPagerAdapter(FragmentManager manager) { | |
| super(manager); | |
| } | |
| @Override | |
| public Fragment getItem(int position) { | |
| return mFragmentList.get(position); | |
| } | |
| @Override | |
| public int getCount() { | |
| return mFragmentList.size(); | |
| } | |
| public void addFrag(Fragment fragment, String title) { | |
| mFragmentList.add(fragment); | |
| mFragmentTitleList.add(title); | |
| } | |
| @Override | |
| public CharSequence getPageTitle(int position) { | |
| return mFragmentTitleList.get(position); | |
| } | |
| } | |
| protected void animateFab(final int position) { | |
| final FloatingActionButton fab = findViewById(R.id.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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment