与RecyclerView + AppBarLayout扔 [英] Flinging with RecyclerView + AppBarLayout
问题描述
我现在用的是新CoordinatorLayout与AppBarLayout和CollapsingToolbarLayout。下面AppBarLayout,我有内容列表的RecyclerView。
I am using the new CoordinatorLayout with AppBarLayout and CollapsingToolbarLayout. Below AppBarLayout, I have a RecyclerView with a list of content.
我已经验证一扔就RecyclerView滚动的作品,当我上下滚动列表。不过,我也想在AppBarLayout扩张过程中平滑滚动。
I have verified that fling scrolling works on the RecyclerView when I am scrolling up and down the list. However, I would also like the AppBarLayout to smoothly scroll during expansion.
当向上滚动扩大CollaspingToolbarLayout,立即停止滚动,一旦抬起手指离开屏幕。如果你在一个快速移动向上滚动,有时CollapsingToolbarLayout再崩溃也是如此。这与RecyclerView行为似乎功能很多不同的使用NestedScrollView时比。
When scrolling up to expand the CollaspingToolbarLayout, scrolling immediately stops once lifting your finger off the screen. If you scroll up in a quick motion, sometimes the CollapsingToolbarLayout re-collapses as well. This behavior with the RecyclerView seems to function much differently than when using a NestedScrollView.
我试图设置在recyclerview不同的滚动属性,但我一直没能想出解决办法。
I've tried to set different scroll properties on the recyclerview but I haven't been able to figure this out.
下面是一个视频显示了一些滚动的问题。 https://youtu.be/xMLKoJOsTAM
Here is a video showing some of the scrolling issues. https://youtu.be/xMLKoJOsTAM
下面是一个例子,显示了问题的RecyclerView(CheeseDetailActivity)。 <一href="https://github.com/tylerjroach/cheesesquare">https://github.com/tylerjroach/cheesesquare
Here is an example showing the issue with the RecyclerView (CheeseDetailActivity). https://github.com/tylerjroach/cheesesquare
下面是一个使用由克里斯·巴内斯一个NestedScrollView最初的例子。 https://github.com/chrisbanes/cheesesquare
Here is the original example that uses a NestedScrollView from Chris Banes. https://github.com/chrisbanes/cheesesquare
推荐答案
The answer of Kirill Boyarshinov was almost correct.
主要的问题是,RecyclerView有时是给一扔不正确的方向,因此,如果您添加以下code到他的回答能够正常工作:
The main problem is that the RecyclerView sometimes is giving incorrect fling direction, so if you add the following code to his answer it works correctly:
public final class FlingBehavior extends AppBarLayout.Behavior {
private static final int TOP_CHILD_FLING_THRESHOLD = 3;
private boolean isPositive;
public FlingBehavior() {
}
public FlingBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive) {
velocityY = velocityY * -1;
}
if (target instanceof RecyclerView && velocityY < 0) {
final RecyclerView recyclerView = (RecyclerView) target;
final View firstChild = recyclerView.getChildAt(0);
final int childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild);
consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD;
}
return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
isPositive = dy > 0;
}
}
我希望这会有所帮助。
I hope that this helps.
这篇关于与RecyclerView + AppBarLayout扔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!