安卓ViewPager尺寸

7
< p >在活动布局中,ViewPager是否必须是唯一存在的对象?我正在尝试实现这样的功能:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reader_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/page_viewer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Gallery
    android:id="@+id/miniatures_gallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /></LinearLayout>

我在页面顶部有一个大的分页滚动条(已经存在),下面还有一个较小的图库滚动条。但是现在只显示了分页滚动条,没有显示图库滚动条。你有什么建议吗?


1
尝试这个:http://helpmeco.de/2012/1/android-viewpager-and-gallery-integration - browep
谢谢,这很棒...但是我在页面旋转时遇到了崩溃,有什么建议吗? - Lorenzo Sciuto
你有堆栈跟踪吗?你是从GitHub获取项目还是从教程中复制粘贴的? - browep
可能是 https://dev59.com/K2oy5IYBdhLWcg3wq_7O 的重复问题。 - Blackhex
更新的helpmeco.de链接:http://responsiveandroid.com/2012/01/26/android-viewpager-and-gallery-integration.html - browep
3个回答

6
ViewPager不支持wrap_content,因为它通常不会同时加载所有子项,所以无法获得适当的大小(选项是每次切换页面时更改页面大小)。
但是你可以设置精确的尺寸(例如150dp),也可以使用match_parent。 你还可以通过更改LayoutParams中的height属性从代码动态修改尺寸。

1
它有什么问题?另一个解决方案是,如果您希望分页器填充任何空白空间,则可以使用以下方法:<android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/>然后,分页器只留下足够的空间用于其他视图。 - Jave

5
将视图页面的布局权重分配为1,高度为0dp,而不是wrap_content。
<android.support.v4.view.ViewPager
    android:id="@+id/page_viewer"
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:layout_weight="1"/>

如何在RelativeLayout上实现? - corlaez

0

我用了几个hack解决了这个问题。以下是我的解决方案:

首先,我需要一个布局来忽略ViewPager的高度限制。将其用作ViewPager项目的父布局。

public class TallLinearLayout extends LinearLayout {

    public TallLinearLayout(Context context) {
        super(context);
    }

    public TallLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TallLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
    }
}

我随后编写了调整ViewPager大小的逻辑:
private class ViewPagerContentWrapper implements OnGlobalLayoutListener {
    private ViewPager mViewPager;

    public ViewPagerContentWrapper(ViewPager viewPager) {
        mViewPager = viewPager;
    }

    @Override
    public void onGlobalLayout() {
        int position = mViewPager.getCurrentItem(); 
        check(position);
        check(position + 1);
    }

    private void check(int position) {
        ViewGroup vg = (ViewGroup) mViewPager.getChildAt(position); 
        View v = vg == null ? null : vg.getChildAt(0);

        if (v != null) {
            int height = v.getHeight();
            if (height > mViewPager.getHeight()) {
                resize(height);
            }
        }
    }

    private void resize(int height) {
        mViewPager.setLayoutParams(
                new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        height
                )
        );
    }
}

我将其注册为全局布局监听器:

viewPager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewPagerContentWrapper(viewPager));

这将创建一个无限的布局循环。最初,系统请求 onLayout,它将调用您的 OnGlobalLayoutListener,然后调用 onLayout,接着又会调用 OnGlobalLayoutListener,如此往复... - Dmitriy Tarasov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接