我正在制作一个与最近Android状态栏类似的布局。
容器内有两个视图:ViewPager
和 RecyclerView
。默认行为是当我滚动RecyclerView
时,我希望ViewPager
减小尺寸,反之亦然。
逻辑:
viewPagerMaxHeight = 200;
if scrollTop
is ViewPager.height > viewPagerMaxHeight?
YES: Prevent Scroll and Decrease ViewPager size apropriatry
No: Scroll RecyclerView
if scrollBottom
did we scroll to position 0?
YES: Start increasing ViewPager size
No: Scroll RecyclerView
注意事项:
- RecyclerView
包含不同大小的项目。
- 有时候会删除和添加项目。
- 这是一个简单的 RecyclerView,不像在通知中那样它们会相互折叠。
我可以自己构建大部分逻辑,但我无法为 RecyclerView
制作一个合适的监听器,让它返回滚动的方向和量。
防止 RecyclerView
滚动是一个额外的奖励。
编辑:
我已经在github上制作了一个示例。
v.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
Log.e("scrollY", ""+scrollY);
Log.e("oldScrollY", ""+oldScrollY);
Log.e("currentHeight", ""+currentHeight);
if(scrollY == 200) {
Log.e("==200", "JU");
} else if (scrollY < 200) {
Log.e("<200", ""+currentHeight);
if(currentHeight < fullHeight) {
Log.e("current<full", Integer.toString(deltaScroll));
deltaScroll = oldScrollY - scrollY;
currentHeight = currentHeight + deltaScroll;
if(currentHeight > fullHeight) {
currentHeight = fullHeight;
}
ku.getLayoutParams().height = currentHeight;
ku.requestLayout();
}
v.scrollTo(0, 200);
} else if (scrollY > oldScrollY) {
Log.e("Scroll DOWN", "" + Integer.toString(scrollY));
deltaScroll = scrollY - oldScrollY;
currentHeight = currentHeight - deltaScroll;
if(currentHeight > minHeight) {
ku.getLayoutParams().height = currentHeight;
ku.requestLayout();
v.scrollTo(0, 200);
} else {
currentHeight = minHeight;
ku.getLayoutParams().height = minHeight;
ku.requestLayout();
}
}
}
});
我正在为RecycleView
设置内边距,并将NestedScrollView
滚动到第一个项目,以便内边距不可见。这使我即使在已经到达顶部时也能向上滚动。
一切似乎都正常,但是您会注意到,当缓慢滚动时,滚动会出现“抖动”(如果滚动得足够快,则不会发生)。
我的猜测是因为NestedScrollView
本身会改变高度,例如向上滚动时也会发生向下滚动。