提问者:小点点

Android:垂直回收视图中的水平回收视图


这似乎是android中相当常见的模式。我正在尝试创建类似于Facebook显示广告的方式的东西:

您可以看到,它们有一个外部垂直回收器视图和一个内部水平回收器视图,作为外部垂直回收器视图适配器中的项目之一。

我遵循了Google关于“管理ViewGroup中的触摸事件”的指南-http://developer.android.com/training/gestures/viewgroup.html Recyclerview扩展了ViewGroup,并且那里的代码与我想做的类似。

我对它进行了一些定制,使它能够检测Y轴上的移动,而不是X轴上的移动,并将其应用于外部垂直循环视图。

/**
 * Created by Simon on 10/11/2015.
 *///This is the recyclerview that would allow all vertical scrolls
public class VerticallyScrollRecyclerView extends RecyclerView {


    public VerticallyScrollRecyclerView(Context context) {
        super(context);
    }

    public VerticallyScrollRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticallyScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    ViewConfiguration vc = ViewConfiguration.get(this.getContext());
    private int mTouchSlop = vc.getScaledTouchSlop();
    private boolean mIsScrolling;
    private float startY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);
        // Always handle the case of the touch gesture being complete.
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            // Release the scroll.
            mIsScrolling = false;
            startY = ev.getY();
            return false; // Do not intercept touch event, let the child handle it
        }
        switch (action) {
            case MotionEvent.ACTION_MOVE: {
                if (mIsScrolling) {
                    // We're currently scrolling, so yes, intercept the
                    // touch event!
                    return true;
                }

                // If the user has dragged her finger horizontally more than
                // the touch slop, start the scroll

                // left as an exercise for the reader
                final float yDiff = calculateDistanceY(ev.getY());
                Log.e("yDiff ", ""+yDiff);
                // Touch slop should be calculated using ViewConfiguration
                // constants.
                if (yDiff > mTouchSlop) {
                    // Start scrolling!
                    Log.e("Scroll", "we are scrolling vertically");
                    mIsScrolling = true;
                    return true;
                }
                break;
            }
        }
        return false;
    }

    private float calculateDistanceY(float endY) {
        return startY - endY;
    }

}

我的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_recyclerview_holder">

    <com.example.simon.customshapes.VerticallyScrollRecyclerView
        android:id="@+id/main_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
/>

</RelativeLayout>

当我尝试拖动内部水平循环视图(希望触摸事件被外部垂直循环视图拦截)时,外部循环视图根本不会垂直滚动。

它还给我一个错误说:

处理滚动时出错;找不到id -1的指针索引。有没有跳过任何运动事件?

有人知道如何让这个正常工作吗?


共1个答案

匿名用户

我遇到了类似的问题,但在网上找不到任何解决方案。在我的应用程序中,我有自定义交互式元素的列表,您可以与它们进行水平滑动交互(在这种情况下,滚动应该被锁定),但是当您垂直滑动时,它应该滚动。根据您的评论,您已经解决了您的问题,但是对于那些问题与我的问题相似并且他们的研究将他们带到这里的人,我将发布我的解决方案。我在RecyclerView的引擎盖下看了一下,这是我发现的。“处理滚动时出错;找不到 ID -1 的指针索引。有没有跳过任何 MotionEvents?“——问题是用于获取索引的变量 mScrollPointerIdACTION_DOWN发生时在 onTouchEvent 中设置。在我的特殊情况下,当ACTION_DOWN发生时,我从onInterceptTouchEvent返回false,因为当时我不知道天气用户想要滚动或与列表元素交互。在这种情况下,不会调用onTouchEvent。后来,当我发现用户想要与元素交互时,我从onInterceptTouchEvent返回false,并调用onTouchEvent,但它无法处理滚动,因为mScrollPointerId未设置。这里的解决方案只是在ACTION_DOWN发生时从onInterceptTouchEvent调用onTouchEvent

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    ...
    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            onTouchEvent(e);
            ...
            break;
            }
        ...
        }
    ...
}