Android 缩放时如何使RecyclerView的标题保持在顶部

Android 缩放时如何使RecyclerView的标题保持在顶部,android,android-recyclerview,Android,Android Recyclerview,我有一个我的RecyclerView(作为一个项目装饰)的标题,它在正常滚动时会粘贴在顶部,但是我的自定义RecyclerView也会缩放,但是在缩放标题时会将屏幕缩小。如何使用RecyclerView缩放标题,但将其固定在屏幕顶部 标题: public class RecyclerViewHeaderDecorator extends ZoomRecyclerView.ItemDecoration { private static final String TAG = RecyclerView

我有一个我的RecyclerView(作为一个项目装饰)的标题,它在正常滚动时会粘贴在顶部,但是我的自定义RecyclerView也会缩放,但是在缩放标题时会将屏幕缩小。如何使用RecyclerView缩放标题,但将其固定在屏幕顶部

标题:

public class RecyclerViewHeaderDecorator extends ZoomRecyclerView.ItemDecoration {
private static final String TAG = RecyclerViewDecorator.class.getSimpleName();
private View header;
private int layoutResId;

public RecyclerViewHeaderDecorator(int layoutResId) {
    this.layoutResId = layoutResId;
}

@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    header = LayoutInflater.from(parent.getContext()).inflate(layoutResId, parent, false);
    if (parent.getChildCount() > 0) {

        // Specs for parent (RecyclerView)
        int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
        int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED);

        // Specs for children (headers)
        int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, parent.getPaddingLeft() + parent.getPaddingRight(), header.getLayoutParams().width);
        int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, parent.getPaddingTop() + parent.getPaddingBottom(), header.getLayoutParams().height);

        header.measure(childWidthSpec, childHeightSpec);

        header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());

        canvas.save();
        header.draw(canvas);
        canvas.restore();
    }
}
}
回收视图:

public class ZoomRecyclerView extends RecyclerView {
private ScaleGestureDetector scaleGestureDetector;
private static final String TAG = ZoomRecyclerView.class.getSimpleName();

private float scaleFactor = 1.f;
private static final float minScale = 1.0f;
private static final float maxScale = 3.0f;

public ZoomRecyclerView(Context context, AttributeSet attrs) {
    super(context, attrs);
    scaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scaleFactor *= detector.getScaleFactor();

            //makes sure user does not zoom in or out past a certain amount
            scaleFactor = Math.max(minScale, Math.min(scaleFactor, maxScale));

            //refresh the view and compute the size of the view in the screen
            invalidate();

            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {

        }
    });
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);

    if (event.getPointerCount() > 1) {
        //notify the scaleGestureDetector that an event has happened
        scaleGestureDetector.onTouchEvent(event);
    }

    return true;
}

@Override
protected void dispatchDraw(@NonNull Canvas canvas) {
    //scales the display, centered on where the user is touching the display
    canvas.scale(scaleFactor, scaleFactor, scaleGestureDetector.getFocusX(), scaleGestureDetector.getFocusY());

    super.dispatchDraw(canvas);
}
}