2015-08-25 113 views
0

我目前使用FAB并且后面的this用于滑动选项卡。我在切换标签时需要增加动画。由于切换选项卡时的动画FAB

动画/ simple_grow.xml

<scale xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromXScale="0.0" 
    android:fromYScale="0.0" 
    android:toXScale="1.0" 
    android:toYScale="1.0" 
    android:duration="700" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"> 
</scale> 

编辑: ViewPagerAdapter.java

package adapter; 

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

    CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created 
    int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created 

    // Build a Constructor and assign the passed Values to appropriate values in the class 
    public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) { 
     super(fm); 

     this.Titles = mTitles; 
     this.NumbOfTabs = mNumbOfTabsumb; 

    } 

    //This method return the fragment for the every position in the View Pager 
    @Override 
    public Fragment getItem(int position) { 

     if(position == 0) // if the position is 0 we are returning the First tab 
     { 
      FactorialTab factorialTab = new FactorialTab(); 

      return factorialTab; 

     } else if(position == 1)    // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab 
     { 
      PermutationTab permutationTab = new PermutationTab(); 
      return permutationTab; 
     } 
     else { 
      RandomTab randomTab = new RandomTab(); 
      return randomTab; 
     } 
    } 

    // This method return the titles for the Tabs in the Tab Strip 
    @Override 
    public CharSequence getPageTitle(int position) { 
     return Titles[position]; 
    } 

    // This method return the Number of tabs for the tabs Strip 
    @Override 
    public int getCount() { 
     return NumbOfTabs; 
    } 
} 

MainActivity.java

package activity; 

public class MainActivity extends AppCompatActivity { 

    Toolbar mToolbar; 

    ViewPager pager; 
    ViewPagerAdapter adapter; 
    SlidingTabLayout tabs; 
    CharSequence Titles[] = {"Factorial", "Permutation", "Random"}; 
    int Numboftabs = 3; 

    FloatingActionButton fab; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mToolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(mToolbar); 

     adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs); 

     pager = (ViewPager) findViewById(R.id.pager); 
     pager.setAdapter(adapter); 

     tabs = (SlidingTabLayout) findViewById(R.id.tabs); 
     tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width 

     tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
      @Override 
      public int getIndicatorColor(int position) { 
       return getResources().getColor(R.color.fab_pressed); 
      } 
     }); 

     tabs.setViewPager(pager); 

     fab = (FloatingActionButton) findViewById(R.id.fab);  
     fab.show(false); 

     Animation animation = AnimationUtils.loadAnimation(this, R.anim.simple_grow); 

     fab.startAnimation(animation); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

我很想知道如何从ViewPagerAdapter中知道我的位置。谢谢:D

回答

0
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     @Override 
     public void onPageSelected(int position) { 
      //show or hide your fab here 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 

     } 
    }); 
+0

你要我添加我的代码吗? –

+0

是的请 - 这将有帮助 – mstrengis

+0

添加代码:D –

相关问题