Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Another solution is to update <code>ViewPager</code> height according to the current page height in its <code>PagerAdapter</code>. Assuming that your are creating your <code>ViewPager</code> pages this way:</p> <pre><code>@Override public Object instantiateItem(ViewGroup container, int position) { PageInfo item = mPages.get(position); item.mImageView = new CustomImageView(container.getContext()); item.mImageView.setImageDrawable(item.mDrawable); container.addView(item.mImageView, 0); return item; } </code></pre> <p>Where <code>mPages</code> is internal list of <code>PageInfo</code> structures dynamically added to the <code>PagerAdapter</code> and <code>CustomImageView</code> is just regular <code>ImageView</code> with overriden <code>onMeasure()</code> method that sets its height according to specified width and keeps image aspect ratio.</p> <p>You can force <code>ViewPager</code> height in <code>setPrimaryItem()</code> method:</p> <pre><code>@Override public void setPrimaryItem(ViewGroup container, int position, Object object) { super.setPrimaryItem(container, position, object); PageInfo item = (PageInfo) object; ViewPager pager = (ViewPager) container; int width = item.mImageView.getMeasuredWidth(); int height = item.mImageView.getMeasuredHeight(); pager.setLayoutParams(new FrameLayout.LayoutParams(width, Math.max(height, 1))); } </code></pre> <p>Note the <code>Math.max(height, 1)</code>. That fixes annoying bug that <code>ViewPager</code> does not update displayed page (shows it blank), when previous page has zero height (i. e. null drawable in the <code>CustomImageView</code>), each odd swipe back and forth between two pages.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload