【发布时间】:2017-12-06 10:49:43
【问题描述】:
我正在做一个类似于最近 android 的状态栏的布局。
我有两个 Views 在容器内。 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 滚动到第一项,因此填充不可见。这让我即使已经在 TOP 也可以滚动 TOP。
一切似乎都正常,但是您会注意到滚动缓慢时滚动“跳跃”(如果滚动足够快就不会发生)。
我猜这是因为NestedScrollView 本身会改变高度,例如,在向上滚动时,向下滚动也会发生。
【问题讨论】:
-
混合使用 RecyclerView 和 NestedScrollView 通常会导致“跳跃”性能。您能否详细说明您使用 ViewPager 的目的是什么?如果可能的话,分享你的布局 XML....
-
查看协调器布局
-
@AbhishekSingh 有一个包含我所有观点的 github
标签: android android-recyclerview android-nestedscrollview nestedscrollview