0

I have ViewPager with ListView below it in Linear Layout. Its working fine however when I am try to scroll only ListView scrolls while ViewPager remains static.

This is my XML code:

<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/pager"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:isScrollContainer="true" />

    <ListView
        android:id="@+id/categorylist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

I want ViewPager also to scroll and move upward rather than remaining static.

halfer
  • 19,824
  • 17
  • 99
  • 186
virendrao
  • 1,763
  • 2
  • 22
  • 42

2 Answers2

2

@vo12 Add View pager as the header of the list.

And for the error that you are getting.

  1. Remove view pager from that xml:

    <ListView
        android:id="@+id/categorylist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    

. Create a new View pager in code or inflate a different xml.

 <android.support.v4.view.ViewPager  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:isScrollContainer="true" />

And add the header view

   View headerView = getLayoutInflater().inflate(R.layout.header_view , null , false);
   listView.addHeaderView(headerView);

You were getting an error because your viewpager was attached to LinearLayout and so it's layoutparams were set accordingly but to add it to the list you either need to remove this view from linearlayout and add or add a new one.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • sure, what is your problem? – vipul mittal Jul 22 '14 at 11:48
  • I have activity which has ViewPager.. ViewPager has an adapter of type FragmentPagerAdapter. I only have one fragment because I want to change content and keep design same. So if there are 5 items i created five instances of same fragment. But there is some problem with data. I have many things in layout of fragment. I change action bar title based on fragment value received. However current fragment instance has previous instances value.. Could you help on this – virendrao Jul 22 '14 at 12:03
  • I think you should ask a different question and share more code – vipul mittal Jul 22 '14 at 12:05
  • created http://stackoverflow.com/questions/24887112/viewpager-with-fragmentpageadater-not-working-properly – virendrao Jul 22 '14 at 12:13
0

Try to add the ViewPager as a header of your listview.

gbero
  • 3,890
  • 1
  • 26
  • 30
  • I tried that only first: Got following error do you know what is problem: 07-22 15:19:57.603: E/AndroidRuntime(17489): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams 07-22 15:19:57.603: E/AndroidRuntime(17489): at android.widget.ListView.clearRecycledState(ListView.java:532) 07-22 15:19:57.603: E/AndroidRuntime(17489): at android.widget.ListView.resetList(ListView.java:518) – virendrao Jul 22 '14 at 09:50
  • Please make some research, here's a good tutorial. http://zeering.com/LoadAnswers.aspx?q=adding%20Viewpager%20as%20a%20header%20to%20a%20listView I'm nearly sure that it will fit your needs. – gbero Jul 22 '14 at 10:12