AlvaroSantisteban
AlvaroSantisteban

Reputation: 5336

viewpager inside scrollview makes fragment not displayed when it should

I know that there loads of questions regarding the situation of a ViewPager inside a ScrollView, but I haven't seen one describing the situation that I am facing.

The thing is, when I don't have the ScrollView, my viewPager works correctly, as it can be seen in this pic. No scrollView

I also works if I place the ScrollView just for the elements below the ViewPager, but I don't want this behavior.

ScrollView not affecting viewPager

When I add the ScrollView I can scroll up & down and I can swipe to left and right too (to do so, I use this approach), but then the images are loaded but not displayed, as it can be seen in the following pic.

ScrollView with ViewPager inside

What I found out is that if, e.g. I have four images and I'm in the first position seeing the first image, I can swipe two times to the right (third position) and I won't see anything, but when I swipe one time back to the left (second position), I can then see the second image!

Not completely sure if its related, but the method getCount seems to go totally crazy and keeps being called non-stop.

XML

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/footer"
android:id="@+id/custom_scroll"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:scrollbarFadeDuration="0"
>   

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    >
    <com.-----.-----.WrapContentHeightViewPager
        android:id="@+id/imagePager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    >
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title" 
        android:textSize="15sp"
        android:paddingTop="15dp"
        android:paddingLeft="10dp" 
        android:paddingRight="10dp" />
    <TextView
        android:id="@+id/tags"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tags" 
        android:textColor="#ffff44"
        android:textSize="13sp"
        android:paddingLeft="10dp" 
        android:paddingRight="10dp" />

I tried using different kinds of custom ScrollViews but the problem persists. I´m testing it with a Nexus 4 with Android 4.4

Upvotes: 2

Views: 5451

Answers (2)

Dchaser88
Dchaser88

Reputation: 124

You can use this method to set width or height of elements in code,

      private static int GetActualDIPValue(Activity activity, int PX)
  {

        Resources r = activity.getResources();

        int actualDIP = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,PX, r.getDisplayMetrics());

        return actualDIP;
  }

Upvotes: 0

bogdan
bogdan

Reputation: 338

@AlvaroSantisteban Happy I could help. Struggled with this myself too. It all goes down to understanding how layouts work, more specifically understanding wrap_content and match_parent. So here's the original solution:

Your ViewPager should have a fixed height, considering it's inside a ScrollView.

Upvotes: 11

Related Questions