我解決了幾個黑客的問題。這涉及到:
首先,我需要一個忽略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