Coder Social home page Coder Social logo

Comments (1)

vishalandroid123 avatar vishalandroid123 commented on May 26, 2024 1

`public class PickerLayoutManager extends LinearLayoutManager {

private float scaleDownBy = 0.66f;
private float scaleDownDistance = 0.9f;
private boolean changeAlpha = true;

private onScrollStopListener onScrollStopListener;

public PickerLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
}

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    super.onLayoutChildren(recycler, state);

    int orientation = getOrientation();
    if (orientation == HORIZONTAL) {
        scaleDownViewHorizontal();
    } else {
        scaleDownViewVertical();
    }
}

@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
    int orientation = getOrientation();
    if (orientation == HORIZONTAL) {
        int scrolled = super.scrollHorizontallyBy(dx, recycler, state);
        scaleDownViewHorizontal();
        return scrolled;
    } else return 0;
}

@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
    int orientation = getOrientation();
    if (orientation == VERTICAL) {
        int scrolled = super.scrollVerticallyBy(dy, recycler, state);
        scaleDownViewVertical();
        return scrolled;
    } else return 0;
}

private void scaleDownViewHorizontal() {
    float mid = getWidth() / 2.0f;
    float unitScaleDownDist = scaleDownDistance * mid;
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        float childMid = (getDecoratedLeft(child) + getDecoratedRight(child)) / 2.0f;
        float scale = 1.0f + (-1 * scaleDownBy) * (Math.min(unitScaleDownDist, Math.abs(mid - childMid))) / unitScaleDownDist;
        child.setScaleX(scale);
        child.setScaleY(scale);
        if (changeAlpha) {
            child.setAlpha(scale);
        }
    }
}

private void scaleDownViewVertical() {
    float mid = getHeight() / 2.0f;
    float unitScaleDownDist = scaleDownDistance * mid;
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        float childMid = (getDecoratedTop(child) + getDecoratedBottom(child)) / 2.0f;
        float scale = 1.0f + (-1 * scaleDownBy) * (Math.min(unitScaleDownDist, Math.abs(mid - childMid))) / unitScaleDownDist;
        child.setScaleX(scale);
        child.setScaleY(scale);
        if (changeAlpha) {
            child.setAlpha(scale);
        }
    }
}

@Override
public void onScrollStateChanged(int state) {
    super.onScrollStateChanged(state);
    if (state == 0) {
        if (onScrollStopListener != null) {
            int selected = 0;
            float lastHeight = 0f;
            for (int i = 0; i < getChildCount(); i++) {
                if (lastHeight < getChildAt(i).getScaleY()) {
                    lastHeight = getChildAt(i).getScaleY();
                    selected = i;
                }
            }
            onScrollStopListener.selectedView(getChildAt(selected));
        }
    }
}

public float getScaleDownBy() {
    return scaleDownBy;
}

public void setScaleDownBy(float scaleDownBy) {
    this.scaleDownBy = scaleDownBy;
}

public float getScaleDownDistance() {
    return scaleDownDistance;
}

public void setScaleDownDistance(float scaleDownDistance) {
    this.scaleDownDistance = scaleDownDistance;
}

public boolean isChangeAlpha() {
    return changeAlpha;
}

public void setChangeAlpha(boolean changeAlpha) {
    this.changeAlpha = changeAlpha;
}

public void setOnScrollStopListener(onScrollStopListener onScrollStopListener) {
    this.onScrollStopListener = onScrollStopListener;
}

public interface onScrollStopListener {
    public void selectedView(View view);
}

}
`

and change the layout file to
`

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:clipToPadding="false"
    android:paddingBottom="183dp"
    android:paddingTop="183dp" />

`

from horizontalpicker.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.