2

I am having trouble creating and adding a view pager programmatically.

I have found this question Android create ViewPager programmatically but the accepted answer doesnt work for me.

Here is the relative code.

           ImagesPagerAdapter mImagePagerAdapter = new ImagesPagerAdapter(getActivity(), mProduct.getImages());
            ViewPager mImagePager = (ViewPager) inflater.inflate(R.layout.partial_view_pager, null);
            mImagePager.setAdapter(mImagePagerAdapter);
            rootView.add(mImagePager);

I have also tried modifying the second line to but no effect:

           ViewPager mImagePager = new ViewPager(getActivity());

The view pager just doesnt show up in the rendered layout. If i debug i can see that it instantiates the views but they just dont show. I have tried adding the id like suggested in the solution and also setting the id in layout partial_view_layout.

The only thing that seems to work is if i add the view pager through xml directly in the layout. This is not an option for me, since i want to be able to add multiple view pagers to the same layout.

Any ideas?

Community
  • 1
  • 1
JanBo
  • 2,925
  • 3
  • 23
  • 32
  • Check my answer for this post (http://stackoverflow.com/questions/28676491/how-to-dynamically-set-number-of-swipeable-tabs-in-action-bar-using-fragment/28679356#28679356) – Xcihnegn Mar 08 '15 at 22:01
  • I don see how this answer relates to my question? I dont have trouble with dynamic number of tabs within an view pager, i want to add many view pager's to the layout so i will have many rows of swipeable content. – JanBo Mar 09 '15 at 08:48
  • 2
    Have you tried setting an id (like `viewPager.setId(R.id.your_id)`) to viewpager? – waqaslam Oct 05 '15 at 12:11

1 Answers1

1

Your question is an important one, it applies not only to ViewPagers but actually to the generic case: "how to create a view dynamically?"

Three steps:

  • setup the View object
  • setup the LayoutParams (so that the view can tell the layout what size it wants to be etc.)
  • Add it to the View hierarchy where you want it to appear

For a ViewPager there are a few details that you are missing:

// this is the container for the ViewPager
FrameLayout mRootView;
mRootView = (FrameLayout) findViewById(R.id.root_layout);

ViewPager viewPager = new ViewPager(this);

viewPager.setId(View.generateViewId()); // --> this is important!

viewPager.setAdapter(new CustomViewPagerAdapter(getSupportFragmentManager(), this));

// --> Important! * 
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

viewPager.setLayoutParams(params);

// --> Important!
mRootView.addView(viewPager, viewPager.getLayoutParams());

One final note, depending on the Layout that you are using you will have to change the LayoutParams that you use:

ie.

RelativeLayout -> RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(...)

LinearLayout -> LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(...)

HenriqueMS
  • 3,864
  • 2
  • 30
  • 39