deorez
deorez

Reputation: 113

How to place Listview and Viewpager inside a ScrollView?

I have a listview and viewpager which I've used to generate my image-slider. These two have their own adapters. In my layout.xml I would like to put them into a scroll view but when I did viewpager doesn't scroll with listview. Could you please help me if you have an idea?

Thanks

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.test.MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/test">

            <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/pager_news"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <ListView
                android:id="@+id/news_lw"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:nestedScrollingEnabled="true">

            </ListView>
        </LinearLayout>

    </LinearLayout>

Upvotes: 0

Views: 476

Answers (2)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21766

Use NestedScrollView with RecyclerView instead of ListView.

Try this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/test">

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/news_lw"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/pager_news">

        </android.support.v7.widget.RecyclerView>
    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

Upvotes: 1

tynn
tynn

Reputation: 39863

ListView doesn't support nested scrolling. So instead use a NestedScrollView with a RecyclerView if you think this behavior is necessary.

Even better would be to use a CoordinatorLayout and define a custom behavior which scrolls the ViewPager in or out if the RecyclerView is scrolled a specific extend.

Upvotes: 1

Related Questions