Why latest version constraintLayout doesn't work in nestedScrollView with CoordinatorLayout

余生长醉 提交于 2020-01-05 05:42:24

问题


i have using coordinatorlayout with collapsingToolbar for parallax effect when scroll

and content view (text about page) bottom of collapsingToolbar in nestedScrollView.

my problem is constraint layout seems incomplete inside nestedScrollView

if i use LinearLayout in nestedScrollView content view everything is showing. but i using ConstraintLayout in nestedScrollView content view is seems incomplete as you can see below code

content view made with constraint layout

<androidx.constraintlayout.widget.ConstraintLayout 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/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_anchor="@+id/appBarLayout"
        app:layout_anchorGravity="bottom">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/coordinatorLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:collapsedTitleTextAppearance="@style/PrimaryTextField.NewsDetails.Expanded"
                    app:contentScrim="@color/transparentBlack"
                    app:expandedTitleGravity="bottom"
                    app:expandedTitleMarginBottom="@dimen/input_fields_margin"
                    app:expandedTitleMarginTop="@dimen/shrine_toolbar_offset_top"
                    app:expandedTitleTextAppearance="@style/PrimaryTextField.NewsDetails.Expanded"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                    app:titleEnabled="false">

                <androidx.appcompat.widget.AppCompatImageView
                        android:id="@+id/newsPhoto"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        app:layout_collapseMode="parallax"
                        app:layout_collapseParallaxMultiplier="0.7" />

                <androidx.appcompat.widget.Toolbar
                        android:id="@+id/news_detail_toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="@android:color/transparent"
                        android:theme="@style/ToolBarStyle.NewsDetails"
                        app:layout_collapseMode="pin"
                        app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
                        app:popupTheme="@style/AppTheme.PopupOverlay"
                        app:title=""
                        app:titleTextColor="@android:color/white" />
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.core.widget.NestedScrollView
                android:id="@+id/scrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:fitsSystemWindows="true"
                android:orientation="vertical"
                android:scrollbarSize="5dp"
                android:scrollbars="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/contentLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="400dp"
                    android:orientation="vertical">

                <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/newsTitle"
                        style="@style/PrimaryTextFieldBold"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/small_margin"
                        android:maxLines="3"
                        android:textSize="@dimen/primary_text_large"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        />

                <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/newsDate"
                        style="@style/PrimaryTextFieldBold"
                        android:layout_width="wrap_content"
                        android:layout_margin="@dimen/small_margin"
                        android:textSize="@dimen/primary_text_medium"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/newsTitle"
                        tools:text="08.08.2008 18:00" />

                <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/photoOwner"
                        style="@style/PrimaryTextFieldBold"
                        android:layout_margin="@dimen/small_margin"
                        android:layout_weight="1"
                        android:textSize="@dimen/primary_text_medium"
                        app:layout_constraintBottom_toBottomOf="@id/newsDate"
                        app:layout_constraintStart_toEndOf="@id/newsDate"
                        app:layout_constraintTop_toTopOf="@id/newsDate"
                        tools:text="Daily News" />


                <androidx.appcompat.widget.AppCompatImageView
                        android:id="@+id/showComments"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end"
                        android:padding="@dimen/small_margin"
                        android:src="@drawable/ic_comment"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/newsTitle" />


                <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/newsText"
                        style="@style/PrimaryTextField"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/small_margin"
                        android:paddingBottom="32dp"
                        android:textSize="@dimen/primary_text_medium"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/newsDate"
                />


            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

if i have using Linearlayout it is work.

