Setting view height in Scrollview window in android

I need to show the viewpager (image and text below the image in the pager line) inside the scrollview. I download the image, text from the Internet and show in the lines of the pager. I wrapped the viewpager inside a srollview to support landscape mode.

<com.xxx.myapp.android.ui.CustomScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:fadingEdge="none" android:fillViewport="true" android:gravity="center"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="800dp" android:layout_gravity="top" android:layout_marginTop="10dp" /> </LinearLayout> </com.xxx.myapp.android.ui.CustomScrollView> 

And here is the CustomScrollView . The problem is how to adjust the height of the presentation pager based on the height of its children so that I can scroll the screen until there is only a pager object. If I set the pager presentation height in wrapcontent, nothing is displayed in the viewpager. If I set 800dp, then I will see the pager elements, but there is no need to scroll on the screen. I do not want the scroll to be scrolled beyond the height of the pager. Please help me.

+9
android android-viewpager android-scrollview
source share
4 answers

Wrap your pager line inside CustomScrollView instead of wrapping your pager inside CustomScrollview. And do not fix the height of the Viewpager, as Piyush Gupat commented. Use wrapcontent.

-one
source share

To fix the height of the viewpager, we can customize the viewpager class.

 public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } 

where this reMeasureCurrentPage should be called in the viewpager onPageSelected callback.and CustomScrollView is the parent view.

+24
source share
 mViewPager = new ViewPager(mContext) { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); View view = getChildAt(this.getCurrentItem()); if (view != null) { view.measure(widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); } private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }; 
+9
source share

Try this way

 <com.xxx.myapp.android.ui.CustomScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:fadingEdge="none" android:fillViewport="true" android:gravity="center"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="top" android:layout_marginTop="10dp" /> </LinearLayout> </com.xxx.myapp.android.ui.CustomScrollView> 
+4
source share

All Articles