Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager not working if set layout_height = wrap_content

Tags:

android

I got issues when worked with ViewPaper. It doesn't show anything when I set ViewPaper's height = wrap_content and it shows if I set height for ViewPager (ex: android:layout_height= "350dp"). Please help! Thanks!

This is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="#FF0000"
                android:gravity="center"
                android:text="Quick Links"
                android:textColor="@color/white"
                android:textSize="20sp" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

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

                <com.viewpagerindicator.CirclePageIndicator
                    android:id="@+id/circlePage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/pager"
                    android:layout_marginBottom="10dp" >
                </com.viewpagerindicator.CirclePageIndicator>
            </RelativeLayout>

            <com.custom.ExpandableHeightListView
                android:id="@+id/lvAdmissions"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:divider="@drawable/bg_listview_divider"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/selector_animation" >
            </com.custom.ExpandableHeightListView>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

Resolved: I follow this link and it help me resolved this issues. https://stackoverflow.com/a/24666987/1928560

like image 960
Phong NGUYEN Avatar asked Jun 17 '15 07:06

Phong NGUYEN


People also ask

Can I use ViewPager with views not with fragments?

yes...you can use View instead of Fragment in viewpager. Here you can Find Whole example that will help you to achieve Viewpager without Fragment. Go through this documentation. Save this answer.

Is ViewPager deprecated?

Called when the host view is attempting to determine if an item's position has changed. This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated.

Should I use ViewPager or ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

How do I refresh ViewPager fragments?

OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.


2 Answers

ViewPager height must either be specified match_parent or you must specify a value for it. Two solutions,

  1. Use weights concept inside LinearLayout or
  2. Use onMeasure method in your class file. Refer Android: I am unable to have ViewPager WRAP_CONTENT

This is how you can use weights in your LinearLayout

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#FF0000"
            android:gravity="center"
            android:text="Quick Links"
            android:textColor="@color/white"
            android:textSize="20sp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

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

            <com.viewpagerindicator.CirclePageIndicator
                android:id="@+id/circlePage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/pager"
                android:layout_marginBottom="10dp" >
            </com.viewpagerindicator.CirclePageIndicator>
        </RelativeLayout>

        <com.custom.ExpandableHeightListView
            android:id="@+id/lvAdmissions"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:divider="@drawable/bg_listview_divider"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/selector_animation" >
        </com.custom.ExpandableHeightListView>
    </LinearLayout>

This way your viewpager layout and ExpandableListView shares equal height. You can change the ratio by changing the weight value

like image 136
Zahid Avatar answered Sep 17 '22 19:09

Zahid


=>100% working when viewpager height set wrap_content.

public class FollowUseWrapHeightViewPager extends ViewPager {
public FollowUseWrapHeightViewPager(Context context) {
    super(context);
}

public FollowUseWrapHeightViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = 0;
    View view = null;
    for(int i = 0; i < getChildCount(); i++) {
        view = getChildAt(i);
        view.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = view.getMeasuredHeight();
        if(h > height) height = h;
    }

    if (height != 0) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
}

/**
 * Determines the height of this view
 *
 * @param measureSpec A measureSpec packed into an int
 * @param view the base view with already measured height
 *
 * @return The height of the view, honoring constraints from measureSpec
 */
private int measureHeight(int measureSpec, View view) {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        result = specSize;
    } else {
        // set the height from the base view if available
        if (view != null) {
            result = view.getMeasuredHeight();
        }
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return result;
}
}
like image 45
Hitesh Tarbundiya Avatar answered Sep 17 '22 19:09

Hitesh Tarbundiya