简体   繁体   中英

Scroll view in Recycler View

How can i add scroll view in the xml file with having Relative layout, Recycler view, TextView, Image View. Below is the respective xml code :

Please go through the below code.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StoryList">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<ImageView
    android:id="@+id/skandaimagestory"
    android:src="@drawable/pictureskanda"
    android:layout_width="match_parent"
    android:layout_height="130dp" />
<TextView
    android:id="@+id/Heading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/skandaimagestory"
    android:gravity="center"
    android:padding="17dp"
    android:text="SURVIVORS TALES"
    android:textSize="20dp"
    android:textStyle="bold" />

<LinearLayout
    android:layout_below="@+id/Heading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:background="#f6facf"
        android:padding="10dp"
        android:id="@+id/story_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

</ScrollView>

Thank you in advance.

您可以使用android.support.v4.widget.NestedScrollView

Inside ScrollView layout there must have only one root layout. Notice that I have used LinearLayout as root layout. It could be also RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".StoryList">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageView
        android:id="@+id/skandaimagestory"
        android:src="@drawable/pictureskanda"
        android:layout_width="match_parent"
        android:layout_height="130dp" />
        <TextView
        android:id="@+id/Heading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/skandaimagestory"
        android:gravity="center"
        android:padding="17dp"
        android:text="SURVIVORS TALES"
        android:textSize="20dp"
        android:textStyle="bold" />

        <LinearLayout
        android:layout_below="@+id/Heading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:background="#f6facf"
            android:padding="10dp"
            android:id="@+id/story_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

         </LinearLayout>
      </LinearLayout>

    </ScrollView>

Just need to have Linear Layout (vertical) in ScrollView

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StoryList">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
     >
<ImageView
    android:id="@+id/skandaimagestory"
    android:src="@drawable/pictureskanda"
    android:layout_width="match_parent"
    android:layout_height="130dp" />
<TextView
    android:id="@+id/Heading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/skandaimagestory"
    android:gravity="center"
    android:padding="17dp"
    android:text="SURVIVORS TALES"
    android:textSize="20dp"
    android:textStyle="bold" />

<LinearLayout
    android:layout_below="@+id/Heading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:background="#f6facf"
        android:padding="10dp"
        android:id="@+id/story_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>
</LinearLayout>
</ScrollView>

You can use a NestedScrollView instead of your regular ScrollView like this

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/skandaimagestory"
        android:src="@drawable/pictureskanda"
        android:layout_width="match_parent"
        android:layout_height="130dp" />
    <TextView
        android:id="@+id/Heading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/skandaimagestory"
        android:gravity="center"
        android:padding="17dp"
        android:text="SURVIVORS TALES"
        android:textSize="20dp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_below="@+id/Heading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:background="#f6facf"
            android:padding="10dp"
            android:id="@+id/story_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

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

And then when initialising your RecyclerView just add these two lines:

recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM