Set Viewpager Height Inside Scrollview In Android
I have to show a viewpager (An image and text below the image in pager row) inside a scrollview. I'm downloading the image, text from web and showing in pager rows. I wrapped viewp
Solution 1:
To fix the height of the viewpager we can customize the viewpager class.
publicclassWrapContentViewPagerextendsViewPager {
privateintmCurrentPagePosition=0;
publicWrapContentViewPager(Context context) {
super(context);
}
publicWrapContentViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
Viewchild= getChildAt(mCurrentPagePosition);
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
inth= child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
} catch (Exception e) {
e.printStackTrace();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
publicvoidreMeasureCurrentPage(int position) {
mCurrentPagePosition = position;
requestLayout();
}
}
where this reMeasureCurrentPage should be called at viewpager onPageSelected callback.and CustomScrollView is the parent view.
Solution 2:
mViewPager = newViewPager(mContext) {
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Viewview= getChildAt(this.getCurrentItem());
if (view != null) {
view.measure(widthMeasureSpec, heightMeasureSpec);
}
setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
}
privateintmeasureHeight(int measureSpec, View view) {
intresult=0;
intspecMode= MeasureSpec.getMode(measureSpec);
intspecSize= MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
// set the height from the base view if availableif (view != null) {
result = view.getMeasuredHeight();
}
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
};
Solution 3:
Try this way
<com.xxx.myapp.android.ui.CustomScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="center"android:fadingEdge="none"android:fillViewport="true"android:gravity="center"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:descendantFocusability="blocksDescendants"android:orientation="vertical" ><android.support.v4.view.ViewPagerandroid: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>
Solution 4:
This is the best and easy solution :
1) Inside your custom ViewPager Class -
overridefunonMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
var heightMeasureSpec = heightMeasureSpec
val mode = MeasureSpec.getMode(heightMeasureSpec)
// Unspecified means that the ViewPager is in a ScrollView WRAP_CONTENT.// At Most means that the ViewPager is not in a ScrollView WRAP_CONTENT.if (mode == MeasureSpec.UNSPECIFIED || mode == MeasureSpec.AT_MOST) {
// super has to be called in the beginning so the child views can be initialized.super.onMeasure(widthMeasureSpec, heightMeasureSpec)
var height = 0for (i in0 until childCount) {
val child = getChildAt(i)
child.measure(
widthMeasureSpec,
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
)
val h = child.measuredHeight
if (h > height) height = h
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
}
// super has to be called again so the new specs are treated as exact measurementssuper.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
2) While Setting the adapter to your ViewPager -
valviewPagerAdapter= ViewPagerAdapter(fragmentList, supportFragmentManager)
view_pager.adapter = viewPagerAdapter
view_pager.measure(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)`
view_pager.currentItem = 0
Solution 5:
Wrap your pager row inside CustomScrollView instead of wrapping the pager inside CustomScrollview. And Don't fix height of Viewpager as Piyush Gupath commented. Use wrapcontent.
Post a Comment for "Set Viewpager Height Inside Scrollview In Android"