如何在缩放时使RecyclerView的标题粘到顶部

时间:2017-07-28 01:56:28

标签: android android-recyclerview

我有一个我的RecyclerView标题(作为itemDecoration)在正常滚动时粘到顶部,但我的自定义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();
    }
}
}

RecyclerView:

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);
}
}

0 个答案:

没有答案
相关问题