Skip to content Skip to sidebar Skip to footer

Android Viewpager Dimension

Does the ViewPager have to be the only object present inside the activity layout? I'm trying to implement something like this:
<android.support.v4.view.ViewPager
    android:id="@+id/page_viewer"
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:layout_weight="1"/>

Solution 3:

I solved the problem with a couple of hacks. Here is what it involves:

First, I needed a layout that would ignore ViewPager's height limit. Used it as a parent layout for ViewPager items.

publicclassTallLinearLayoutextendsLinearLayout {

    publicTallLinearLayout(Context context) {
        super(context);
    }

    publicTallLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    @OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
    }
}

I then wrote the logic for resizing ViewPager:

privateclassViewPagerContentWrapperimplementsOnGlobalLayoutListener {
    private ViewPager mViewPager;

    publicViewPagerContentWrapper(ViewPager viewPager) {
        mViewPager = viewPager;
    }

    @OverridepublicvoidonGlobalLayout() {
        intposition= mViewPager.getCurrentItem(); 
        check(position);
        check(position + 1);
    }

    privatevoidcheck(int position) {
        ViewGroupvg= (ViewGroup) mViewPager.getChildAt(position); 
        Viewv= vg == null ? null : vg.getChildAt(0);

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

    privatevoidresize(int height) {
        mViewPager.setLayoutParams(
                newLinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        height
                )
        );
    }
}

Which I registered as global layout listener:

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

Post a Comment for "Android Viewpager Dimension"