【问题标题】:Add a sticky header to parallax scrolling - android为视差滚动添加粘性标题 - android
【发布时间】:2015-01-20 10:16:06
【问题描述】:
我想在我的应用程序中有一个视差滚动,就像带有“粘性”标题的 spotify 应用程序一样。这意味着标题将固定在屏幕顶部。我发现很多 ScrollView 库分别执行这些功能,但我找不到任何同时执行这两个功能的库。
我正在使用ParallaxScroll 库进行视差滚动,并使用StickyScrollViewItems 将项目粘贴到屏幕顶部。
非常感谢任何帮助。
【问题讨论】:
标签:
android
parallax
android-scrollview
sticky
【解决方案1】:
访问https://github.com/ksoichiro/Android-ObservableScrollView
如果你不想使用库,你可以从这里获取使标题保持粘性的逻辑:-
@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
if (dragging) {
int toolbarHeight = mToolbarView.getHeight();
if (firstScroll) {
float currentHeaderTranslationY = ViewHelper.getTranslationY(mHeaderView);
if (-toolbarHeight < currentHeaderTranslationY) {
mBaseTranslationY = scrollY;
}
}
float headerTranslationY = ScrollUtils.getFloat(-(scrollY - mBaseTranslationY), -toolbarHeight, 0);
ViewPropertyAnimator.animate(mHeaderView).cancel();
ViewHelper.setTranslationY(mHeaderView, headerTranslationY);
}
}
/// 这是使视图具有粘性的关键方法。
setTranslationY(float translationY)
设置此视图相对于其顶部位置的垂直位置。