代码之家  ›  专栏  ›  技术社区  ›  Farid Arbai

当图像折叠时,NestedScrollView中的RecyclerView不会向上滚动

  •  2
  • Farid Arbai  · 技术社区  · 8 年前

    https://github.com/FaridArbai/TapExchange/tree/master/TapExchange

    我想寻求帮助,解决一个问题,我已经挣扎了大约两天,但仍然没有找到解决办法,尽管我已经做了彻底的研究。

    基本上,我有一个AppBarLayout,里面有一个可折叠的toolbarlayout,它的任务是当用户向下滚动CardViews的RecyclerView时折叠一个背景图像。当我试图在图像完全折叠后向下滚动RecyclerView时,问题就出现了:如果我触摸组成RecyclerView的卡片视图之一,它将不会滚动!

    这是没有折叠的外观(这里我可以毫无问题地滚动):

    Uncollapsed Image, able to scroll in any direction

    这是图像折叠后的外观(如果我的手指碰到组成RecyclerView的任何cardview,我将无法向下滚动):

    Collapsed Image, unable to scroll down if I touch one of the cardviews

    整个布局的代码:

        <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".profiles.PersonalProfile"
        android:background="#FCFCFC">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/personal_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/personal_collapsing"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                app:contentScrim="?attr/colorPrimary"
                app:title=""
                app:titleEnabled="false">
    
                <ImageView
                    android:id="@+id/personal_image_background"
                    android:layout_width="match_parent"
                    android:layout_height="340dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/profile_background"/>
    
                <android.support.v7.widget.Toolbar 
                    android:id="@+id/personal_toolbar"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:layout_collapseMode="pin"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:title="">
    
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
                        <Space
                            android:id="@+id/avatar_collapsed_target"
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:layout_marginLeft="0dp"/>
    
                        <TextView
                            android:id="@+id/personal_title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textSize="18dp"
                            android:layout_gravity="center"
                            android:layout_marginLeft="10dp"
                            android:text="Unknown Username"/>
                    </LinearLayout>
    
                </android.support.v7.widget.Toolbar>
    
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/section_selection_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@drawable/ic_action_add" />
    
    
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/personal_image_foreground"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginTop="120dp"
            android:layout_gravity="top|center_horizontal"
            android:src="@drawable/executive"
            app:collapsedTarget="@id/avatar_collapsed_target"
            app:layout_behavior="com.faridarbai.tapexchange.graphical.CollapsingAvatarBehavior"
            android:elevation="5dp"/>
    
        <LinearLayout
            android:id="@+id/personal_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_anchor="@id/personal_image_foreground"
            app:layout_anchorGravity="bottom|center"
            android:paddingTop="30dp">
    
            <TextView
                android:id="@+id/username_status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Unknown Username"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
        </LinearLayout>
    
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/personal_nested_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:fillViewport="true">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/personal_sections_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="0dp" />
    
        </android.support.v4.widget.NestedScrollView>
    
    </android.support.design.widget.CoordinatorLayout>
    

    我正在使用API 25构建,并使用25.4.0版的GUI实用程序。如果有人能帮忙,我将不胜感激。

    编辑

    我尝试使用SDK n 27和27.1.1构建工具,抑制了snap和NestedScrollView,但仍然遇到了同样的问题:一旦背景图像折叠,如果我最初将手指放在RecyclerView的CardView上,RecyclerView将无法向上滚动。这很奇怪,因为WhatsApp、Facebook和Telegram使用了非常相似的布局,它们没有这个问题,所以应该有一个公认的解决方案。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Islam Mansour    8 年前

    为什么在NestedScrollView中使用RecyclerView?!. 如果你有更多不同布局的项目,你可以使用不同视图类型的recyclerView,但我不建议在NestedScrollView中使用它,试着直接使用它,别忘了把它放在 app:layout_behavior="@string/appbar_scrolling_view_behavior"