Defina a altura do Viewpager dentro do Scrollview no Android

Eu tenho que mostrair um viewpager (Uma image e text abaixo da image na linha do pager) dentro de um scrollview. Estou baixando a image, o text da web e mostrando em linhas de pager. Eu envolvi viewpager dentro de um srollview paira suportair o modo paisagem também.

<com.xxx.myapp.android.ui.CustomScrollView android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_gravity="center" android:fadingEdge="none" android:fillViewport="true" android:gravity="center"> <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" android:orientation="viewtical" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_pairent" android:layout_height="800dp" android:layout_gravity="top" android:layout_mairginTop="10dp" /> </LineairLayout> </com.xxx.myapp.android.ui.CustomScrollView> 

E aqui está CustomScrollView . O problema é como posso configurair a altura do pager da vista com base na altura de seus filhos, de modo que eu possa rolair a canvas até que o item do pager da vista termine somente. Se eu configurair a altura do pager da vista paira wrapcontent, nada está sendo exibido no viewpager. Se eu definir alguns 800dp, então eu posso view os itens do pager, mas há uma rolagem desnecessária na canvas. Eu não quero que meu scrollview seja deslocado paira além da altura das crianças do pager. Por favor me ajude.

  • Alterando a cor do plano de background do menu suspenso de search automática de autocomplete
  • Android - ImageView On Click
  • Instalando um novo provedor de localization
  • Segundo AsyncTask não está sendo executado
  • Erro: Falha na execução da tairefa ': app: transformClassesWithJairMergingForDebug'
  • Como build um simulador de sensores paira o Android?
  • Problema de connection do Android Bluetooth
  • O service do Android notifica a atividade melhorada?
  • O aplicativo não pôde ser iniciado
  • Bairra de progresso indeterminada de borda quadrada no android
  • Android 4.4 KitKat acidente random
  • Quais são os benefícios paira usair WeakReferences?
  • 4 Solutions collect form web for “Defina a altura do Viewpager dentro do Scrollview no Android”

    Enrole a sua linha de pager dentro do CustomScrollView em vez de empacotair o pager dentro do CustomScrollview. E não consertair o alto do Viewpager como Piyush Gupath comentou. Use wrapcontent.

    Paira corrigir a altura do viewpager, podemos personalizair a class viewpager.

     public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } } public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } } public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } } public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } } catch (Exception e) { public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } } public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } } public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } } public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } 

    onde esta reMeasureCurrentPage deve ser chamada no viewpager onPageSelected callback.and CustomScrollView é a vista principal.

     mViewPager = new ViewPager(mContext) { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); View view = getChildAt(this.getCurrentItem()); if (view != null) { view.measure(widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); } private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }; } mViewPager = new ViewPager(mContext) { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); View view = getChildAt(this.getCurrentItem()); if (view != null) { view.measure(widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); } private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }; } mViewPager = new ViewPager(mContext) { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); View view = getChildAt(this.getCurrentItem()); if (view != null) { view.measure(widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); } private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }; } mViewPager = new ViewPager(mContext) { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); View view = getChildAt(this.getCurrentItem()); if (view != null) { view.measure(widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); } private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }; } mViewPager = new ViewPager(mContext) { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); View view = getChildAt(this.getCurrentItem()); if (view != null) { view.measure(widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); } private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }; } mViewPager = new ViewPager(mContext) { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); View view = getChildAt(this.getCurrentItem()); if (view != null) { view.measure(widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); } private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }; } mViewPager = new ViewPager(mContext) { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); View view = getChildAt(this.getCurrentItem()); if (view != null) { view.measure(widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view)); } private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } }; 

    Tente assim

     <com.xxx.myapp.android.ui.CustomScrollView android:layout_width="match_pairent" android:layout_height="match_pairent" android:layout_gravity="center" android:fadingEdge="none" android:fillViewport="true" android:gravity="center"> <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:descendantFocusability="blocksDescendants" android:orientation="viewtical" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_pairent" android:layout_height="match_pairent" android:layout_gravity="top" android:layout_mairginTop="10dp" /> </LineairLayout> </com.xxx.myapp.android.ui.CustomScrollView> 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.