There is this layout. The area on which the Card are located. When swipe right-left, the following 2 items should be displayed. I do this with the ViewPager , but the problem is that the ViewPager places one item at a time in the visible area. How to solve this problem?

enter image description here

enter image description here

  • five
    place in the ViewPager 2 card element. - Vladyslav Matviienko
  • You need to make one ViewPager page which contains two CardView (and not one) and so for each page - pavlofff
  • I have a list of objects that should have 2 displayed in the viewPager, how to implement exactly that 2 elements should be taken from the sheet - kalugin1912
  • And did not you try using RecyclerView? - Artem

2 answers 2

I do not pretend to a complete solution, but it seems that such an idea should work.

All cards are placed in a horizontal LinearLayout , which is wrapped in a HorizontalScrollView :

 <HorizontalScrollView layout_width="match_parent" layout_height="wrap_content"> <LinearLayout orientation="horizontal" layout_width="wrap_content" layout_height="wrap_content"> <Card>…</Card> <Card>…</Card> … </LinearLayout> </HorizontalScrollView> 

This will give us the opportunity to scroll the cards horizontally.

Now you need to change the width of the cards so that two cards fit on the screen. For this you can, for example, inherit from Card and override onMeasure :

 public class MyCard extends Card { // … @Override protected void onMeasure(int widthMeasureSpec, final int heightMeasureSpec) { final View parentScrollView = ((View)(getParent().getParent())); if (parentScrollView != null) { if (parentScrollView instanceof HorizontalScrollView) { widthMeasureSpec = parentScrollView.getMeasuredWidth() / 2; } } setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); } } 

It remains to scroll to calculate the nearest position of the scroll, in which two cards will be displayed on the screen and manually scrolled through the HorizontalScrollView.fullScroll(…) .

    The adapter redefined the getCount method so that it returns half the size of your list.

     private class MyFragmentPagerAdapter extends FragmentPagerAdapter { public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return Fragment1.newInstance(position); } @Override public int getCount() { return (int) (myList.size()/2); } } 

    And in the fragment itself in the onCreateView method, from the resulting position, take two items from the list. Suppose you have page 3, respectively, take items 5 and 6 from the list.