2

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

Community
  • 1
  • 1
AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62
  • Your ViewPager should have a fixed height, considering it's inside a ScrollView – bogdan Mar 12 '14 at 13:19
  • Is that so? That would be terrible in my case, I would need to change several things :( – AlvaroSantisteban Mar 12 '14 at 14:12
  • @bogdan today I had time to try it and you were right. I have to make many changes in my code but it´s not related to the question. If you want to post an answer, I will mark it as accepted. Thanks! :) – AlvaroSantisteban Mar 21 '14 at 15:15
  • Check answer here: http://stackoverflow.com/questions/9034030/viewpager-in-scrollview/31440577#31440577 – Andrew Dmytrenko Jul 15 '15 at 20:39
  • @AlvaroSantisteban how to resolve this issue ? also facing same issue i have set viewpagers height to match_parent and also tried with wrap_content but nothing works – Erum Aug 25 '15 at 11:35

2 Answers2

11

@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.

bogdan
  • 338
  • 6
  • 10
  • I still dont get very well the whole thing, because one should not use fixed sizes, right? And also, in my case, I would like to have a height that its set on runtime and with this approach, I cant have it. Do you have any ideas or related code? – AlvaroSantisteban Mar 24 '14 at 10:15
  • Thank you so much! It's help me – Aleksandr Gorshkov Aug 23 '15 at 12:47
  • @bodgan how to resolve this issue ? also facing same issue i have set viewpagers height to match_parent and also tried with wrap_content but nothing works – Erum Aug 25 '15 at 11:35
  • @Erum Can I see your layout? – bogdan Aug 26 '15 at 05:45
  • @Erum the link is not valid anymore, sorry – bogdan Aug 28 '15 at 07:20
  • @bogdan pls check this http://pastie.org/10381119 oin this layout i have to put whole layout in scrollview so that viewpager 's fragment can scroll in whole page – Erum Aug 28 '15 at 09:54
0

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;
  }
Dchaser88
  • 124
  • 10