0

我正在尝试RecyclerView用 3 层实现嵌套的 s。第一个RecyclerView(父)视图设置为垂直滚动。第二个RecyclerView(第一个的子)视图设置为水平滚动。第三个RecyclerView(第二个的子)视图也设置为水平滚动。

可视化:

->RecyclerView垂直滚动
-->RecyclerView水平滚动
--->RecyclerView水平滚动

现在,问题是我不能让第三个水平 RecyclerView 水平滚动。我认为问题是设备优先考虑 2nd 的水平滚动RecyclerView

你能帮我解决这个问题吗?

这是第一个垂直滚动布局的代码片段:

<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

4

1 回答 1

0
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);
        }
    }
}
于 2019-04-29T11:44:44.073 回答