android-Animate隐藏/显示工具栏在选项卡之间滑动时的显示/显示,例如在whatsapp相机中 [英] android-Animate the hiding/showing of toolbar on swiping between tabs like in whatsapp camera
问题描述
在tablayout中的各个选项卡之间滑动时,我成功显示和隐藏了appbar.但现在,我希望为标签的这种隐藏和出现动画化,就像我们在whatsapp中切换相机"标签时所做的那样.
i am successfull in showing and hiding of appbar when swiping between tabs in tablayout. but now i want to animate this hiding and appearing of tab smoothly like we do in switching for the camera tab in whatsapp.
推荐答案
解决方案是使用 onPageScrolled()
中的translationY属性来翻译 AppbarLayout
使用 AppbarLayout
的底部值回调 ViewPager
的 OnPageChangeListener
接口.
The way to go about it is to translate the AppbarLayout
using its translationY attribute in onPageScrolled()
callback of the ViewPager
's OnPageChangeListener
interface using the AppbarLayout
's bottom value.
mViewPager.addOnPageChangeListener(new OnPageChangeListener(){
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// get an instance of the appBarLayout This should probably be done at the activity level in its oncreate() method
/* example:
public ViewPager mViewPager;
public AppBarLayout appBar;
onCreate(...){
...
mViewPager = (ViewPager) findViewById(R.id.viewpager);
appBar = (AppBarLayout) findViewById(R.id.appbar);
mViewPager.addOnPageChangeListener(...);
...
}
*/
// set the position you want the app bar to be hidden on the ViewPager
int hideAppBarAtPosition = 0;
if (appBar != null) {
// get an instance of the bottom value of the appBar
float mBottom = (float) appBar.getBottom();
// going right offset changes from zero to one
if (position == hideAppBarAtPosition) {
// Translate the appBar up as the resideReveal slides into view
float y = (positionOffset * mBottom) - mBottom;
// Raise the appBar a little bit higher when it is no longer visible
if (y == -mBottom) {
float h = (float) appBar.getHeight();
if (mBottom < h) mBottom = h;
y = -mBottom - mBottom / 8f;
}
appBar.setTranslationY(y);
} else if (position == hideAppBarAtPosition - 1) {
// Translate the appBar up as the resideReveal slides into view
appBar.setTranslationY(-(positionOffset * mBottom));
} else if (appBar.getTranslationY() != 0f) {
// Reset translation of the appBar
appBar.setTranslationY(0f);
}
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
如果您需要更多有关如何实现它的想法,请查看此开源项目 https://github.com/goody-h/ResidingTab
If you need more ideas on how to implement it, view this open source project https://github.com/goody-h/ResidingTab
这篇关于android-Animate隐藏/显示工具栏在选项卡之间滑动时的显示/显示,例如在whatsapp相机中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!