Swipe one item at a time Recyclerview

2019-01-17 16:12发布

I tried adding on Scroll listener for recycler view and made some logic but i am not able to swipe one item at a time. I did some search on internet but i got some third party library which has custom recycler view. Can we implement one item swipe at a time in recycler view? If yes Please tell how? One item swipe at a time like this image.

3条回答
萌系小妹纸
2楼-- · 2019-01-17 16:22

I know it's late but others might find this helpful. At that time I had no other choice so i used https://github.com/lsjwzh/RecyclerViewPager library. Recently I found we can use PagerSnapHelper which is in v7 support Library. Like :

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
查看更多
ら.Afraid
3楼-- · 2019-01-17 16:24

This is late, i know.

There is a very simple way to get exactly the requested scrolling behaviour with the use of a custom SnapHelper.

Create your own SnapHelper by overwriting the standard one (android.support.v7.widget.LinearSnapHelper).

public class SnapHelperOneByOne extends LinearSnapHelper{

    @Override
    public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY){

        if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
            return RecyclerView.NO_POSITION;
        }

        final View currentView = findSnapView(layoutManager);

        if( currentView == null ){
            return RecyclerView.NO_POSITION;
        }

        final int currentPosition = layoutManager.getPosition(currentView);

        if (currentPosition == RecyclerView.NO_POSITION) {
            return RecyclerView.NO_POSITION;
        }

        return currentPosition;
    }
}

This is basicly the stadard method, but without adding a jump counter which gets calculated by scroll speed.

If you swipe fast and long, the next(or previous) view will be centered (shown).

If you swipe slow and short, the current centered view stays centered after release.

I hope this answer can still help anybody.

查看更多
混吃等死
4楼-- · 2019-01-17 16:45

https://github.com/googlesamples/android-HorizontalPaging/

This has the link to something similar to what you have shown in the images. Let me know if there is something additional you are looking for, and I will link the relevant libraries.

Basically the difference between ViewPager and recyclerView is that, in recyclerView you are switching between many item, while in ViewPager you are switching between many fragments or independent pages itself.

I see you are using this https://github.com/lsjwzh/RecyclerViewPager, is there any particular use case you have in mind?

查看更多
登录 后发表回答