<androidx.constraintlayout.widget.ConstraintLayout 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/mainLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_anchor="@+id/appBarLayout"
            app:layout_anchorGravity="bottom">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
                android:id="@+id/coordinatorLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.appbar.AppBarLayout
                    android:id="@+id/appBarLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent">

                <com.google.android.material.appbar.CollapsingToolbarLayout
                        android:id="@+id/collapsing_toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:collapsedTitleTextAppearance="@style/PrimaryTextField.NewsDetails.Expanded"
                        app:contentScrim="@color/transparentBlack"
                        app:expandedTitleGravity="bottom"
                        app:expandedTitleMarginBottom="@dimen/input_fields_margin"
                        app:expandedTitleMarginTop="@dimen/shrine_toolbar_offset_top"
                        app:expandedTitleTextAppearance="@style/PrimaryTextField.NewsDetails.Expanded"
                        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                        app:titleEnabled="false">

                    <androidx.appcompat.widget.AppCompatImageView
                            android:id="@+id/newsPhoto"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:adjustViewBounds="true"
                            android:scaleType="fitXY"
                            app:layout_collapseMode="parallax"
                            app:layout_collapseParallaxMultiplier="0.7" />

                    <androidx.appcompat.widget.Toolbar
                            android:id="@+id/news_detail_toolbar"
                            android:layout_width="match_parent"
                            android:layout_height="?attr/actionBarSize"
                            android:background="@android:color/transparent"
                            android:theme="@style/ToolBarStyle.NewsDetails"
                            app:layout_collapseMode="pin"
                            app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
                            app:popupTheme="@style/AppTheme.PopupOverlay"
                            app:title=""
                            app:titleTextColor="@android:color/white" />
                </com.google.android.material.appbar.CollapsingToolbarLayout>
            </com.google.android.material.appbar.AppBarLayout>

            <androidx.core.widget.NestedScrollView
                    android:id="@+id/scrollView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:fitsSystemWindows="true"
                    android:orientation="vertical"
                    android:scrollbarSize="5dp"
                    android:scrollbars="vertical"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <LinearLayout
                        android:id="@+id/contentLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:minHeight="400dp"
                        android:orientation="vertical">

                    <androidx.appcompat.widget.AppCompatTextView
                            android:id="@+id/newsTitle"
                            style="@style/PrimaryTextFieldBold"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/small_margin"
                            android:maxLines="3"
                            android:textSize="@dimen/primary_text_large"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />

                    <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                        <androidx.appcompat.widget.AppCompatTextView
                                android:id="@+id/newsDate"
                                style="@style/PrimaryTextFieldBold"
                                android:layout_width="wrap_content"
                                android:layout_margin="@dimen/small_margin"
                                android:textSize="@dimen/primary_text_medium"
                                tools:text="08.08.2008 18:00" />

                        <androidx.appcompat.widget.AppCompatTextView
                                android:id="@+id/photoOwner"
                                style="@style/PrimaryTextFieldBold"
                                android:layout_margin="@dimen/small_margin"
                                android:layout_weight="1"
                                android:textSize="@dimen/primary_text_medium"
                                tools:text="Daily News" />


                        <androidx.appcompat.widget.AppCompatImageView
                                android:id="@+id/showComments"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="end"
                                android:padding="@dimen/small_margin"
                                android:src="@drawable/ic_comment" />

                    </LinearLayout>


                    <androidx.appcompat.widget.AppCompatTextView
                            android:id="@+id/newsText"
                            style="@style/PrimaryTextField"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/small_margin"
                            android:paddingBottom="32dp"
                            android:textSize="@dimen/primary_text_medium"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toBottomOf="@id/newsDate"/>

                </LinearLayout>
            </androidx.core.widget.NestedScrollView>
        </androidx.coordinatorlayout.widget.CoordinatorLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

Constraint layout version "2.0.0-beta2"


回答1:


add this

implementation 'com.android.support.constraint:constraint-layout:1.1.3'



回答2:


If you are using Constraint"2.0.0-beta 2" then I would recommended you to try Motion Layout in your example. Motion Layout is a layout class that extends from Constraint Layout. Motion Layout has all the features of Constraint Layout.

you can refer this link to understand motion layout https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-1/



来源:https://stackoverflow.com/questions/57142959/why-latest-version-constraintlayout-doesnt-work-in-nestedscrollview-with-coordi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!