我解决了几个黑客的问题。这涉及到:
首先,我需要一个忽略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));
试试这个:http://helpmeco.de/2012/1/android-viewpager-and-gallery-integration – browep 2012-02-28 23:47:52
tnx这真是太好了......但是我在页面旋转时遇到了崩溃,对此有何建议? – 2012-02-29 10:15:57
你有堆栈跟踪吗?你从github上获得这个项目吗,还是从教程中复制粘贴? – browep 2012-02-29 22:56:52