Viewpager does not load content after downloading images from the Internet

I have a viewpager that contains images uploaded by the Nostra Universal Image Loader. This viewpager is also part of the list as a header. Setting the height in wrap_content does not work, and it does not show anything, but if I set it to a specific failure, it works. My suspect is that the viewpager is getting to wrap its contents before loading images so that it doesn't show anything.

Here is the layout:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="16dp" android:layout_marginLeft="16dp" android:layout_marginTop="16dp"> <android.support.v4.view.ViewPager android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@id/viewpager" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <com.sblive.other.CirclePageIndicator android:id="@+id/titles" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginBottom="3dp" android:layout_gravity="center_horizontal|bottom" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerHorizontal="true"/> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/progressBar" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> 

Part of the PagerAdapter for ViewPager:

 @Override public Object instantiateItem(ViewGroup container, int position) { inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View itemView = inflater.inflate(R.layout.header_item, container,false); ImageView img = (ImageView) itemView.findViewById(R.id.imageView); ProgressBar progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar); display(img, list.get(position), progressBar); ((ViewPager) container).addView(itemView); return itemView; } 
+8
android android-layout android-viewpager
source share
1 answer

Views do not recalculate height (or width) when using "wrap_content" . Therefore, it is not updated when items are added dynamically. You can call it a limitation of Android.

You can recalculate the height and set it to the largest child by overriding the onMeasure method of your viewPager.

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int height = 0; 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) height = h; } heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 

You can set it equal to the height of the very first parent of the viewpager.

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { View child = getChildAt(0); child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int height = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 

Depending on what you are trying to achieve, you can also use layout_weight correctly to make the viewpager take up the remaining space in the parent.

0
source share

All Articles