简体   繁体   English

CoordinatorLayout 中 RecyclerView 的滚动问题

[英]Scrolling issue with RecyclerView in CoordinatorLayout

I have issue with scrolling in my code .. here is my xml :-我在我的代码中滚动有问题.. 这是我的 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>

I have two different recyclerView in single Activity.as in my code , i take root element as CoordinatorLayout.我在我的代码中的单个 Activity.as 中有两个不同的 recyclerView,我将根元素作为 CoordinatorLayout。 i add two RecyclerView in my code.when i scroll my recyclerview it scroll only first recyclerView and second RecyclerView scroll below first recyclerview.我在我的代码中添加了两个 RecyclerView。当我滚动我的 recyclerview 时,它只滚动第一个 recyclerView,第二个 RecyclerView 滚动到第一个 recyclerview 下方。 but i want to both recyclerView scroll at same time when i scroll up my screen.但是当我向上滚动屏幕时,我想同时滚动 recyclerView。

Gif with my problem.我的问题的GIF。 Green boxes scrolled but i want to stop this green boxes scrolling绿色框滚动,但我想阻止这个绿色框滚动

When you have two recycler view in a view and you want them to use all the space, you need to use NestedScrollView .当您在一个视图中有两个回收器视图并且您希望它们使用所有空间时,您需要使用NestedScrollView

Your code will be the following:您的代码如下:

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

And in your code, use this in both recycler view:在您的代码中,在两个回收站视图中都使用它:

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

More info in smbd uknow`s answer here更多信息在 smbd uknow 的回答这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CoordinatorLayout中的Recyclerview-无休止的滚动问题 - Recyclerview inside CoordinatorLayout - Endless scrolling issue CoordinatorLayout、AppBarLayout 中的 RecyclerView 滚动问题 - RecyclerView inside CoordinatorLayout,AppBarLayout Scrolling issue CoordinatorLayout和RecyclerView问题 - CoordinatorLayout and RecyclerView issue CoordinatorLayout与NestedScrollView和Horizo​​ntal Scrolling RecyclerView - CoordinatorLayout with NestedScrollView and Horizontal Scrolling RecyclerView 在CoordinatorLayout的Viewpager中滚动RecyclerView和同级 - Scrolling RecyclerView and siblings in a Viewpager, in a CoordinatorLayout CoordinatorLayout内ViewPager内的CoordinatorLayout滚动问题 - CoordinatorLayout inside ViewPager inside CoordinatorLayout scrolling issue NestedScrollView和CoordinatorLayout。 滚动问题 - NestedScrollView and CoordinatorLayout. Issue on Scrolling 在 CoordinatorLayout 中禁用 AppBarLayout 时 RecyclerView 滚动故障? - RecyclerView scrolling glitches when disabling AppBarLayout in a CoordinatorLayout? 滚动时单击 CoordinatorLayout 中的 RecyclerView 不起作用 - Click not working on RecyclerView in CoordinatorLayout when scrolling 浓咖啡,当 NestedScrollView 或 RecyclerView 在 CoordinatorLayout 中时滚动不起作用 - Espresso, scrolling not working when NestedScrollView or RecyclerView is in CoordinatorLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM