Android ViewPager 高度自適應(yīng)

ViewPager 是開發(fā)中經(jīng)常用到的一個控件,但高度卻不能根據(jù)內(nèi)容自適應(yīng),設(shè)置android:layout_height="wrap_content"之后還是占滿全屏??吹木W(wǎng)上的解決辦法主要有下面幾種:

  • 設(shè)置固定高度 android:layout_height="100dp"。在一定情況下這種方法是可以的,但在屏幕適配方面可能會有問題。
  • 在LinearLayout布局中使用weight改變ViewPager的高度。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="123456" />
</LinearLayout>

這種方法也可以調(diào)整ViewPager的高度,但其高度取決于其他控件,在一些情況下無法滿足需求。

  • 動態(tài)計算高度,通過LayoutParmas改變ViewPager的高度,或者在onMeasure中返回childView的高度。網(wǎng)上大部分的解決方法是通過下面的方法。
@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        //下面遍歷所有child的高度
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height) //采用最大的view的高度。
                height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
                MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

這種方式是通過計算ViewPager已經(jīng)加載的頁面中高度最大的值設(shè)置為ViewPager的高度,這種方法在ViewPager的每個頁面的高度都相等的情況下是非常實用的,能很好的解決ViewPager高度自適應(yīng)問題,但如果需求是ViewPager的每個頁面的高度都不太一致,用這種方式就會有些問題,從頁面高度大的滑動到頁面高度小的時,ViewPager的高度會不變,不能做到實時根據(jù)內(nèi)容的大小進(jìn)行調(diào)整。

  • 我對上面的方法進(jìn)行了一些調(diào)整,能做到ViewPager的高度實時。
public class ContentViewPager extends ViewPager {
    public ContentViewPager(Context context) {
        super(context);
    }
    public ContentViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int index = getCurrentItem();
        int height = 0;
        View v = ((Fragment) getAdapter().instantiateItem(this, index)).getView();
        if (v != null) {
            v.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            height = v.getMeasuredHeight();
        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

通過實時獲取ViewPager當(dāng)前顯示頁面的高度來調(diào)整ViewPager的高度,通過這種方式可是實現(xiàn)ViewPager的高度實時根據(jù)頁面的內(nèi)容來調(diào)整,但這種方式也有點問題,就是在ViewPager切換時能明顯看到頁面高度的變化,暫時沒找到好的方式來解決這個問題。
歡迎大家使用,如果有更好的方式也希望能分享出來。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容