在android中的Scrollview中设置Viewpager高度

Gay*_*hri 9 android android-scrollview android-viewpager

我必须在scrollview中显示一个viewpager(在pager行中的图像下方的图像和文本).我正在从网上下载图像,文本并在寻呼机行中显示.我在srollview中包含了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> 
Run Code Online (Sandbox Code Playgroud)

这是CustomScrollView.问题是如何根据子高度设置视图寻呼机高度,以便我可以滚动屏幕直到视图寻呼机项目结束.如果我将view pager height设置为wrapcontent,则viewpager中不会显示任何内容.如果我设置了800dp,那么我可以看到寻呼机项目,但屏幕上有不必要的滚动.我不希望我的滚动视图滚动超出寻呼机儿童的高度.请帮我.

rKr*_*hna 24

要修复viewpager的高度,我们可以自定义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();
}
}
Run Code Online (Sandbox Code Playgroud)

这个reMeasureCurrentPage应该在viewpager上调用onPageSelected callback.and CustomScrollView是父视图.


kim*_*kim 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;
        }
    };
Run Code Online (Sandbox Code Playgroud)


Bir*_*dia 5

试试这个

<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> 
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,android:fillViewport =“ true”为我工作。 (3认同)

San*_*osh 0

将寻呼机行包裹在 CustomScrollView 内,而不是将寻呼机包裹在 CustomScrollview 内。并且不要像 Piyush Gupath 评论的那样修复 Viewpager 的高度。使用换行内容。