在RecyclerView调整大小

时间:2017-06-08 12:16:13

标签: android android-recyclerview

我正在制作一个缩放到缩放的RecyclerView并且具有缩放缩放功能,但是一旦放大,onItemClickListeners就处于错误的位置。以下是修改后的RecyclerView的代码。我也做了simple demo that is hosted on GitHub。在尝试演示时,当您单击某个项目时,您应该获得一个Toast,其中会显示您单击的图标的编号。尝试不缩放,然后放大并再次尝试查看问题。我相信我需要通知RecyclerView需要更新onItemClickListener位置,但无法找到要覆盖的正确方法。谢谢你的帮助!

public class PinchRecyclerView extends RecyclerView {
    private static final int INVALID_POINTER_ID = -1;
    private int mActivePointerId = INVALID_POINTER_ID;
    private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1.f;
    private float maxWidth = 0.0f;
    private float maxHeight = 0.0f;
    private float mLastTouchX;
    private float mLastTouchY;
    private float mPosX;
    private float mPosY;
    private float width;
    private float height;


    public PinchRecyclerView(Context context) {
        super(context);
        if (!isInEditMode())
            mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
    }

    public PinchRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode())
            mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
    }

    public PinchRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (!isInEditMode())
            mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = View.MeasureSpec.getSize(widthMeasureSpec);
        height = View.MeasureSpec.getSize(heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent ev) {
        super.onTouchEvent(ev);
        final int action = ev.getAction();
        mScaleDetector.onTouchEvent(ev);
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                final float x = ev.getX();
                final float y = ev.getY();
                mLastTouchX = x;
                mLastTouchY = y;
                mActivePointerId = ev.getPointerId(0);
                break;
            }

            case MotionEvent.ACTION_MOVE: {
                final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK)
                        >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                final float x = ev.getX(pointerIndex);
                final float y = ev.getY(pointerIndex);
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;

                if (mPosX > 0.0f)
                    mPosX = 0.0f;
                else if (mPosX < maxWidth)
                    mPosX = maxWidth;

                if (mPosY > 0.0f)
                    mPosY = 0.0f;
                else if (mPosY < maxHeight)
                    mPosY = maxHeight;

                mLastTouchX = x;
                mLastTouchY = y;

                invalidate();
                break;
            }

            case MotionEvent.ACTION_UP: {
                mActivePointerId = INVALID_POINTER_ID;
                break;
            }

            case MotionEvent.ACTION_CANCEL: {
                mActivePointerId = INVALID_POINTER_ID;
                break;
            }

            case MotionEvent.ACTION_POINTER_UP: {
                final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                final int pointerId = ev.getPointerId(pointerIndex);
                if (pointerId == mActivePointerId) {
                    final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                    mLastTouchX = ev.getX(newPointerIndex);
                    mLastTouchY = ev.getY(newPointerIndex);
                    mActivePointerId = ev.getPointerId(newPointerIndex);
                }
                break;
            }
        }

        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.translate(mPosX, mPosY);
        canvas.scale(mScaleFactor, mScaleFactor);
        canvas.restore();
    }

    @Override
    protected void dispatchDraw(@NonNull Canvas canvas) {
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        if (mScaleFactor == 1.0f) {
            mPosX = 0.0f;
            mPosY = 0.0f;
        }
        canvas.translate(mPosX, mPosY);
        canvas.scale(mScaleFactor, mScaleFactor);
        super.dispatchDraw(canvas);
        canvas.restore();
        invalidate();
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();
            mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 3.0f));
            maxWidth = width - (width * mScaleFactor);
            maxHeight = height - (height * mScaleFactor);
            invalidate();
            return true;
        }
    }
}

无缩放,单击#34,工作 No Zoom, Clicked #34, Working

放大,点击#34,不工作 Zoomed In, Clicked #34, Not Working

3 个答案:

答案 0 :(得分:3)

<强>更新 我认为最好在你的几个课程中传播修改。 Here是仅PinchRecyclerView.java的链接,包含所需的所有更改。我会将整个项目保留在下面,以防其他用途。

您正在更改画布上的显示,而不是像azizbekian在评论中所述的基础视图。如果您不想使用自定义GridLayoutManager并继续使用您已启动的方法,则需要将屏幕触摸映射回您的基础视图。

这是您在GitHub上执行映射的updated project。以下是基本的变化:

  • PinchRecyclerView添加触控侦听器。

  • PinchRecyclerView上的触摸坐标转换为基础布局中的相应位置。

  • 通过搜索预期的项目视图进行识别并进行点击处理。

如果你尝试这个更新的应用程序,正如你在视频中看到的那样,触摸监听器也会在紧急情况下触发,这就是一个错误。

Video

答案 1 :(得分:0)

以下是recyclelerview如何运作的架构

适配器将数据提供给&gt; ViewHolder

ViewHolder将数据项findviewbyid存储在内存/保存位置,以防止每次滚动性能问题时查找视图。下一步...其

ViewHolder&gt;的LayoutManager

布局管理器存储每个项目的位置,并完成更新位置的工作以缩放,滚动等。

我认为解决此问题的方法是尝试调用

adapter.notifyDataSetChanged();

在检测到缩放活动时返回true的方法内部。不要在adaper类中这样做,请在缩放活动类中执行此操作。

这应该更新LayoutManager / ViewHolder并重新分配ID。

adapter.notifyItemRangeChanged(position, list.size());

告诉项目范围已更改

您可以在此处看到更多信息 How to update RecyclerView Adapter Data?

How do I make RecyclerView update its layout?

已回答

答案 2 :(得分:0)

为什么不在RecyclerView.ViewHolder中添加点击听众:

itemView.setOnClickListener(...);

正如azizbekian已经说过的那样,使用你自己的LayoutManager进行布局。