如何设置动态高度以查看 android 中的寻呼机子片段?

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

我正在查看包含 4 个不同片段的寻呼机。其中,3 个片段仅包含 webview。所有片段加载不同的内容。我无法根据加载的内容设置高度。为此,我编写了一个自定义 viewpager 来测量 viewpager 的高度。对于第一个 viewpager 片段,高度计算正确。但是当导航到第二个或第三个片段时,viewpager 高度计算不准确。我不知道,我的代码中有什么问题,我需要更改什么才能在导航到该视图寻呼机页面后计算每个片段的正确高度。这是我的视图寻呼机和寻呼机适配器代码。请帮助我。

自定义视图寻呼机:

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];
}

终于找到答案了。在 xml 的 Webview 中放在 属性 下方。这将根据内容自动获取 webview 高度。

android:scrollbarStyle="insideOverlay"