我正在尝试实现具有 3 层的嵌套 RecyclerView
。第一个 RecyclerView
(父) View 设置为垂直滚动。第二个 RecyclerView(第一个 subview )设置为水平滚动。第三个 RecyclerView
(第二个的 subview )也设置为水平滚动。
可视化:
-> RecyclerView
垂直滚动
--> RecyclerView
具有水平滚动
---> RecyclerView
具有水平滚动
现在,问题是我无法使第三个水平 RecyclerView 水平滚动。我认为问题是设备优先考虑第二个 RecyclerView
的水平滚动。
你能帮我解决这个问题吗?
这是第一个垂直滚动布局的 fragment 代码:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
这是第二个水平滚动的:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
这是最后一个水平滚动的:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
我已经尝试使用 onIntercept...()
拦截触摸,以在触摸第三个 RecyclerView
时取消第二个 RecyclerView
的滚动。
最佳答案
public class NestedScrollViewHome extends NestedScrollView {
@SuppressWarnings("unused")
private int slop;
@SuppressWarnings("unused")
private float mInitialMotionX;
@SuppressWarnings("unused")
private float mInitialMotionY;
public NestedScrollViewHome(Context context) {
super(context);
init(context);
}
private void init(Context context) {
ViewConfiguration config = ViewConfiguration.get(context);
slop = config.getScaledEdgeSlop();
}
public NestedScrollViewHome(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public NestedScrollViewHome(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);
}
}
}
关于java - 具有水平滚动的嵌套 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55902584/