【问题标题】:How to let NestedScrollView scroll in MotionLayout?如何让 NestedScrollView 在 MotionLayout 中滚动?
【发布时间】:2023-03-05 05:40:01
【问题描述】:

一旦运动场景结束,我在 MotionLayout 中有一个 NestedScrollView,然后 NestedScrollView 可见,但是当我尝试滚动视图时,没有任何事件会转到 NestedScrollView。

Video of Requirement

布局xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/motion_scene"
tools:context=".screens.moviedetail.controller.MovieDetailFragment">

<ImageView
    android:id="@+id/movieDetailPosterImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/poster_image"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_image_24px" />

<TextView
    android:id="@+id/movieDetailMovieTitleTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/movieTitleBackground"
    android:fontFamily="sans-serif-smallcaps"
    android:gravity="center_vertical"
    android:padding="@dimen/commonPadding"
    android:text="@string/movie_title"
    android:textColor="@android:color/white"
    android:textSize="@dimen/commonTextSize" />

<androidx.core.widget.NestedScrollView
    android:id="@+id/detailScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        android:orientation="vertical">

        <TextView
            android:id="@+id/movieDetailMovieOverviewHeadTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@string/movie_detail_heading_font"
            android:gravity="center_vertical"
            android:paddingStart="@dimen/commonPadding"
            android:paddingLeft="@dimen/commonPadding"
            android:paddingTop="@dimen/commonPadding"
            android:paddingEnd="@dimen/commonPadding"
            android:paddingRight="@dimen/commonPadding"
            android:text="@string/overview"
            android:textColor="@color/headTextColor"
            android:textSize="@dimen/commonTextSize" />

        <TextView
            android:id="@+id/movieDetailMovieOverviewTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:fontFamily="@string/movie_detail_heading_font"
            android:gravity="center_vertical"
            android:maxLines="@integer/overviewMaxLength"
            android:paddingStart="@dimen/commonPadding"
            android:paddingLeft="@dimen/commonPadding"
            android:paddingEnd="@dimen/commonPadding"
            android:paddingRight="@dimen/commonPadding"
            android:paddingBottom="@dimen/commonPadding"
            android:textColor="@color/textColor"
            android:textSize="@dimen/commonTextSize" />

        <TextView
            android:id="@+id/movieDetailMovieRatingTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@string/movie_detail_heading_font"
            android:gravity="center_vertical"
            android:padding="@dimen/commonPadding"
            android:text="@string/movie_rating"
            android:textColor="@color/headTextColor"
            android:textSize="@dimen/commonTextSize" />

        <TextView
            android:id="@+id/movieDetailMovieReleaseDataTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@string/movie_detail_heading_font"
            android:gravity="center_vertical"
            android:padding="@dimen/commonPadding"
            android:text="@string/release_data"
            android:textColor="@color/headTextColor"
            android:textSize="@dimen/commonTextSize" />

    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>

运动场景 xml

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<ConstraintSet android:id="@+id/start">
    <Constraint
        android:id="@id/movieDetailPosterImageView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginEnd="0dp"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_editor_absoluteX="0dp"
        motion:layout_editor_absoluteY="0dp" />
    <Constraint
        android:id="@id/movieDetailMovieTitleTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:alpha="0"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@id/movieDetailPosterImageView" />

    <Constraint
        android:id="@id/detailScrollView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:alpha="0"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@id/movieDetailMovieTitleTextView" />
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
    <Constraint
        android:id="@id/movieDetailPosterImageView"
        android:layout_width="0dp"
        android:layout_height="250dp"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="-1dp"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent" />
    <Constraint
        android:id="@id/movieDetailMovieTitleTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:alpha="1"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@id/movieDetailPosterImageView"
        motion:layout_constraintVertical_bias="0" />

    <Constraint
        android:id="@id/detailScrollView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:alpha="1"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@id/movieDetailMovieTitleTextView" />
</ConstraintSet>

<Transition
    motion:constraintSetEnd="@id/end"
    motion:constraintSetStart="@id/start"
    motion:duration="500"
    motion:motionInterpolator="bounce">

    <OnSwipe
        motion:dragDirection="dragUp"
        motion:touchAnchorId="@id/movieDetailPosterImageView"
        motion:touchAnchorSide="bottom" />

</Transition>

如何在 MotionLayout 中使用 NestedScrollView?

相关话题:

SOF question

Issue in google's motion layout demo application

更新 #1:

    1234563 /p>
  • 也许 Motion Layout 没有重新计算 ScrollView 中的动态变化不确定。

【问题讨论】:

  • 你找到动态内容的解决方案了吗?
  • 找到解决方案了吗?

标签: android android-nestedscrollview android-motionlayout


【解决方案1】:

添加

android:descendantFocusability="blocksDescendants"

到NestedScrollView里面的LinearLayout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 2021-04-11
    相关资源
    最近更新 更多