0

我有4個不同的片段查看尋呼機。在這裏,3個片段只包含webview。所有的片段加載不同的內容。我無法根據加載的內容設置高度。爲此,我編寫了一個自定義viewpager來測量viewpager的高度。對於第一個viewpager片段,高度計算正確。但是,當導航到第二個或第三個片段時,viewpager高度計算不準確。我不知道,我的代碼中存在什麼問題,以及需要更改哪些內容以便在導航到該視圖頁面頁面之後計算每個片段的適當高度。這是我的查看尋呼機和尋呼機適配器代碼。請幫助我。如何設置動態高度以查看android中的尋呼機子片段?

自定義視圖尋呼機:

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

按照此[GIT中轂動態視圖尋呼機鏈接](https://github.com/vabhishek/WrapContentViewPagerDemo/tree/master/DynamicViewPager-master),可能幫助你 –

+0

@NileshRathod,我想這一點,但它沒有幫助。它不工作。 – Janaki

回答

0

最後,我找到了答案。將下面的屬性放在Webview的xml中。這將根據內容自動採用webview高度。

android:scrollbarStyle="insideOverlay" 
相關問題