Search code examples
androidandroid-fragmentsandroid-viewpagerfragmentpageradapter

How to set dynamic height to view pager child fragments in android?


I am having view pager with 4 different fragments. In this, 3 fragments only contains webview. All the fragments load different contents. I unable to set a height based on the loaded content. For that, I wrote a custom viewpager to measure height of viewpager. For the first viewpager fragment, height is calculated properly. But when navigate to second or third fragment, viewpager height is not calculated accurately. I don't know, what is the issue is in my code and what i need to change to calculate proper height for each fragment after navigate to that view pager page. Here is my view pager and pager adapter codes. Kindly help me.

Custom View pager:

public class CustomViewPager extends ViewPager {

public CustomViewPager (Context context) {
    super(context);
}

public CustomViewPager (Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;

    final View tab = getChildAt(0);
    int width = getMeasuredWidth();
    int tabHeight = tab.getMeasuredHeight();

    if (wrapHeight) {
        // Keep the current measured width.
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
    }

    int fragmentHeight = measureFragment(((Fragment) getAdapter().instantiateItem(this, getCurrentItem())).getView());
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(tabHeight + fragmentHeight + (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()), MeasureSpec.AT_MOST);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
public void setOnPageChangeListener(OnPageChangeListener listener) {
    requestLayout();
    invalidate();
    super.setOnPageChangeListener(listener);
}

@Override
public void addOnPageChangeListener(OnPageChangeListener listener) {
    super.addOnPageChangeListener(listener);

}

public int measureFragment(View view) {
    if (view == null)
        return 0;

    view.measure(0, 0);
    return view.getMeasuredHeight();
}
 }

PagerAdapter:

public class MerchantsCataAdapter extends FragmentPagerAdapter {
Context context;
Merchant merchant;
Fragment fragment;

private int mCurrentPosition = -1;

private String[] tabs = {"Home", "About Us","Promotions","Store Information"};

public MerchantsCataAdapter(FragmentManager fm, Context context,Merchant merchant) {
    super(fm);
    this.context = context;
    this.merchant = merchant;

}

@Override
public Fragment getItem(int index) {

    switch (index) {
        case 0:
            fragment= new HomeContentFragment().newInstance(merchant.getCompany_home_content(),merchant.getVideo_url());
            break;
        case 1:
            fragment= new AboutusFragment().newInstance(merchant.getFull_description(),merchant.getVideo_url());
            break;
        case 2:
            fragment= new AboutusFragment().newInstance(merchant.getCompany_promotions(),merchant.getVideo_url());
            break;
        case 3:
            fragment= new FragmentStoreInformation().newInstance(merchant.getMerchantId(),merchant.getName());
            break;

    }
    return fragment;
}



@Override
public int getCount() {
    return tabs.length;
}

public String getTabView(int position) {

    return tabs[position];
}

Solution

  • Finally, I find the answer. Place below property in Webview in xml. This will take webview height automatically based on the content.

    android:scrollbarStyle="insideOverlay"