[英]Scroll view in Recycler View
如何在具有相對布局,Recycler視圖,TextView,Image View的xml文件中添加滾動視圖。 以下是各自的xml代碼:
請檢查以下代碼。
<?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>
先感謝您。
您可以使用android.support.v4.widget.NestedScrollView
在ScrollView
布局內,必須只有一個根布局。 注意,我已經使用LinearLayout
作為根布局。 也可以是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>
只需在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>
您可以像這樣使用NestedScrollView
而不是常規的ScrollView
<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>
然后在初始化RecyclerView
只需添加以下兩行:
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.