问题描述
我有这样的布局:
<NestedScrollView>
<RecyclerView> // vertical recycler view
<RecyclerView/> // horizontal recycler view
<RecyclerView/>
<RecyclerView/>
...
<RecyclerView>
</NestedScrollView>
结果看起来像Google Play商店:
The result looks like Google play store:
我在水平回收商
视图中禁用了NestedScrolling:
And I disabled NestedScrolling in horizontal Recycler
view:
horizontalRecyclerView.setHasFixedSize(true);
horizontalRecyclerView.setNestedScrollingEnabled(false);
我的问题:
vertical recyclerview
不会滚动,每当 ACTION_UP
发生时, vertical recyclerview
也停止滚动。
My problem:
The vertical recyclerview
does not scroll fling, whenever ACTION_UP
happen, the vertical recyclerview
also stop scrolling.
如何在 nestedscrollview $中嵌套
vertical recyclerview
c $ c>和水平Recyclerview
在 vertical recyclerview
内,如Playstore并保持滚动顺畅。
How can I nest vertical recyclerview
inside nestedscrollview
, and horizontal recyclerview
inside vertical recyclerview
like Playstore and keep the scroll smooth.
使用@vrund purohit的自定义嵌套滚动视图(下面的代码),并禁用nestedscroll垂直和水平recyclelerview:
Using custom nested scroll view of @vrund purohit (code below), and disabled nestedscroll both vertical and horizontal recyclerview:
verticalRecyclerView.setNestedScrollingEnabled(false);
... add each horizontal recyclerviews:
horizontalRecyclerView.setNestedScrollingEnabled(false);
推荐答案
我有同样的问题,我解决了这个问题自定义 NeatedScrollView
。
I had this same problem and I solved this issue by customizing NeatedScrollView
.
以下是该类。
MyNestedScrollView
public class MyNestedScrollView extends NestedScrollView {
@SuppressWarnings("unused")
private int slop;
@SuppressWarnings("unused")
private float mInitialMotionX;
@SuppressWarnings("unused")
private float mInitialMotionY;
public MyNestedScrollView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
ViewConfiguration config = ViewConfiguration.get(context);
slop = config.getScaledEdgeSlop();
}
public MyNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyNestedScrollView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private float xDistance, yDistance, lastX, lastY;
@SuppressWarnings("unused")
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final float x = ev.getX();
final float y = ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
lastX = ev.getX();
lastY = ev.getY();
// This is very important line that fixes
computeScroll();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - lastX);
yDistance += Math.abs(curY - lastY);
lastX = curX;
lastY = curY;
if (xDistance > yDistance) {
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
public interface OnScrollChangedListener {
void onScrollChanged(NestedScrollView who, int l, int t, int oldl,
int oldt);
}
private OnScrollChangedListener mOnScrollChangedListener;
public void setOnScrollChangedListener(OnScrollChangedListener listener) {
mOnScrollChangedListener = listener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollChangedListener != null) {
mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
}
}
}
快乐编码。
这篇关于NestedScrollView里面没有使用Recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!