9

I have RecyclerView with items ImageView. I want to make RecyclerView to display only one item at time, dragging to edge like ViewPager, when changing. Anyone knows how to achieve this?

I'm using code from this source:

Android Simple RecyclerView Widget Example

Only one difference is that I don't have TextView, only ImageView.

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
  • If you can add your code you have right now, this will help to do good answer that fit your question – Shudy May 19 '15 at 09:33
  • Well you can set your item view size to be equal to the screen size – M090009 May 19 '15 at 09:44
  • But the problem is that only one item must be displayed at time, one picture. And when I swipe, it must change next :/ – Damian Kozlak May 19 '15 at 09:49
  • Okay. so you need a vertical swiping exactly like a ViewPager? – Ahmed Hegazy May 19 '15 at 09:58
  • That's right, hegazy :) I tried use ViewPager with axis transformation, but the performance is not good. – Damian Kozlak May 19 '15 at 10:02
  • I'm using an already made VerticalViewPager that supports vertical swiping and the performance is realy good. I'm gonna write it in an answer. – Ahmed Hegazy May 19 '15 at 10:07
  • Did you try putting VerticalViewPager in standard ViewPager to achive both Horizonal and Vertial swiping? – Damian Kozlak May 19 '15 at 10:10
  • Oh. you meen like a grid? like a horizontal view pager in which every page is a vertical viewpager or reversed? – Ahmed Hegazy May 19 '15 at 10:12
  • Yes. I have _n_ elements in horizontal axis, each of _n_ got _x_ different photos - vertical axis (each _x_ is not constant for all _n_, some got 2, some 4). Yesterday I put VerticalViewPager in standard ViewPager, receiving 3 fragments and performance was bad, even on Note 3 :/ – Damian Kozlak May 19 '15 at 10:16
  • You won't believe me that this where I used the library that I mentioned! – Ahmed Hegazy May 19 '15 at 10:20
  • So should I use lib which you mentioned - castorflex/VerticalViewPager - and put this in standard ViewPager? – Damian Kozlak May 19 '15 at 10:25
  • that's what I'm doing and it works good for me. I don't in your case. what's is the problem with the performance. may be the images are large. may be your aren't using the ViewHolder pattern.... – Ahmed Hegazy May 19 '15 at 10:38

2 Answers2

46

You can use the PagerSnapHelper class:

RecyclerView recyclerFoodItemsHorizontal = (RecyclerView) findViewById(R.id.recycler_food_items_horizontal);
SnapHelper mSnapHelper = new PagerSnapHelper();
mSnapHelper.attachToRecyclerView(recyclerFoodItemsHorizontal);

It will work similarly to ViewPager.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Kshitij Jain
  • 549
  • 6
  • 12
1

As from I see you need a vertical ViewPager. I'm using castorflex/VerticalViewPager library for achieving that for one of my projects. It works just like a ViewPager, no code changes as I can see It's just a copy paste from the v19 ViewPager available in the support lib, where he changed all the left/right into top/bottom and X into Y.

Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
  • 1
    VerticalViewPager inside HorizontalViewPager works perfectly. Instead of FragmentPagerAdapter, I used now PagerAdapter in VerticalViewPager. Maximum 5% CPU usage on Note 3. Thank you for help :) – Damian Kozlak May 19 '15 at 13:24
  • Thanks for your feedback. I use it with FragmentStatePagerAdapter and it also works great ;) – Ahmed Hegazy May 19 '15 at 13:26
  • Currently (2020) there is already Google's official implementation of the same thing, they call it ViewPager2 and compared to ViewPager the ViewPager2 is capable of vertical scrolling (https://developer.android.com/training/animation/vp2-migration#vertical-support) – Michal Vician Apr 27 '20 at 17:23