相关疑难解决方法(0)

NestedScrollView + CodinatorLayout scrollBy() scrollTo() 方法什么都不做

我有一个 NestedScrollView 与 CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout 一起使用,具有类似于本教程的视差效果

我需要以编程方式滚动内容(最好是平滑滚动,即动画),但是调用滚动方法(scrollBy()、scrollTo()、smoothScrollTo()、smoothScrollBy())什么也不做。

请注意,我正在使用app:layout_behavior="@string/appbar_scrolling_view_behavior"<-- 不确定问题是否与此有关。

nsv_form.smoothScrollBy(0, 300)当用户单击按钮时,我正在调用Kotlin,但没有任何反应:(

(也尝试过scrollTo(),,scrollBy()+- 300,各种不同的变化)

更新:我研究了源代码,似乎这些*scroll*()方法期望布局的内容大于父视图(有道理)。就我而言,内容较小,所以我怀疑这就是滚动方法不起作用的原因。也许我需要不同的东西而不是scroll?

NestedScrollView 的位置开始部分离开屏幕,上面有一个 CollapsingToolbarLayout 中的图像,就像这样,所以看起来我需要以编程方式移动 NestedScrollView 的位置并触发 CoordinatorLayout 的滚动行为。 - 我该怎么做呢?

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

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

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/iv_image"
                android:layout_width="match_parent"
                android:layout_height="@dimen/image_height"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                tools:src="@drawable/some_image" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nsv_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout …
Run Code Online (Sandbox Code Playgroud)

android android-coordinatorlayout android-nestedscrollview

5
推荐指数
1
解决办法
1095
查看次数

NestedScrollView 的 smoothScrollTo() 表现怪异

我试图实现的是滚动到scroll_position_1tab1(等)被点击喜欢这样。我根本不明白发生了什么。下面是我的布局结构。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/scroll_position_1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/scroll_position_2"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />

        </android.support.constraint.ConstraintLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|snap">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="180dp"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
    </android.support.design.widget.TabLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

这就是我在单击选项卡时滚动但不起作用的方式:

mNestedScroller.smoothScrollTo(0, scroll_position_1_text_view.getY());    
Run Code Online (Sandbox Code Playgroud)

而将其修改为:

mNestedScroller.scrollTo(0, scroll_position_1_text_view.getY());
Run Code Online (Sandbox Code Playgroud)

工作没有问题!请帮忙。谢谢!

android android-coordinatorlayout android-nestedscrollview

1
推荐指数
1
解决办法
3160
查看次数