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

时间:2017-07-28 12:10:44

标签: android android-fragments android-viewpager fragmentpageradapter

我正在查看包含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];
}

1 个答案:

答案 0 :(得分:0)

最后,我找到了答案。在Webview中以xml放置在属性下面。这将根据内容自动获取webview高度。

android:scrollbarStyle="insideOverlay"