在CoordinatorLayout

时间:2018-05-26 11:07:41

标签: android xml android-layout android-recyclerview android-coordinatorlayout

我的代码滚动问题..这是我的xml: -

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/colorThemeDark"
    android:fitsSystemWindows="true"
    android:stateListAnimator="@drawable/appbar_always_elevated"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:collapsedTitleGravity="left"
        app:contentScrim="@color/colorThemeDark"
        app:expandedTitleGravity="center|bottom"
        app:expandedTitleTextAppearance="@style/ExpandedAppBar"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/circleView"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_vertical|center_horizontal"
            android:src="@drawable/main_logo"
            app:layout_collapseMode="none" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

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

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#323642"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <RelativeLayout
        android:id="@+id/firstText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:paddingLeft="10dp">

        <TextView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="What you learn today?"
            android:textColor="@color/colorWhite"
            android:textStyle="bold" />
    </RelativeLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/catBoxes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/firstText"
        android:layout_marginTop="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />

    <RelativeLayout
        android:id="@+id/secondText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/catBoxes"
        android:paddingLeft="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Recommended for you"
            android:textColor="@color/colorWhite"
            android:textStyle="bold" />
    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/catMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/secondText"
        android:layout_marginTop="5dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" </android.support.v7.widget.RecyclerView>
</RelativeLayout></android.support.design.widget.CoordinatorLayout>

我在代码中的单个Activity.as中有两个不同的recyclelerView,我将根元素作为CoordinatorLayout。我在我的代码中添加了两个RecyclerView。当我滚动我的recyclerview时,它只滚动第一个recyclerView,第二个RecyclerView滚动到第一个recyclerview下面。但是当我向上滚动屏幕时,我想要同时回收RecyclerView滚动。

click this link for i make gif for my problem. Green boxes scrolled but i want to stop this green boxes scrolling

1 个答案:

答案 0 :(得分:0)

如果视图中有两个回收站视图,并且您希望它们使用所有空间,则需要使用NestedScrollView

您的代码如下:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#323642"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:id="@+id/firstText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:paddingLeft="10dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="What you learn today?"
                android:textColor="@color/colorWhite"
                android:textStyle="bold" />
        </RelativeLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/catBoxes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/firstText"
            android:layout_marginTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp" />

        <RelativeLayout
            android:id="@+id/secondText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/catBoxes"
            android:paddingLeft="10dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Recommended for you"
                android:textColor="@color/colorWhite"
                android:textStyle="bold" />
        </RelativeLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/catMenu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/secondText"
            android:layout_marginTop="5dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>

在您的代码中,在两个回收站视图中使用它:

LayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setAutoMeasureEnabled(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setNestedScrollingEnabled(false);

smbd uknow`s answer here

中的更多信息