【问题标题】:Scroll view in Recycler View回收站视图中的滚动视图
【发布时间】:2018-11-07 11:23:27
【问题描述】:

如何在 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>

提前谢谢你。

【问题讨论】:

  • 你能解释一下为什么需要一个 ScrollView 吗?比如你想达到什么目标?如果您需要它的唯一原因是在上方放置一些视图并使其一起滚动,您只需将 CoordinatorLayout 与 AppbarLayout 一起使用。更优雅的方式
  • 如果你试图获得一个元素列表,其中第一个是标题,我建议你只保留 RecyclerView 并使用不同的 ViewTypes。无论如何,您不应该使用“wrap_content”作为 ScrollView 和 RecyclerView 的高度。
  • 感谢您的帮助..

标签: android android-studio android-layout android-scrollview android-relativelayout


【解决方案1】:

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

【讨论】:

  • 感谢您的帮助。
【解决方案2】:

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>

【讨论】:

    【解决方案3】:

    只需要在ScrollView中有Linear Layout(垂直)

       <?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>
    

    【讨论】:

    • 非常感谢您的帮助。
    【解决方案4】:

    您可以像这样使用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);
    

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 2023-03-17
      相关资源
      最近更新 更多