2013-11-15 34 views
7

私はscrollviewの中でviewpager(ページャの行のイメージの下のイメージとテキスト)を表示する必要があります。私はイメージ、ウェブからテキストをダウンロードし、ページャの行に表示しています。風景モードをサポートするために、srollviewの中にビューページャーをラップしました。androidのScrollview内でViewpagerの高さを設定する

<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> 

そしてここにはCustomScrollViewです。問題は、ビューページャーの高さを子の高さに基づいて設定するにはどうすればよいのですか。ビューページャーの項目が終わるまで画面をスクロールできるのです。ビューページャの高さをwrapcontentに設定すると、ビューページに何も表示されません。 800dpを設定すると、ページャの項目が表示されますが、画面に不要なスクロールが表示されます。私は、スクロールビューをページャの子供の高さを越えてスクロールさせたくない。私を助けてください。

+0

表示ページャに固定高さを指定しないでください。 – Piyush

+0

解決しましたか?同じ問題に直面する –

+0

@Gayathri com.xxx.myapp.android.ui.CustomScrollViewのクラスを共有してください –

答えて

-1

を試してみてください。 Piyush Gupath氏がコメントしたように、Viewpagerの高さを固定しないでください。 wrapcontentを使用します。

3

はCustomScrollViewの内側にあなたのページ行をラップ代わりにCustomScrollview内側にポケットベルをラップこのよう

<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

ありがとう、アンドロイド:fillViewport = "true"は私のために働いた。 – 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

完璧!ありがとう –

16

ビューページの高さを固定するには、ビューページのクラスをカスタマイズできます。

このreMeasureCurrentPageがviewpager onPageSelected callback.and CustomScrollViewで呼ばれるべき
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(); 
} 
} 

は、親ビューです。

+1

現在表示されている子の高さを持つビューページャーが必要です。 これは、SOFに関する多くの回答の中の完璧な解決策です。 – Santhosh

+0

パーフェクト!ありがとうございました! – Madi

関連する問題