2

I'm trying to create layout with two ViewPager inside ScrollView. First one has set height to desired size and second should match to rest of layout. Problem is that second ViewPager is not visible. What am I doing wrong? This is my layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

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

            <android.support.v4.view.ViewPager
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical" />
        </LinearLayout>
    </ScrollView>

</FrameLayout>

I'm using second ViewPager to show Fragments (using FragmentPagerAdapter), this is my FragmentPagerAdapter code:

public class MainPagerAdapter extends FragmentPagerAdapter {
    public MainPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new FirstFragment();
        case 1:
            return new SecondFragment();
        default:
            return new FirstFragment();
        }
    }

    @Override
    public int getCount() {
        return 2;
    }
}

Code of FirstFragment:

public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.first_step, container, false);
    }
}

first_step.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@android:color/holo_blue_light" />
    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@android:color/holo_green_light" />  
</LinearLayout>
user3626048
  • 706
  • 4
  • 19
  • 52

5 Answers5

0

Change

 <android.support.v4.view.ViewPager
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical" />

to

<android.support.v4.view.ViewPager
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:orientation="vertical" />

and also

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

to

   <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
Gordak
  • 2,060
  • 22
  • 32
  • Nothing changed, only first one is visible. – user3626048 Jul 10 '15 at 08:37
  • I edited my post. The problem is that you put a LinearLayout with height "match_parent" into a ScrollView. – Gordak Jul 10 '15 at 08:50
  • Thanks for your reply. When I set height of second ViewPager to some size then it's visible, but I want to match it to rest of layout. – user3626048 Jul 10 '15 at 08:56
  • The thing is, it can't. The "rest of the layout" is a scrollView, with infinite height. Asking the LinearLayout to match the parent height is the same as asking the linearlayout to be infinitely high, which is not possible. Remove the scrollView if you want the linear layout to take all the screen. – Gordak Jul 10 '15 at 09:02
0

if you are using layout weight. you have to set width or height layout 0dp accordingly to your use

this gives equal space to both pagers give weight in ratio i.e 2:1 or 2:1 etc

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

        <android.support.v4.view.ViewPager
            android:id="@+id/gallery"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <android.support.v4.view.ViewPager
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical" />
    </LinearLayout>
Abhishek
  • 2,350
  • 2
  • 21
  • 35
  • When I did that none of viewPagers is visible. – user3626048 Jul 10 '15 at 08:37
  • have to set images or your data to it..?.. can you show me your java code.. try to set background color in case you are checking.. – Abhishek Jul 10 '15 at 08:40
  • check this link.. similar ques.. http://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts http://stackoverflow.com/questions/21086449/designing-a-screen-with-multiple-viewpager-in-one-screen – Abhishek Jul 10 '15 at 09:02
0

you have to define

android:layout_height="200dp"

remove

 android:layout_weight="1"

after both viewpager is visible not to set android:layout_height="wrap_content"

Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
0

Set fillViewport=true in ScollView.

I think ScollView is useless, if the second ViewPager have not fixed height over screen.

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
hoi
  • 2,168
  • 20
  • 22
  • Thank you, now it looks ok but I can't scroll, do you know what's the problem? – user3626048 Jul 10 '15 at 09:34
  • Try to make second ViewPager's height higher, like 1000dp. – hoi Jul 10 '15 at 10:05
  • When I set height then it's scrollable but my Fragment in ViewPager may have different heights so I can't do that. – user3626048 Jul 10 '15 at 10:19
  • I don't know why you want to use scollview. If children of the scrollview are fit in screen then vertical scroll are useless. – hoi Jul 13 '15 at 07:52
  • But they aren't fit in screen, view are about 2000dp height – user3626048 Jul 13 '15 at 07:55
  • I think it's another problem. You should make custom viewpager for different height of viewpager. See this http://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content/18167273#18167273 – hoi Jul 13 '15 at 09:19
0
case ViewPager is  a puzzle view,you must set the height for it,then it can
appear in you UI Screen,gallery dont have this problem;
why use ScrollView in this layout, i dont understand.
but,if you use linearlayout

so you can do like this,background is just for test,and you should get destiny in your project to match screen, not just 200 *2:

layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <android.support.v4.view.ViewPager
                android:id="@+id/content1"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@android:color/darker_gray"
                android:orientation="vertical" />


    <android.support.v4.view.ViewPager
                android:id="@+id/content2"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="#ff0000"
                android:orientation="vertical" />
</LinearLayout>

activity:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpage01);
        int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        ViewPager content = (ViewPager) findViewById(R.id.content2);
        content.getLayoutParams().height = screenHeight - 200 * 2;
        content.requestLayout();
    }