10

OK! I've been trying to do this on and of for the last few weeks and cant seem to find a good solution to how I can accomplish what I want. Been trying with HorizontalScrollView, Gallery (which is deprecated).

I am now looking into RecycleView because I came over this method .smoothScrollToPosition() and thought that might help me.

Here is an illustration of what I want to accomplish:

enter image description here

I also have to be able to create the relativelayouts programmatically

Can anyone point out to me how this can be accomplished? Is there any native way to do it?

EDIT: After looking further in to @CommonsWare suggestion about looking into ViewPager and the commonsware.com/blog/2012/08/20/ third post from Dave Smith I think this is the way to go. Havent modefied it to my use yet, but its looking good.

I am using Dave Smiths sample from his Github: https://gist.github.com/devunwired/8cbe094bb7a783e37ad1

Community
  • 1
  • 1
The Dude
  • 1,088
  • 7
  • 16
  • 30
  • 3
    `ViewPager` can be used for this, depending upon your objectives: https://commonsware.com/blog/2012/08/20/multiple-view-viewpager-options.html See in particular the third option, from Dave Smith, in that post. – CommonsWare May 21 '15 at 12:43
  • This might be exactly what I'm looking for. – The Dude May 21 '15 at 12:50

1 Answers1

4

Do not use that approach, it will use to much memory (if at some point you'll need to extend to a more generic solution) because you will have to keep an elevate number of ViewPager Pages (Fragment) in memory. I had this same challenge and I've accomplished this with HorizontalListView.

So you need to create an Adapter that will add "stubs" View's in order to get the first one or the last one (could be more depending on the desired visible views at the same time) centred in the screen since this is a ListView. For the snapping effect you can use something like this (all items need to have equal dimensions):

View.OnTouchListener hListViewsOnTouchListener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP && v instanceof HListView) {
                    View c = ((HListView) v).getChildAt(0);
                    int posToGo = Math.abs(c.getX()) < c.getWidth() / 2 ? ((HListView) v).getFirstVisiblePosition() : ((HListView) v).getFirstVisiblePosition() + 1;
                    ((HListView) v).smoothScrollToPositionFromLeft(posToGo, 0);
                }
                return false;
            }
        };

This approach not only uses less memory but it's generic regarding the number of child views, it's smoother and all the elements are clickable (not only the centred one)... well it's a ListView :)
Here's a screen shot of the final output example:
enter image description here enter image description here enter image description here

Take care.

GuilhE
  • 11,591
  • 16
  • 75
  • 116
  • You dont happend to have som samplecode I could use? – The Dude May 21 '15 at 13:48
  • Btw. The link you provided to HorizontalListView is now deprecated – The Dude May 21 '15 at 14:40
  • It's deprecated because with API >= 5.0 Lolipop, the RecycleView can be horizontal. https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html A RecyclerView.LayoutManager implementation which provides similar functionality to ListView. – GuilhE May 21 '15 at 14:47
  • But the main concern point here is that you can achieve this by just using a simple ListView (horizontal), its BaseAdapter (with that "stubs" logic) and a View.OnTouchListener :) – GuilhE May 21 '15 at 14:59
  • Is there any tutorial out there on how to use the library? – The Dude May 21 '15 at 15:27
  • HorizontalListView or RecycleView? ListView will not be deprecated so you can use HorizontalListView with no worries, the author wont continue to give support just that. You can start using RecycleView (I haven't use yet) but until Google says so ListView it's not deprecated so HorizontalListView will be functional. HorizontalListView extends ListView so add that dependency and instead of creating ListView create HListView just that ;) – GuilhE May 21 '15 at 16:26
  • 1
    This question already answered by myself with demo video clip and alogrithm. I used recyclerView as you need. Have a look at this link below: http://stackoverflow.com/a/34647005/4201474 – TranHieu Mar 31 '16 at 10:06