这是输出..
示例代码https://github.com/msquare097/MFlex-toolbar,....
首先,很好的问题而且非常有趣...
并实施它。
我通过改变 viewpager 滚动来使用工具栏的最小高度。
首先,只需声明所有Toolbar 高度相对于您的活动中实现ViewPager 的片段。
我直接把它当作Integer。您从dimen 加载它作为DP 并将其转换为PX。
在我的 ViewPager 中,我拍摄了 4 个片段。所以声明 4 工具栏的高度。
int heightForPage0 = 150;
int heightForPage1 = 400;
int heightForPage2 = 300;
int heightForPage3 = 600;
设置好适配器后,只需添加监听器
mViewPager.addOnPageChangeListener(this);
并覆盖这三个方法并编写以下代码..
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.d(TAG,positionOffset+"");
int currentHeight;
int nextHeight;
switch (position) {
case 0:
currentHeight = heightForPage0;
nextHeight = heightForPage1;
calculateHeightAndApply(currentHeight, nextHeight, positionOffset);
break;
case 1:
currentHeight = heightForPage1;
nextHeight = heightForPage2;
calculateHeightAndApply(currentHeight, nextHeight, positionOffset);
break;
case 2:
currentHeight = heightForPage2;
nextHeight = heightForPage3;
calculateHeightAndApply(currentHeight, nextHeight, positionOffset);
break;
case 3:
// for last page we don't have to worry about it.
// bcoz there is no next page available;
break;
}
}
@Override
public void onPageSelected(int position) {}
@Override
public void onPageScrollStateChanged(int state) {}
这是我在onPageScrolled..中调用的神奇方法。
private void calculateHeightAndApply(int currentHeight, int nextHeight, float positionOffset) {
if (positionOffset==0) {
return;
}
int diff = nextHeight - currentHeight;
int newHeight = (int) ((positionOffset*diff));
mToolbar.setMinimumHeight(currentHeight+newHeight);
}