2013-11-15 19 views
7

Muszę pokazać viewpager (Obraz i tekst poniżej obrazu w wierszu pagera) wewnątrz przewijania. Pobierany jest obraz, tekst z sieci i wyświetlany w wierszach pagera. Zawinęłem viewpager wewnątrz widoku srollview, aby obsługiwać także tryb poziomy.Ustaw wysokość Viewpager wewnątrz Scrollview w Androidzie

<com.xxx.myapp.android.ui.CustomScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:fadingEdge="none" 
    android:fillViewport="true" 
    android:gravity="center"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:descendantFocusability="blocksDescendants" 
     android:orientation="vertical" > 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewPager" 
      android:layout_width="match_parent" 
      android:layout_height="800dp" 
      android:layout_gravity="top" 
      android:layout_marginTop="10dp" /> 
    </LinearLayout> 
</com.xxx.myapp.android.ui.CustomScrollView> 

A tutaj jest CustomScrollView. Problem polega na tym, w jaki sposób ustawić wysokość pagera widoku na podstawie jego wysokości podrzędnej, aby można było przewijać ekran, dopóki element pagera widoku nie zostanie zakończony. Jeśli ustawię wyświetlanie wysokości pagera na wrapcontent, nic nie będzie wyświetlane w viewpager. Jeśli ustawię jakieś 800dp, to zobaczę elementy pagerów, ale na ekranie nie ma potrzeby przewijania. Nie chcę, żeby mój przewijany widok był przewijany poza wysokość dzieci pagerów. Proszę pomóż mi.

+0

Nie podawaj wysokości poprawki w celu wyświetlenia pagera. – Piyush

+0

Czy rozwiązałeś? Wobec tego samego problemu –

+0

@Gayathri proszę udostępnić klasę com.xxx.myapp.android.ui.CustomScrollView –

Odpowiedz

-1

owinąć pagera wiersz wewnątrz CustomScrollView zamiast owijania pager wewnątrz CustomScrollview. I nie naprawiaj wysokości Viewpagera, jak skomentował Piyush Gupath. Użyj wrapcontent.

3

Spróbuj w ten sposób

<com.xxx.myapp.android.ui.CustomScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:fadingEdge="none" 
    android:fillViewport="true" 
    android:gravity="center"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:descendantFocusability="blocksDescendants" 
     android:orientation="vertical" > 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewPager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="top" 
      android:layout_marginTop="10dp" /> 
    </LinearLayout> 
</com.xxx.myapp.android.ui.CustomScrollView> 
+2

Dzięki, android: fillViewport = "true" pracował dla mnie. – Justin

9
mViewPager = new ViewPager(mContext) { 
     @Override 
     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; 
     } 
    }; 
+0

idealne! dzięki –

16

Aby ustalić wysokość podglądu, możemy dostosować klasę 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); 
} 

@Override 
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(); 
} 
} 

gdzie to reMeasureCurrentPage powinno zostać zwołane w viewpager onPageSelected callback.and CustomScrollView jest widok rodzica.

+1

Potrzebuję viewpager z wysokością aktualnie wyświetlanego dziecka. Jest to idealne rozwiązanie wśród wielu odpowiedzi na SOF. – Santhosh

+0

Idealny! Dziękuję Ci! – Madi

Powiązane problemy