-
-
Save egslava/589b82a6add9c816a007 to your computer and use it in GitHub Desktop.
package org.cnii.layoutloader.ui; | |
import android.content.Context; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.View; | |
/** | |
* Special thanks to Daniel López Lacalle for his response | |
* (http://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content/20784791#20784791) | |
* */ | |
public class WrapContentHeightViewPager extends ViewPager { | |
public WrapContentHeightViewPager(Context context) { | |
super(context); | |
} | |
public WrapContentHeightViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int height = 0; | |
for(int i = 0; i < getChildCount(); i++) { | |
View child = getChildAt(i); | |
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); | |
int h = child.getMeasuredHeight(); | |
if(h > height) height = h; | |
} | |
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} |
But it is taking the height of the biggest child. What I need is to dynamically set the height. please help.
// @afinas-em This is what i use to meaure the size dinamically. Sorry for the styling but i'm new using github
@OverRide
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
//View v = getChildAt(getCurrentItem());
TabsCircleAdapter tabsCircleAdapter = (TabsCircleAdapter) getAdapter();
//View v = tabsCircleAdapter.getCurrentItem(getCurrentItem());
View v = null;
if (tabsCircleAdapter != null) v = tabsCircleAdapter.getCurrentItem(getCurrentItem());
if(v != null) {
v.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
height = v.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} else {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
really save
This works and height is of tallest child, incase you need diffrent height then save height as interger array and request layout on page change listener, and populate height accordingly.
`public class WrapContentHeightViewPager extends ViewPager {
private int mCurrentPagePosition = 0;
private int[] heightArr = new int[3]; // allocating memory to array
public WrapContentHeightViewPager(Context context) {
super(context);
}
public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mode = MeasureSpec.getMode(heightMeasureSpec);
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);
int height = 0;
int numOfElements = getChildCount();
for (int i = 0; i < numOfElements; i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
height = h;
//if (h > height) height = h;
heightArr[i]=height;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightArr[mCurrentPagePosition], MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void reMeasureCurrentPage(int position) {
mCurrentPagePosition = position;
requestLayout();
}
}
`
This is for reference not orginal by me, credit goes to TheLittleNaruto and many devs from stack overflow.
This is what is used. Just add a flag of fillViewPort
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.v4.view.ViewPager
android:id="@+id/case_data_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
</ScrollView>
Some people are born to be awesome.!! Thank you.
how i can use ? :(
Thanks, you saved me !!!
I have the same problem with @ilya-afanasev, childCount
detected zero at first display, so it doesn't show until I tap the other tab. can anyone give advice?
Thanks a lot.