Add swipe effiect using recyclerview like inshorts app

155 Views Asked by At

I am trying to implement the swipe effect like inshorts and way2news app, So far I can only find the solutions using viewpager and pageradapter. Those are working fine for me but I have to implement the same solution using recyclerview and recyclerview adapter.

Below is the code I have tried

Created a custom layoutmanager which is supposed to achieve the same result.

public class CustomLinearLayoutManager extends LinearLayoutManager {

private final float mShrinkAmount = 0.15f;
private final float mShrinkDistance = 1.0f;
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);

}

@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
    int orientation = getOrientation();
    if (orientation == VERTICAL) {

// below code is not giving the expected result

        int scrolled = super.scrollVerticallyBy(dy, recycler, state);
        float midpoint = getHeight() / 2.0f;
        float d0 = 0.0f;
        float d1 = mShrinkDistance * midpoint;
        float s0 = 1.0f;
        float s1 = 1.0f - mShrinkAmount; 
        // loop through active children and set scale of child
        System.out.println(getChildCount());
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);

            float childMidpoint = (getDecoratedBottom(child) + getDecoratedTop(child)) / 2.0f;
            float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
            float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
            child.setScaleX(scale);
            child.setScaleY(scale);
        }
        return scrolled;
    } else {
        return 0;
    }

}

}

setting the custom layoutmanager and added the PagerSnapHelper for fling/snapping effect in MainActivity

    adapter = new RecyclerViewAdapter(setData(),this);
    RecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new CustomLinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
    //recyclerView.addItemDecoration(new OverlapDecoration());
    PagerSnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(recyclerView);
    recyclerView.setAdapter(adapter);

Behaviour i want

this is the behaviour i want

Behaviour I am getting

enter image description here

What can I try next?

1

There are 1 best solutions below

0
On

You can try below library to get way2news app like effect

https://github.com/openaphid/android-flip