Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Viewpager height inside Scrollview in android

I have to show a viewpager (An image and text below the image in pager row) inside a scrollview. I'm downloading the image, text from web and showing in pager rows. I wrapped viewpager inside a srollview to support the landscape mode too.

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

And here is CustomScrollView. The problem is How can I set the view pager height based on its children height so that I can scroll the screen till the view pager item ends only. If I set view pager height to wrapcontent, nothing is showing in viewpager. If I set some 800dp then I can see the pager items but there is unnecessary scrolling in the screen. I dont want my scrollview to be scrolled beyond the pager childrens height. Please help me.

like image 328
Gayathri Avatar asked Nov 15 '13 11:11

Gayathri


2 Answers

Try this way

<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> 
like image 118
Biraj Zalavadia Avatar answered Oct 13 '22 19:10

Biraj Zalavadia


To fix the height of the viewpager we can customize the viewpager class.

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

where this reMeasureCurrentPage should be called at viewpager onPageSelected callback.and CustomScrollView is the parent view.

like image 36
rKrishna Avatar answered Oct 13 '22 19:10

rKrishna