scrollview与片段中的recyclerview

时间:2017-02-22 13:36:04

标签: android android-fragments android-recyclerview android-scrollview

这是我放在viewPager

中的片段
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_own_product_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/profile_bg"
tools:context="fyp.hkust.facet.fragment.OwnProductFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/ownproductlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:padding="5dp">

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


<com.melnykov.fab.FloatingActionButton
    android:id="@+id/add_product_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="16dp"
    android:elevation="6dp"
    android:src="@mipmap/ic_plus_white_36dp"
    fab:fab_colorNormal="@color/colorPrimary"
    fab:fab_colorPressed="@color/primary_pressed"
    fab:fab_colorRipple="@color/ripple"
    fab:fab_shadow="true" />

以下是活动

更新

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout     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:id="@+id/profile_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.MainActivity">


<!-- your content layout -->
<LinearLayout
    android:id="@+id/profile_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:elevation="0dp" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/profile_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <RelativeLayout
            android:id="@+id/activity_profile"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/profile_bg"
            tools:context="fyp.hkust.facet.activity.ProfileActivity">

            <RelativeLayout
                android:id="@+id/profile_layout"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:orientation="vertical"
                android:weightSum="1">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/profile_picture"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_marginBottom="25dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="25dp"
                    android:elevation="6dp"
                    android:src="@mipmap/app_icon" />

                <Button
                    android:id="@+id/btn_edit"
                    android:layout_width="60dp"
                    android:layout_height="35dp"
                    android:layout_alignBottom="@+id/profile_email"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="20dp"
                    android:adjustViewBounds="true"
                    android:background="@drawable/border_button_no_bg"
                    android:clickable="true"
                    android:contextClickable="true"
                    android:scaleType="fitCenter"
                    android:text="@string/edit_btn_text"
                    android:textColor="@color/font_color_pirmary" />

                <TextView
                    android:id="@+id/profile_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="47dp"
                    android:layout_toEndOf="@+id/profile_picture"
                    android:layout_toStartOf="@+id/btn_edit"
                    android:text="@string/profile_username"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/profile_email"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/profile_username"
                    android:layout_toEndOf="@+id/profile_picture"
                    android:layout_toStartOf="@+id/btn_edit"
                    android:paddingLeft="20dp"
                    android:paddingTop="10dp"
                    android:text="@string/profile_email"
                    android:textColor="@color/profile_email_text_color"
                    android:textSize="10sp" />
            </RelativeLayout>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/profile_layout"
                android:background="@color/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:tabGravity="fill"
                app:tabIndicatorColor="@color/white"
                app:tabMode="fixed"
                app:tabSelectedTextColor="@color/white"
                app:tabTextColor="@color/grey_title" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/tabs"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentStart="true"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>

如何让它滚动整个视图,而不是像在instragram一样只在回收器视图中滚动?谢谢。

2 个答案:

答案 0 :(得分:1)

使用NestedScrollview 而不是scrollview。

您可以根据需要启用/禁用recyclerview的嵌套滚动功能。

滚动视图不支持嵌套滚动。嵌套滚动视图将为您提供所需的内容。

您应该尝试遵循此层次结构

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="192dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            app:contentScrim="@color/action_bar"
            app:expandedTitleGravity="right"
            app:layout_scrollFlags="scroll|enterAlways">

            <RelativeLayout
                android:id="@+id/app_images_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="10dp"
                app:layout_collapseMode="parallax">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="below will be your elements" />

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/accent"
                    app:tabGravity="fill"
                    app:tabIndicatorHeight="2dp"
                    app:tabMode="fixed" />
            </RelativeLayout>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:0)

更改此项,在xml布局结束时关闭ScrollView。将它放在Relative布局下面。

     <?xml version="1.0" encoding="utf-8"?>
    <ScrollView 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:id="@+id/profile_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:id="@+id/activity_profile"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/profile_bg"
        tools:context="fyp.hkust.facet.activity.ProfileActivity">

        <RelativeLayout
            android:id="@+id/profile_layout"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:orientation="vertical"
            android:weightSum="1">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_picture"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginBottom="25dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="25dp"
                android:elevation="6dp"
                android:src="@mipmap/app_icon" />

            <Button
                android:id="@+id/btn_edit"
                android:layout_width="60dp"
                android:layout_height="35dp"
                android:layout_alignBottom="@+id/profile_email"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="20dp"
                android:adjustViewBounds="true"
                android:background="@drawable/border_button_no_bg"
                android:clickable="true"
                android:contextClickable="true"
                android:scaleType="fitCenter"
                android:text="@string/edit_btn_text"
                android:textColor="@color/font_color_pirmary" />

            <TextView
                android:id="@+id/profile_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="47dp"
                android:layout_toEndOf="@+id/profile_picture"
                android:layout_toStartOf="@+id/btn_edit"
                android:text="@string/profile_username"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/profile_email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/profile_username"
                android:layout_toEndOf="@+id/profile_picture"
                android:layout_toStartOf="@+id/btn_edit"
                android:paddingLeft="20dp"
                android:paddingTop="10dp"
                android:text="@string/profile_email"
                android:textColor="@color/profile_email_text_color"
                android:textSize="10sp" />
        </RelativeLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/profile_layout"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/white"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/grey_title" />

        <LinearLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tabs"
            android:orientation="vertical">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </LinearLayout>

    </RelativeLayout>
</ScrollView>