3

I am trying to implement a layout, which contains a list of ViewPagers. Each ViewPager is swipeable independently. See the link to the picture below.

picture of the layout

I tried with ScrollView and a LinearLayout with ViewPagers inside it, but I only get one ViewPager shown. Is it even possible to get several ViewPagers on one screen?

my code so far: main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager2"
            android:layout_width="wrap_content"
            android:layout_height="100dp" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager3"
            android:layout_width="wrap_content"
            android:layout_height="100dp" />
    </LinearLayout>

</ScrollView>

MainActivity

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ViewPagerAdapter adapter2 = new ViewPagerAdapter( this );
        ViewPager pager = (ViewPager)findViewById( R.id.viewpager2 );
        pager.setAdapter( adapter2 );

        ViewPagerAdapter adapter3 = new ViewPagerAdapter( this );
        ViewPager pager3 =
            (ViewPager)findViewById( R.id.viewpager3 );
        pager3.setAdapter( adapter3 );   
    }

}

Any ideas? Thanks! EDIT: this code actually works!

kiberNet
  • 155
  • 1
  • 7

3 Answers3

12

There is indeed something fishy with using multiple ViewPager. I am inflating several views, each holding a ViewPager, and only the first ViewPager got populated with data from its adapter. However, if I give each ViewPager an unique id, all of them gets populated.

I have a container whose only child is a ViewPager:

ViewPager pager = (ViewPager) pagerContainer.getChildAt(0);
         pager.setId(i);

where i is a positive index of a loop.

jayeffkay
  • 1,229
  • 1
  • 16
  • 22
  • thanks for your answer which helps me solving the problem that only one viewpager has its children view been added. i use mViewPager.setID(sViewPagerID++) to make the resource id unique , sViewPagerID is a static member of class references viewpager – gonglong May 28 '14 at 01:59
  • 1
    There is the possibility to call View.generateViewId() from API 17 ++ – luckyhandler Mar 17 '15 at 15:30
  • 2
    Absolutely the right answer for me. Was driving me mad for days, I just couldn't understand why the fragments for all the other viewpagers below the first weren't displaying! Thanks! – Daniel Julio Sep 02 '15 at 10:51
  • Wow, thanks, this really helped me, I think I'd never figure it out without this. Was adding viewpagers programatically on a view and they were not working, just the first one. After giving each one a specific ID it started to work like a charm! – Felipe Ribeiro R. Magalhaes Nov 03 '16 at 15:42
  • Thank you, worked to me too, but I had problems to i=0, I recommend start with i=1. – Ângelo Polotto Apr 10 '18 at 20:32
2

The issue is related to by default height of ViewPager as by default it is taking "fill_parent" , which we are not able to reset with "wrap_content". The solution which i found that fixing the height to some hard code value will return you all the view pager item appear in your screen.

I know this is some fishy solution but it works fine for me and i hope it should work with you.

Roll no1
  • 1,315
  • 1
  • 16
  • 23
-1

On your LinearLayout, in your XML. Set android:weightSum="2". For each viewpager, set android:layout_weight="1". Also, set the height of each child of the LinearLayout to 0dp. That should do the trick

wdziemia
  • 1,429
  • 1
  • 14
  • 19
  • The weightSum=2 and each layout_weight=1 didn't do the trick. When I set height of each child to 0dp they doesn't appaer at all. – kiberNet Jun 04 '12 at 06:32
  • Actually it was an error in the MainActivity. Used setAdapter on a wrong ViewPager :) Thanks for the tip with weightSum anyway! – kiberNet Jun 04 '12 at 07:17
  • @KiberNet: Hi I to have the same issue. and i am setting the adopter properly. and trying with wgightsum also. no progress. and in fact if i ViewPagers in Scrollview just like you did view pager itself not displayed. please help me if you have any idea. – Raj Mar 20 '13 at 10:27